From de10d7299ba3dce18b8e12a41830ec0e686df340 Mon Sep 17 00:00:00 2001 From: Jakub Libosvar Date: Wed, 28 May 2014 18:39:47 +0200 Subject: [PATCH] Check DB scheme prior to migration to Ml2 When using migration tool from LB/OVS plugin to Ml2, there is no guarantee current scheme is supported by migration tool. This patch checks version stored in DB by alembic and compares whether version is supported. Closes-bug: #1307720 Change-Id: I4519a0e5a0f3027675958a68d1f9e0440b177229 --- neutron/db/migration/migrate_to_ml2.py | 27 ++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/neutron/db/migration/migrate_to_ml2.py b/neutron/db/migration/migrate_to_ml2.py index 858e08cf6c5..504061ed7cc 100755 --- a/neutron/db/migration/migrate_to_ml2.py +++ b/neutron/db/migration/migrate_to_ml2.py @@ -76,6 +76,29 @@ OPENVSWITCH = 'openvswitch' ICEHOUSE = 'icehouse' +SUPPORTED_SCHEMA_VERSIONS = [ICEHOUSE] + + +def check_db_schema_version(engine, metadata): + """Check that current version of the db schema is supported.""" + version_table = sa.Table( + 'alembic_version', metadata, autoload=True, autoload_with=engine) + versions = [v[0] for v in engine.execute(version_table.select())] + if not versions: + raise ValueError(_("Missing version in alembic_versions table")) + elif len(versions) > 1: + raise ValueError(_("Multiple versions in alembic_versions table: %s") + % versions) + current_version = versions[0] + if current_version not in SUPPORTED_SCHEMA_VERSIONS: + raise SystemError(_("Unsupported database schema %(current)s. " + "Please migrate your database to one of following " + "versions: %(supported)s") + % {'current': current_version, + 'supported': ', '.join(SUPPORTED_SCHEMA_VERSIONS)} + ) + + # Duplicated from neutron.plugins.linuxbridge.common.constants to # avoid having any dependency on the linuxbridge plugin being # installed. @@ -104,9 +127,9 @@ class BaseMigrateToMl2_Icehouse(object): def __call__(self, connection_url, save_tables=False, tunnel_type=None, vxlan_udp_port=None): engine = sa.create_engine(connection_url) - #TODO(marun) Check for the db version to ensure that it can be - # safely migrated from. metadata = sa.MetaData() + check_db_schema_version(engine, metadata) + self.define_ml2_tables(metadata) # Autoload the ports table to ensure that foreign keys to it and