1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
14 import sqlalchemy as sql
15 from sqlalchemy import func
18 def upgrade(migrate_engine):
20 meta.bind = migrate_engine
22 user_table = sql.Table('user', meta, autoload=True)
23 local_user_table = sql.Table('local_user', meta, autoload=True)
24 password_table = sql.Table('password', meta, autoload=True)
26 # migrate data to local_user table
27 local_user_values = []
28 for row in user_table.select().execute():
29 # skip the row that already exists in `local_user`, this could
30 # happen if run into a partially-migrated table due to the
32 filter_by = local_user_table.c.user_id == row['id']
33 user_count = sql.select([func.count()]).select_from(
34 local_user_table).where(filter_by).execute().fetchone()[0]
36 local_user_values.append({'user_id': row['id'],
37 'domain_id': row['domain_id'],
40 local_user_table.insert().values(local_user_values).execute()
42 # migrate data to password table
44 sql.select([user_table, local_user_table], use_labels=True)
45 .select_from(user_table.join(local_user_table, user_table.c.id ==
46 local_user_table.c.user_id))
48 user_rows = sel.execute()
51 if row['user_password']:
52 password_values.append({'local_user_id': row['local_user_id'],
53 'password': row['user_password']})
55 password_table.insert().values(password_values).execute()
57 # remove domain_id and name unique constraint
58 if migrate_engine.name != 'sqlite':
59 migrate.UniqueConstraint(user_table.c.domain_id,
61 name='ixu_user_name_domain_id').drop()
64 user_table.c.domain_id.drop()
65 user_table.c.name.drop()
66 user_table.c.password.drop()