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
This commit is contained in:
Ivan Kolodyazhny 2014-12-12 18:11:58 +02:00 committed by Ivan Kolodyazhny
parent ebc819cc52
commit 26b19e6353
3 changed files with 37 additions and 13 deletions

View 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:

View 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)

View 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