From a83bfb9f5749d63ee756d9dd708a5c5d24267405 Mon Sep 17 00:00:00 2001 From: Morgan Fainberg Date: Tue, 1 Apr 2014 17:56:13 -0700 Subject: [PATCH] Ignore migrate versioning tables in utf8 sanity check The migration version tables for alembic and sqlalchemy-migrate should not be validated to be utf8. This is because these tools are not explicitly part of the database schema defined in the projects. In certain configurations it is not possible to ensure these tables will be utf8 character set unless a change to the my.cnf or explicit character set on table creation is specified. Change-Id: Ib5c0f93c788afda92aad862e50dc086dc39605f1 Closes-Bug: #1301036 --- openstack/common/db/sqlalchemy/migration.py | 11 +++++++++-- tests/unit/db/sqlalchemy/test_migration_common.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/openstack/common/db/sqlalchemy/migration.py b/openstack/common/db/sqlalchemy/migration.py index bc5a7160..44ce0970 100644 --- a/openstack/common/db/sqlalchemy/migration.py +++ b/openstack/common/db/sqlalchemy/migration.py @@ -213,8 +213,15 @@ def _db_schema_sanity_check(engine): 'where TABLE_SCHEMA=%s and ' 'TABLE_COLLATION NOT LIKE "%%utf8%%"') - table_names = [res[0] for res in engine.execute(onlyutf8_sql, - engine.url.database)] + # NOTE(morganfainberg): exclude the sqlalchemy-migrate and alembic + # versioning tables from the tables we need to verify utf8 status on. + # Non-standard table names are not supported. + EXCLUDED_TABLES = ['migrate_version', 'alembic_version'] + + table_names = [res[0] for res in + engine.execute(onlyutf8_sql, engine.url.database) if + res[0].lower() not in EXCLUDED_TABLES] + if len(table_names) > 0: raise ValueError(_('Tables "%s" have non utf8 collation, ' 'please make sure all tables are CHARSET=utf8' diff --git a/tests/unit/db/sqlalchemy/test_migration_common.py b/tests/unit/db/sqlalchemy/test_migration_common.py index 91d746bf..789c1689 100644 --- a/tests/unit/db/sqlalchemy/test_migration_common.py +++ b/tests/unit/db/sqlalchemy/test_migration_common.py @@ -196,3 +196,16 @@ class TestMigrationCommon(test_base.DbTestCase): self.assertRaises(ValueError, migration._db_schema_sanity_check, mock_eng) + + def test_db_sanity_table_not_utf8_exclude_migrate_tables(self): + with mock.patch.object(self, 'engine') as mock_eng: + type(mock_eng).name = mock.PropertyMock(return_value='mysql') + # NOTE(morganfainberg): Check both lower and upper case versions + # of the migration table names (validate case insensitivity in + # the sanity check. + mock_eng.execute.return_value = [['migrate_version', 'latin1'], + ['alembic_version', 'latin1'], + ['MIGRATE_VERSION', 'latin1'], + ['ALEMBIC_VERSION', 'latin1']] + + migration._db_schema_sanity_check(mock_eng)