From c0fb42182ccb6da0a652aab68569a74d449a6301 Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Tue, 22 Jul 2014 19:04:00 +0400 Subject: [PATCH] Fix migration set_length_of_description_field_metering If PostgreSQL version is less than 9.1.13 migration set_length_of_description_field_metering fails as there is no special 'create if not exist' expression. This change add special function for this case. Closes-bug: #1348138 Change-Id: Ibe4f370fe400072abd281bd2313261790335eae2 --- neutron/db/migration/__init__.py | 15 +++++++++++++++ ...set_length_of_description_field_metering.py | 18 +++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/neutron/db/migration/__init__.py b/neutron/db/migration/__init__.py index 6b367233b..2f8fae257 100644 --- a/neutron/db/migration/__init__.py +++ b/neutron/db/migration/__init__.py @@ -51,3 +51,18 @@ def alter_enum(table, column, enum_type, nullable): else: op.alter_column(table, column, type_=enum_type, existing_nullable=nullable) + + +def create_table_if_not_exist_psql(table_name, values): + if op.get_bind().engine.dialect.server_version_info < (9, 1, 0): + op.execute("CREATE LANGUAGE plpgsql") + op.execute("CREATE OR REPLACE FUNCTION execute(TEXT) RETURNS VOID AS $$" + "BEGIN EXECUTE $1; END;" + "$$ LANGUAGE plpgsql STRICT;") + op.execute("CREATE OR REPLACE FUNCTION table_exist(TEXT) RETURNS bool as " + "$$ SELECT exists(select 1 from pg_class where relname=$1);" + "$$ language sql STRICT;") + op.execute("SELECT execute($$CREATE TABLE %(name)s %(columns)s $$) " + "WHERE NOT table_exist(%(name)r);" % + {'name': table_name, + 'columns': values}) \ No newline at end of file diff --git a/neutron/db/migration/alembic_migrations/versions/33c3db036fe4_set_length_of_description_field_metering.py b/neutron/db/migration/alembic_migrations/versions/33c3db036fe4_set_length_of_description_field_metering.py index 0882aa7f4..28dd886d7 100644 --- a/neutron/db/migration/alembic_migrations/versions/33c3db036fe4_set_length_of_description_field_metering.py +++ b/neutron/db/migration/alembic_migrations/versions/33c3db036fe4_set_length_of_description_field_metering.py @@ -41,11 +41,19 @@ def upgrade(active_plugins=None, options=None): if not migration.should_run(active_plugins, migration_for_plugins): return - op.execute("CREATE TABLE IF NOT EXISTS meteringlabels( " - "tenant_id VARCHAR(255) NULL, " - "id VARCHAR(36) PRIMARY KEY NOT NULL, " - "name VARCHAR(255) NULL, " - "description VARCHAR(255) NULL)") + if op.get_bind().engine.dialect.name == 'postgresql': + migration.create_table_if_not_exist_psql( + 'meteringlabels', + "(tenant_id VARCHAR(255) NULL, " + "id VARCHAR(36) PRIMARY KEY NOT NULL, " + "name VARCHAR(255) NULL, " + "description VARCHAR(255) NULL)") + else: + op.execute("CREATE TABLE IF NOT EXISTS meteringlabels( " + "tenant_id VARCHAR(255) NULL, " + "id VARCHAR(36) PRIMARY KEY NOT NULL, " + "name VARCHAR(255) NULL, " + "description VARCHAR(255) NULL)") op.alter_column('meteringlabels', 'description', type_=sa.String(1024), existing_nullable=True) -- 2.45.2