From: Ann Kamyshnikova Date: Tue, 1 Sep 2015 12:15:53 +0000 (+0300) Subject: Fix test_external_tables_not_changed X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=4d831a462e2510ab080be7abae49ca3cff056e61;p=openstack-build%2Fneutron-build.git Fix test_external_tables_not_changed test_external_tables_not_changed was not executed properly as new engine was created in env.py. Related-bug: #1466704 Change-Id: If02415d7abd17024946f7aee8fb6abc374a7aefe --- diff --git a/neutron/db/migration/alembic_migrations/env.py b/neutron/db/migration/alembic_migrations/env.py index 834b9ac5f..87dfa9eab 100644 --- a/neutron/db/migration/alembic_migrations/env.py +++ b/neutron/db/migration/alembic_migrations/env.py @@ -103,9 +103,11 @@ def run_migrations_online(): """ set_mysql_engine() - engine = session.create_engine(neutron_config.database.connection) - - connection = engine.connect() + connection = config.attributes.get('connection') + new_engine = connection is None + if new_engine: + engine = session.create_engine(neutron_config.database.connection) + connection = engine.connect() context.configure( connection=connection, target_metadata=target_metadata, @@ -116,8 +118,9 @@ def run_migrations_online(): with context.begin_transaction(): context.run_migrations() finally: - connection.close() - engine.dispose() + if new_engine: + connection.close() + engine.dispose() if context.is_offline_mode(): diff --git a/neutron/tests/functional/db/test_migrations.py b/neutron/tests/functional/db/test_migrations.py index b8e476073..959c5c115 100644 --- a/neutron/tests/functional/db/test_migrations.py +++ b/neutron/tests/functional/db/test_migrations.py @@ -20,6 +20,7 @@ import alembic import alembic.autogenerate import alembic.migration from alembic import script as alembic_script +from contextlib import contextmanager import mock from oslo_config import cfg from oslo_config import fixture as config_fixture @@ -207,6 +208,14 @@ class _TestModelsMigrations(test_migrations.ModelsMigrationsSync): class TestModelsMigrationsMysql(_TestModelsMigrations, base.MySQLTestCase): + @contextmanager + def _listener(self, engine, listener_func): + try: + event.listen(engine, 'before_execute', listener_func) + yield + finally: + event.remove(engine, 'before_execute', + listener_func) # There is no use to run this against both dialects, so add this test just # for MySQL tests @@ -222,20 +231,30 @@ class TestModelsMigrationsMysql(_TestModelsMigrations, "migration.") if hasattr(clauseelement, 'element'): - if (clauseelement.element.name in external.TABLES or + element = clauseelement.element + if (element.name in external.TABLES or (hasattr(clauseelement, 'table') and - clauseelement.element.table.name in external.TABLES)): + element.table.name in external.TABLES)): + # Table 'nsxv_vdr_dhcp_bindings' was created in liberty, + # before NSXV has moved to separate repo. + if ((isinstance(clauseelement, + sqlalchemy.sql.ddl.CreateTable) and + element.name == 'nsxv_vdr_dhcp_bindings')): + return self.fail("External table referenced by neutron core " "migration.") engine = self.get_engine() cfg.CONF.set_override('connection', engine.url, group='database') - migration.do_alembic_command(self.alembic_config, 'upgrade', 'kilo') - - event.listen(engine, 'before_execute', block_external_tables) - migration.do_alembic_command(self.alembic_config, 'upgrade', 'heads') - - event.remove(engine, 'before_execute', block_external_tables) + with engine.begin() as connection: + self.alembic_config.attributes['connection'] = connection + migration.do_alembic_command(self.alembic_config, 'upgrade', + 'kilo') + + with self._listener(engine, + block_external_tables): + migration.do_alembic_command(self.alembic_config, 'upgrade', + 'heads') class TestModelsMigrationsPsql(_TestModelsMigrations,