Merge "Fixes migrations for MySQL 5.6.* and MariaDB 10.1.*"

This commit is contained in:
Jenkins 2016-04-05 08:33:09 +00:00 committed by Gerrit Code Review
commit de46ece85e
2 changed files with 35 additions and 3 deletions

View File

@ -20,6 +20,7 @@ from sqlalchemy.sql.expression import update
from trove.common import cfg
from trove.db.sqlalchemy.migrate_repo.schema import Table
from trove.db.sqlalchemy import utils as db_utils
CONF = cfg.CONF
LEGACY_IMAGE_ID = "00000000-0000-0000-0000-000000000000"
@ -89,6 +90,9 @@ def upgrade(migrate_engine):
meta.bind = migrate_engine
instance_table = Table('instances', meta, autoload=True)
datastore_versions_table = Table('datastore_versions',
meta,
autoload=True)
if has_instances_wo_datastore_version(instance_table):
instances = find_all_instances_wo_datastore_version(instance_table)
@ -97,9 +101,6 @@ def upgrade(migrate_engine):
datastores_table = Table('datastores',
meta,
autoload=True)
datastore_versions_table = Table('datastore_versions',
meta,
autoload=True)
version_id = create_legacy_version(datastores_table,
datastore_versions_table,
@ -111,8 +112,24 @@ def upgrade(migrate_engine):
values=dict(datastore_version_id=version_id)
).execute()
constraint_names = db_utils.get_foreign_key_constraint_names(
engine=migrate_engine,
table='instances',
columns=['datastore_version_id'],
ref_table='datastore_versions',
ref_columns=['id'])
db_utils.drop_foreign_key_constraints(
constraint_names=constraint_names,
columns=[instance_table.c.datastore_version_id],
ref_columns=[datastore_versions_table.c.id])
instance_table.c.datastore_version_id.alter(nullable=False)
db_utils.create_foreign_key_constraints(
constraint_names=constraint_names,
columns=[instance_table.c.datastore_version_id],
ref_columns=[datastore_versions_table.c.id])
def downgrade(migrate_engine):
meta.bind = migrate_engine

View File

@ -52,3 +52,18 @@ def drop_foreign_key_constraints(constraint_names, columns,
refcolumns=ref_columns,
name=constraint_name)
fkey_constraint.drop()
def create_foreign_key_constraints(constraint_names, columns,
ref_columns):
"""Create the foreign key constraints that match the given
criteria.
:param constraint_names: List of foreign key constraint names
:param columns: List of the foreign key columns.
:param ref_columns: List of the referenced columns.
"""
for constraint_name in constraint_names:
fkey_constraint = ForeignKeyConstraint(columns=columns,
refcolumns=ref_columns,
name=constraint_name)
fkey_constraint.create()