]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Check DB scheme prior to migration to Ml2
authorJakub Libosvar <libosvar@redhat.com>
Wed, 28 May 2014 16:39:47 +0000 (18:39 +0200)
committerJakub Libosvar <libosvar@redhat.com>
Tue, 3 Jun 2014 11:30:05 +0000 (13:30 +0200)
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

index 858e08cf6c5afbd052b7d9b1c8fcc3da9268bbc8..504061ed7cc4c732b6c930974d5793a004987656 100755 (executable)
@@ -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