]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add migration tests for PostgreSQL
authorIvan Kolodyazhny <ikolodyazhny@mirantis.com>
Fri, 12 Dec 2014 16:11:58 +0000 (18:11 +0200)
committerIvan Kolodyazhny <e0ne@e0ne.info>
Mon, 12 Jan 2015 22:39:39 +0000 (00:39 +0200)
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

cinder/db/sqlalchemy/migrate_repo/versions/002_quota_class.py
cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py
cinder/tests/test_migrations.py

index 90073eca9ed2f6f4fde68eac243e3c9cc178597e..a3ed1aa388f53f2bd9a803a04143380f14da7dee 100644 (file)
@@ -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:
index c5d1ba0c34919d708bbd918104867645fb902840..8469b0954402e149e604efd7ed6543fd2027c90d 100644 (file)
@@ -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)
index 739090c1206dc764d81ad74b4424281c0fe59795..4861c35bf0c2d733af3ffb7d979e30df66ef0cfe 100644 (file)
@@ -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