Skip to content

Commit 322f266

Browse files
committed
DB migration to fix now invalid User.names values that are already in DB.
1 parent ce23dbd commit 322f266

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2016 Eugene Frolov <eugene@frolov.net.ru>
2+
#
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from restalchemy.storage.sql import migrations
18+
19+
20+
class MigrationStep(migrations.AbstarctMigrationStep):
21+
22+
def __init__(self):
23+
self._depends = ["0023-nullable_fio-c9cb3a.py"]
24+
25+
@property
26+
def migration_id(self):
27+
return "6b042b1d-df57-4553-a031-8ae74ffab015"
28+
29+
@property
30+
def is_manual(self):
31+
return True
32+
33+
def upgrade(self, session):
34+
expression = """
35+
UPDATE iam_users
36+
SET
37+
email = CASE
38+
WHEN email IS NULL THEN name
39+
ELSE email
40+
END,
41+
name = SUBSTRING(name, 1, POSITION('@' IN name) - 1)
42+
WHERE name LIKE '%@%';
43+
"""
44+
session.execute(expression)
45+
46+
def downgrade(self, session):
47+
pass
48+
49+
50+
migration_step = MigrationStep()

0 commit comments

Comments
 (0)