diff --git a/oslo_db/sqlalchemy/test_migrations.py b/oslo_db/sqlalchemy/test_migrations.py index 2f9372e..5204b85 100644 --- a/oslo_db/sqlalchemy/test_migrations.py +++ b/oslo_db/sqlalchemy/test_migrations.py @@ -555,6 +555,20 @@ class ModelsMigrationsSync(object): return self.FKInfo((fk.parent.name,), fk.column.table.name, (fk.column.name,)) + def filter_metadata_diff(self, diff): + """Filter changes before assert in test_models_sync(). + + Allow subclasses to whitelist/blacklist changes. By default, no + filtering is performed, changes are returned as is. + + :param diff: a list of differences (see `compare_metadata()` docs for + details on format) + :returns: a list of differences + + """ + + return diff + def test_models_sync(self): # recent versions of sqlalchemy and alembic are needed for running of # this test, but we already have them in requirements @@ -580,8 +594,8 @@ class ModelsMigrationsSync(object): mc = alembic.migration.MigrationContext.configure(conn, opts=opts) # compare schemas and fail with diff, if it's not empty - diff = alembic.autogenerate.compare_metadata(mc, - self.get_metadata()) + diff = self.filter_metadata_diff( + alembic.autogenerate.compare_metadata(mc, self.get_metadata())) if diff: msg = pprint.pformat(diff, indent=2, width=20) self.fail( diff --git a/oslo_db/tests/sqlalchemy/test_migrations.py b/oslo_db/tests/sqlalchemy/test_migrations.py index 96e7e31..3f37502 100644 --- a/oslo_db/tests/sqlalchemy/test_migrations.py +++ b/oslo_db/tests/sqlalchemy/test_migrations.py @@ -245,6 +245,53 @@ class ModelsMigrationSyncMixin(test_base.DbTestCase): else: return True + def _test_models_not_sync_filtered(self): + self.metadata_migrations.clear() + sa.Table( + 'table', self.metadata_migrations, + sa.Column('fk_check', sa.String(36), nullable=False), + sa.PrimaryKeyConstraint('fk_check'), + mysql_engine='InnoDB' + ) + + sa.Table( + 'testtbl', self.metadata_migrations, + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('spam', sa.String(8), nullable=True), + sa.Column('eggs', sa.DateTime), + sa.Column('foo', sa.Boolean, + server_default=sa.sql.expression.false()), + sa.Column('bool_wo_default', sa.Boolean, unique=True), + sa.Column('bar', sa.BigInteger), + sa.Column('defaulttest', sa.Integer, server_default='7'), + sa.Column('defaulttest2', sa.String(8), server_default=''), + sa.Column('defaulttest3', sa.String(5), server_default="fake"), + sa.Column('defaulttest4', + sa.Enum('first', 'second', name='testenum'), + server_default="first"), + sa.Column('fk_check', sa.String(36), nullable=False), + sa.UniqueConstraint('spam', 'foo', name='uniq_cons'), + sa.ForeignKeyConstraint(['fk_check'], ['table.fk_check']), + mysql_engine='InnoDB' + ) + + with mock.patch.object(self, 'filter_metadata_diff') as filter_mock: + def filter_diffs(diffs): + # test filter returning only constraint related diffs + return [ + diff + for diff in diffs + if 'constraint' in diff[0] + ] + filter_mock.side_effect = filter_diffs + + msg = six.text_type(self.assertRaises(AssertionError, + self.test_models_sync)) + self.assertNotIn('defaulttest', msg) + self.assertNotIn('defaulttest3', msg) + self.assertNotIn('remove_fk', msg) + self.assertIn('constraint', msg) + def _test_models_not_sync(self): self.metadata_migrations.clear() sa.Table( @@ -300,6 +347,9 @@ class ModelsMigrationsSyncMysql(ModelsMigrationSyncMixin, def test_models_not_sync(self): self._test_models_not_sync() + def test_models_not_sync_filtered(self): + self._test_models_not_sync_filtered() + class ModelsMigrationsSyncPsql(ModelsMigrationSyncMixin, migrate.ModelsMigrationsSync, @@ -308,6 +358,9 @@ class ModelsMigrationsSyncPsql(ModelsMigrationSyncMixin, def test_models_not_sync(self): self._test_models_not_sync() + def test_models_not_sync_filtered(self): + self._test_models_not_sync_filtered() + class TestOldCheckForeignKeys(test_base.DbTestCase): def setUp(self):