diff --git a/neutron/db/migration/__init__.py b/neutron/db/migration/__init__.py index 4151770e8..6b367233b 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 43e1dd328..677d6f291 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 c13ebeaf2..8d1178ce0 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 317dca35e..1c55ce7f2 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')