Merge "Fix test_external_tables_not_changed"

This commit is contained in:
Jenkins 2015-09-09 16:51:54 +00:00 committed by Gerrit Code Review
commit 0e32bb7de4
2 changed files with 34 additions and 12 deletions

View File

@ -108,9 +108,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,
@ -122,8 +124,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
@ -205,6 +206,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
@ -220,20 +229,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,