]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix incorrect change of Enum type
authorAnn Kamyshnikova <akamyshnikova@mirantis.com>
Wed, 2 Apr 2014 14:14:26 +0000 (18:14 +0400)
committerAnn Kamyshnikova <akamyshnikova@mirantis.com>
Mon, 21 Apr 2014 09:05:32 +0000 (13:05 +0400)
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
neutron/db/migration/alembic_migrations/versions/1341ed32cc1e_nvp_netbinding_update.py
neutron/db/migration/alembic_migrations/versions/38fc1f6789f8_cisco_n1kv_overlay.py
neutron/db/migration/alembic_migrations/versions/46a0efbd8f0_cisco_n1kv_multisegm.py

index 4151770e83bd87927e6d6165020291543a7784f5..6b367233bac8f614aa6b5f867edf6b889a3e1c68 100644 (file)
@@ -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)
index 43e1dd328a7ce7247925742581d99d59dde37d9e..677d6f2914d13e8086bb89d10c874f301a6815cb 100644 (file)
@@ -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)
index c13ebeaf217d7bba00f1051388e24d408ac33965..8d1178ce0818d99c62dd878aa17efc67e7959737 100644 (file)
@@ -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)
index 317dca35eadb3661f35379517ef3944960c1723e..1c55ce7f2ceba308a670291c886ef4f1cc25c92f 100644 (file)
@@ -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')