diff --git a/keystone/common/sql/migrate_repo/versions/008_normalize_identity.py b/keystone/common/sql/migrate_repo/versions/008_normalize_identity.py index d08ad7c8c0..3f5ae3b884 100644 --- a/keystone/common/sql/migrate_repo/versions/008_normalize_identity.py +++ b/keystone/common/sql/migrate_repo/versions/008_normalize_identity.py @@ -16,19 +16,54 @@ from sqlalchemy import Column, MetaData, String, Table, Text, types +from sqlalchemy.orm import sessionmaker -#this won't work on sqlite. It doesn't support dropping columns +#sqlite doesn't support dropping columns. Copy to a new table instead def downgrade_user_table(meta, migrate_engine): - user_table = Table('user', meta, autoload=True) - user_table.columns["password"].drop() - user_table.columns["enabled"].drop() + maker = sessionmaker(bind=migrate_engine) + session = maker() + session.execute("ALTER TABLE user RENAME TO orig_user;") + + user_table = Table( + 'user', + meta, + Column('id', String(64), primary_key=True), + Column('name', String(64), unique=True, nullable=False), + Column('extra', Text())) + user_table.create(migrate_engine, checkfirst=True) + + orig_user_table = Table('orig_user', meta, autoload=True) + for user in session.query(orig_user_table): + session.execute("insert into user (id, name, extra) " + "values ( :id, :name, :extra);", + {'id': user.id, + 'name': user.name, + 'extra': user.extra}) + session.execute("drop table orig_user;") def downgrade_tenant_table(meta, migrate_engine): - tenant_table = Table('tenant', meta, autoload=True) - tenant_table.columns["description"].drop() - tenant_table.columns["enabled"].drop() + maker = sessionmaker(bind=migrate_engine) + session = maker() + session.execute("ALTER TABLE tenant RENAME TO orig_tenant;") + + tenant_table = Table( + 'tenant', + meta, + Column('id', String(64), primary_key=True), + Column('name', String(64), unique=True, nullable=False), + Column('extra', Text())) + tenant_table.create(migrate_engine, checkfirst=True) + + orig_tenant_table = Table('orig_tenant', meta, autoload=True) + for tenant in session.query(orig_tenant_table): + session.execute("insert into tenant (id, name, extra) " + "values ( :id, :name, :extra);", + {'id': tenant.id, + 'name': tenant.name, + 'extra': tenant.extra}) + session.execute("drop table orig_tenant;") def upgrade_user_table(meta, migrate_engine): diff --git a/keystone/common/sql/migrate_repo/versions/008_sqlite_downgrade.sql b/keystone/common/sql/migrate_repo/versions/008_sqlite_downgrade.sql deleted file mode 100644 index 4dec683afa..0000000000 --- a/keystone/common/sql/migrate_repo/versions/008_sqlite_downgrade.sql +++ /dev/null @@ -1,5 +0,0 @@ --- not supported by sqlite, but should be: --- alter TABLE tenant drop column description; --- alter TABLE tenant drop column enabled; --- The downgrade process will fail without valid SQL in this file -select count(*) from tenant; diff --git a/tests/test_sql_upgrade.py b/tests/test_sql_upgrade.py index b474786228..6d94b25869 100644 --- a/tests/test_sql_upgrade.py +++ b/tests/test_sql_upgrade.py @@ -121,6 +121,9 @@ class SqlUpgradeTests(test.TestCase): session.commit() def test_downgrade_9_to_7(self): + self.upgrade(7) + self.populate_user_table() + self.populate_tenant_table() self.upgrade(9) self.downgrade(7)