From f6830d36f3f0d2215f47c0ed1b34bc2483aa1382 Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Wed, 2 Apr 2014 18:14:26 +0400 Subject: [PATCH] Fix incorrect change of Enum type In migration 1341ed32cc1e_nvp_netbinding_update Enum type had been changed incorrectly from ('flat', 'vlan', 'stt', 'gre') to ('flat', 'vlan', 'stt', 'gre', 'l3_ext') for PostgeSQL. The same problem is taken place for vlan_type in migrations 38fc1f6789f8_cisco_n1kv_overlay from ('vlan', 'vxlan', 'trunk', 'multi-segment') to ('vlan', 'overlay', 'trunk', 'multi-segment') and in 46a0efbd8f0_cisco_n1kv_multisegm from ('vlan', 'vxlan') to ('vlan', 'vxlan', 'trunk', 'multi-segment'). In this change request was added separate method for changing Enum type for PostgreSQL. Closes-bug: #1301396 Change-Id: I27197fb7405630a55178be8516a4b62bd135e05c --- neutron/db/migration/__init__.py | 24 +++++++++++++++++++ .../1341ed32cc1e_nvp_netbinding_update.py | 17 ++++++------- .../38fc1f6789f8_cisco_n1kv_overlay.py | 19 ++++++++------- .../46a0efbd8f0_cisco_n1kv_multisegm.py | 14 +++++------ 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/neutron/db/migration/__init__.py b/neutron/db/migration/__init__.py index 4151770e83..6b367233ba 100644 --- a/neutron/db/migration/__init__.py +++ b/neutron/db/migration/__init__.py @@ -14,6 +14,9 @@ # # @author: Mark McClain, DreamHost +from alembic import op +import sqlalchemy as sa + OVS_PLUGIN = ('neutron.plugins.openvswitch.ovs_neutron_plugin' '.OVSNeutronPluginV2') CISCO_PLUGIN = 'neutron.plugins.cisco.network_plugin.PluginV2' @@ -27,3 +30,24 @@ def should_run(active_plugins, migrate_plugins): OVS_PLUGIN in migrate_plugins): migrate_plugins.append(CISCO_PLUGIN) return set(active_plugins) & set(migrate_plugins) + + +def alter_enum(table, column, enum_type, nullable): + bind = op.get_bind() + engine = bind.engine + if engine.name == 'postgresql': + values = {'table': table, + 'column': column, + 'name': enum_type.name} + op.execute("ALTER TYPE %(name)s RENAME TO old_%(name)s" % values) + enum_type.create(bind, checkfirst=False) + op.execute("ALTER TABLE %(table)s RENAME COLUMN %(column)s TO " + "old_%(column)s" % values) + op.add_column(table, sa.Column(column, enum_type, nullable=nullable)) + op.execute("UPDATE %(table)s SET %(column)s = " + "old_%(column)s::text::%(name)s" % values) + op.execute("ALTER TABLE %(table)s DROP COLUMN old_%(column)s" % values) + op.execute("DROP TYPE old_%(name)s" % values) + else: + op.alter_column(table, column, type_=enum_type, + existing_nullable=nullable) diff --git a/neutron/db/migration/alembic_migrations/versions/1341ed32cc1e_nvp_netbinding_update.py b/neutron/db/migration/alembic_migrations/versions/1341ed32cc1e_nvp_netbinding_update.py index 43e1dd328a..677d6f2914 100644 --- a/neutron/db/migration/alembic_migrations/versions/1341ed32cc1e_nvp_netbinding_update.py +++ b/neutron/db/migration/alembic_migrations/versions/1341ed32cc1e_nvp_netbinding_update.py @@ -42,6 +42,11 @@ import sqlalchemy as sa from neutron.db import migration +new_type = sa.Enum('flat', 'vlan', 'stt', 'gre', 'l3_ext', + name='nvp_network_bindings_binding_type') +old_type = sa.Enum('flat', 'vlan', 'stt', 'gre', + name='nvp_network_bindings_binding_type') + def upgrade(active_plugins=None, options=None): if not migration.should_run(active_plugins, migration_for_plugins): @@ -50,10 +55,8 @@ def upgrade(active_plugins=None, options=None): name='phy_uuid', existing_type=sa.String(36), existing_nullable=True) - op.alter_column('nvp_network_bindings', 'binding_type', - type_=sa.Enum('flat', 'vlan', 'stt', 'gre', 'l3_ext', - name='nvp_network_bindings_binding_type'), - existing_nullable=True) + migration.alter_enum('nvp_network_bindings', 'binding_type', new_type, + nullable=False) def downgrade(active_plugins=None, options=None): @@ -63,7 +66,5 @@ def downgrade(active_plugins=None, options=None): name='tz_uuid', existing_type=sa.String(36), existing_nullable=True) - op.alter_column('nvp_network_bindings', 'binding_type', - type_=sa.Enum('flat', 'vlan', 'stt', 'gre', - name='nvp_network_bindings_binding_type'), - existing_nullable=True) + migration.alter_enum('nvp_network_bindings', 'binding_type', old_type, + nullable=False) diff --git a/neutron/db/migration/alembic_migrations/versions/38fc1f6789f8_cisco_n1kv_overlay.py b/neutron/db/migration/alembic_migrations/versions/38fc1f6789f8_cisco_n1kv_overlay.py index c13ebeaf21..8d1178ce08 100644 --- a/neutron/db/migration/alembic_migrations/versions/38fc1f6789f8_cisco_n1kv_overlay.py +++ b/neutron/db/migration/alembic_migrations/versions/38fc1f6789f8_cisco_n1kv_overlay.py @@ -30,27 +30,28 @@ migration_for_plugins = [ 'neutron.plugins.cisco.network_plugin.PluginV2' ] -from alembic import op import sqlalchemy as sa from neutron.db import migration +new_type = sa.Enum('vlan', 'overlay', 'trunk', 'multi-segment', + name='vlan_type') +old_type = sa.Enum('vlan', 'vxlan', 'trunk', 'multi-segment', + name='vlan_type') + + def upgrade(active_plugins=None, options=None): if not migration.should_run(active_plugins, migration_for_plugins): return - op.alter_column('cisco_network_profiles', 'segment_type', - existing_type=sa.Enum('vlan', 'overlay', 'trunk', - 'multi-segment'), - existing_nullable=False) + migration.alter_enum('cisco_network_profiles', 'segment_type', new_type, + nullable=False) def downgrade(active_plugins=None, options=None): if not migration.should_run(active_plugins, migration_for_plugins): return - op.alter_column('cisco_network_profiles', 'segment_type', - existing_type=sa.Enum('vlan', 'vxlan', 'trunk', - 'multi-segment'), - existing_nullable=False) + migration.alter_enum('cisco_network_profiles', 'segment_type', old_type, + nullable=False) diff --git a/neutron/db/migration/alembic_migrations/versions/46a0efbd8f0_cisco_n1kv_multisegm.py b/neutron/db/migration/alembic_migrations/versions/46a0efbd8f0_cisco_n1kv_multisegm.py index 317dca35ea..1c55ce7f2c 100644 --- a/neutron/db/migration/alembic_migrations/versions/46a0efbd8f0_cisco_n1kv_multisegm.py +++ b/neutron/db/migration/alembic_migrations/versions/46a0efbd8f0_cisco_n1kv_multisegm.py @@ -35,6 +35,9 @@ import sqlalchemy as sa from neutron.db import migration +new_type = sa.Enum('vlan', 'vxlan', 'trunk', 'multi-segment', name='vlan_type') +old_type = sa.Enum('vlan', 'vxlan', name='vlan_type') + def upgrade(active_plugins=None, options=None): if not migration.should_run(active_plugins, migration_for_plugins): @@ -60,10 +63,8 @@ def upgrade(active_plugins=None, options=None): sa.PrimaryKeyConstraint('multi_segment_id', 'segment1_id', 'segment2_id') ) - op.alter_column('cisco_network_profiles', 'segment_type', - existing_type=sa.Enum('vlan', 'vxlan', 'trunk', - 'multi-segment'), - existing_nullable=False) + migration.alter_enum('cisco_network_profiles', 'segment_type', new_type, + nullable=False) op.add_column('cisco_network_profiles', sa.Column('sub_type', sa.String(length=255), nullable=True)) @@ -74,7 +75,6 @@ def downgrade(active_plugins=None, options=None): op.drop_table('cisco_n1kv_trunk_segments') op.drop_table('cisco_n1kv_multi_segments') - op.alter_column('cisco_network_profiles', 'segment_type', - existing_type=sa.Enum('vlan', 'vxlan'), - existing_nullable=False) + migration.alter_enum('cisco_network_profiles', 'segment_type', old_type, + nullable=False) op.drop_column('cisco_network_profiles', 'sub_type')