From 1da78c924e31b21ed19d10676064c8540929589d Mon Sep 17 00:00:00 2001
From: Ihar Hrachyshka <ihrachys@redhat.com>
Date: Mon, 7 Aug 2017 10:09:13 -0700
Subject: [PATCH] tests: apply latest release milestone alembic scripts first

Before walking through migration scripts that belong to the release
under development, we should first execute all scripts for the latest
official release because it serves a convergence point. Without it, we
can't guarantee that calling --expand will succeed because some of new
scripts may depend on schema state that is defined by contract scripts.

Change-Id: I501b38900fb2f4409839ecd09be4d937cf20c6a9
Related-Bug: #1671634
---
 neutron/db/migration/__init__.py               |  1 +
 neutron/tests/functional/db/test_migrations.py | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/neutron/db/migration/__init__.py b/neutron/db/migration/__init__.py
index d8f29926b2a..215efe710ce 100644
--- a/neutron/db/migration/__init__.py
+++ b/neutron/db/migration/__init__.py
@@ -34,6 +34,7 @@ NEUTRON_MILESTONES = [
     LIBERTY,
     MITAKA,
     NEWTON,
+    OCATA,
     # Do not add the milestone until the end of the release
 ]
 
diff --git a/neutron/tests/functional/db/test_migrations.py b/neutron/tests/functional/db/test_migrations.py
index 263352b1616..714000f0af2 100644
--- a/neutron/tests/functional/db/test_migrations.py
+++ b/neutron/tests/functional/db/test_migrations.py
@@ -27,6 +27,7 @@ import sqlalchemy
 from sqlalchemy import event  # noqa
 from sqlalchemy.sql import ddl as sqla_ddl
 
+from neutron.db import migration as migration_root
 from neutron.db.migration.alembic_migrations import external
 from neutron.db.migration import cli as migration
 from neutron.db.migration.models import head as head_models
@@ -290,10 +291,19 @@ class _TestModelsMigrations(test_migrations.ModelsMigrationsSync):
         find_migration_exceptions()
         engine = self.engine
         cfg.CONF.set_override('connection', engine.url, group='database')
+
         with engine.begin() as connection:
             self.alembic_config.attributes['connection'] = connection
-            migration.do_alembic_command(self.alembic_config, 'upgrade',
-                                         'kilo')
+
+            # upgrade to latest release first; --expand users are expected to
+            # apply all alembic scripts from previous releases before applying
+            # the new ones
+            for release in migration_root.NEUTRON_MILESTONES:
+                release_revisions = migration._find_milestone_revisions(
+                    self.alembic_config, release)
+                for rev in release_revisions:
+                    migration.do_alembic_command(
+                        self.alembic_config, 'upgrade', rev[0])
 
             with self._listener(engine, check_expand_branch):
                 migration.do_alembic_command(