From 6e525647f608839890a8a00a8e03a540a1891471 Mon Sep 17 00:00:00 2001 From: Abhishek Kekane Date: Wed, 21 Aug 2019 10:38:14 +0000 Subject: [PATCH] Fix INSTR not supported for postgresql Migration script 'train_migrate01_backend_to_store.py' doesn't support INSTR function for postgresql database. INSTR function does support sqlite3 and mysql. Used 'POSITION' function instead of 'INSTR' function if database is postgresql. Closes-Bug: #1840898 Change-Id: I62c592f0885927b873ed35a1efe4a0f8407e7dee --- .../train_migrate01_backend_to_store.py | 13 +++++++++++++ .../db/migrations/test_train_migrate01.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/glance/db/sqlalchemy/alembic_migrations/data_migrations/train_migrate01_backend_to_store.py b/glance/db/sqlalchemy/alembic_migrations/data_migrations/train_migrate01_backend_to_store.py index cfb2b82088..ab777b69cf 100644 --- a/glance/db/sqlalchemy/alembic_migrations/data_migrations/train_migrate01_backend_to_store.py +++ b/glance/db/sqlalchemy/alembic_migrations/data_migrations/train_migrate01_backend_to_store.py @@ -24,6 +24,12 @@ def has_migrations(engine): """ sql_query = ("select meta_data from image_locations where " "INSTR(meta_data, '\"backend\":') > 0") + + # NOTE(abhishekk): INSTR function doesn't supported in postgresql + if engine.name == 'postgresql': + sql_query = ("select meta_data from image_locations where " + "POSITION('\"backend\":' IN meta_data) > 0") + with engine.connect() as con: metadata_backend = con.execute(sql_query) if metadata_backend.rowcount > 0: @@ -37,6 +43,13 @@ def migrate(engine): sql_query = ("UPDATE image_locations SET meta_data = REPLACE(meta_data, " "'\"backend\":', '\"store\":') where INSTR(meta_data, " " '\"backend\":') > 0") + + # NOTE(abhishekk): INSTR function doesn't supported in postgresql + if engine.name == 'postgresql': + sql_query = ("UPDATE image_locations SET meta_data = REPLACE(" + "meta_data, '\"backend\":', '\"store\":') where " + "POSITION('\"backend\":' IN meta_data) > 0") + with engine.connect() as con: migrated_rows = con.execute(sql_query) return migrated_rows.rowcount diff --git a/glance/tests/functional/db/migrations/test_train_migrate01.py b/glance/tests/functional/db/migrations/test_train_migrate01.py index 6d13eb753c..8cfbf55128 100644 --- a/glance/tests/functional/db/migrations/test_train_migrate01.py +++ b/glance/tests/functional/db/migrations/test_train_migrate01.py @@ -99,6 +99,11 @@ class TestTrainMigrate01MySQL(TestTrainMigrate01Mixin, pass +class TestTrain01PostgresSQL(TestTrainMigrate01Mixin, + test_base.PostgreSQLOpportunisticTestCase): + pass + + class TestTrainMigrate01_EmptyDBMixin(test_migrations.AlembicMigrationsMixin): """This mixin is used to create an initial glance database and upgrade it up to the train_expand01 revision. @@ -127,5 +132,9 @@ class TestTrainMigrate01_EmptyDBMixin(test_migrations.AlembicMigrationsMixin): class TestTrainMigrate01_EmptyDBMySQL(TestTrainMigrate01_EmptyDBMixin, test_base.MySQLOpportunisticTestCase): - """This test runs the Train data migrations on an empty databse.""" + pass + + +class TestTrainMigrate01_PySQL(TestTrainMigrate01_EmptyDBMixin, + test_base.PostgreSQLOpportunisticTestCase): pass