From: Ivan Kolodyazhny Date: Fri, 12 Dec 2014 16:11:58 +0000 (+0200) Subject: Add migration tests for PostgreSQL X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=26b19e6353577b0dda52f921c3fe48bdb8e1a1c6;p=openstack-build%2Fcinder-build.git Add migration tests for PostgreSQL Fix migrations for PostgresSQL: 002_quota_class - PostgreSQL-specific reservations foreign key name 004_volume_type_to_uuid - while downgraging volume_type_id from string to int PostgreSQL can't do it automatically Change-Id: I68b8608a85594501536835ddc9dc87bcd2dd80c4 Closes-Bug: #1266595 --- diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/002_quota_class.py b/cinder/db/sqlalchemy/migrate_repo/versions/002_quota_class.py index 90073eca9..a3ed1aa38 100644 --- a/cinder/db/sqlalchemy/migrate_repo/versions/002_quota_class.py +++ b/cinder/db/sqlalchemy/migrate_repo/versions/002_quota_class.py @@ -111,21 +111,29 @@ def downgrade(migrate_engine): meta = MetaData() meta.bind = migrate_engine - if migrate_engine.name == 'mysql': - # NOTE(jsbryant): MySQL Cannot drop the quota_usages table - # until the foreign key reservations_ibfk_1 is removed. We - # remove the foreign key first, and then we drop the table. - table = Table('reservations', meta, autoload=True) - ref_table = Table('reservations', meta, autoload=True) - params = {'columns': [table.c['usage_id']], - 'refcolumns': [ref_table.c['id']], - 'name': 'reservations_ibfk_1'} + fk_name = None + if migrate_engine.name == 'mysql': + fk_name = 'reservations_ibfk_1' + elif migrate_engine.name == 'postgresql': + fk_name = 'reservations_usage_id_fkey' + + # NOTE: MySQL and PostgreSQL Cannot drop the quota_usages table + # until the foreign key is removed. We remove the foreign key first, + # and then we drop the table. + table = Table('reservations', meta, autoload=True) + ref_table = Table('reservations', meta, autoload=True) + params = {'columns': [table.c['usage_id']], + 'refcolumns': [ref_table.c['id']], + 'name': fk_name} + + if fk_name: try: fkey = ForeignKeyConstraint(**params) fkey.drop() except Exception: - LOG.error(_LE("Dropping foreign key reservations_ibfk_1 failed.")) + msg = _LE("Dropping foreign key %s failed.") + LOG.error(msg, fk_name) quota_classes = Table('quota_classes', meta, autoload=True) try: diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py b/cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py index c5d1ba0c3..8469b0954 100644 --- a/cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py +++ b/cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py @@ -133,9 +133,20 @@ def downgrade(migrate_engine): new_id += 1 - volumes.c.volume_type_id.alter(Integer) - volume_types.c.id.alter(Integer) - extra_specs.c.volume_type_id.alter(Integer) + if migrate_engine.name == 'postgresql': + # NOTE(e0ne): PostgreSQL can't cast string to int automatically + table_column_pairs = [('volumes', 'volume_type_id'), + ('volume_types', 'id'), + ('volume_type_extra_specs', 'volume_type_id')] + sql = 'ALTER TABLE {0} ALTER COLUMN {1} ' + \ + 'TYPE INTEGER USING {1}::numeric' + + for table, column in table_column_pairs: + migrate_engine.execute(sql.format(table, column)) + else: + volumes.c.volume_type_id.alter(Integer) + volume_types.c.id.alter(Integer) + extra_specs.c.volume_type_id.alter(Integer) for column in fkey_remove_list: fkeys = list(column.foreign_keys) diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 739090c12..4861c35bf 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -759,3 +759,8 @@ class TestMysqlMigrations(test_base.MySQLOpportunisticTestCase, "and TABLE_NAME!='migrate_version'") count = noninnodb.scalar() self.assertEqual(count, 0, "%d non InnoDB tables created" % count) + + +class TestPostgresqlMigrations(test_base.PostgreSQLOpportunisticTestCase, + MigrationsMixin): + TIME_TYPE = sqlalchemy.types.TIMESTAMP