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
This commit is contained in:
Ann Kamyshnikova 2015-09-01 15:15:53 +03:00
parent f4e980f7d5
commit 4d831a462e
2 changed files with 34 additions and 12 deletions

View File

@ -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():

View File

@ -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')
with engine.begin() as connection:
self.alembic_config.attributes['connection'] = connection
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 self._listener(engine,
block_external_tables):
migration.do_alembic_command(self.alembic_config, 'upgrade',
'heads')
class TestModelsMigrationsPsql(_TestModelsMigrations,