~niedbalski/ubuntu/vivid/neutron/fixes-1447803

« back to all changes in this revision

Viewing changes to neutron/db/migration/alembic_migrations/versions/31d7f831a591_add_constraint_for_routerid.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-10-03 18:45:23 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20141003184523-4mt6dy1q3j8n30c9
Tags: 1:2014.2~rc1-0ubuntu1
* New upstream release candidate:
  - d/p/*: Refreshed.
  - d/control: Add python-requests-mock to BD's.
  - d/control: Align versioned requirements with upstream.
* Transition linuxbridge and openvswitch plugin users to modular
  layer 2 plugin (LP: #1323729):
  - d/control: Mark removed plugin packages as transitional, depend
    on neutron-plugin-ml2, mark oldlibs/extra.
  - d/neutron-plugin-{linuxbridge,openvswitch}.install: Drop.
  - d/control: Depend on neutron-plugin-ml2 for linuxbridge
    agent package.
  - d/neutron-plugin-linuxbridge-agent.upstart: Use ml2 plugin
    configuration files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
from alembic import op
29
29
import sqlalchemy as sa
 
30
from sqlalchemy.engine import reflection
30
31
 
31
32
TABLE_NAME = 'routerl3agentbindings'
32
33
PK_NAME = 'pk_routerl3agentbindings'
43
44
                'routerl3agentbindings_ibfk_1'}}
44
45
 
45
46
 
46
 
def upgrade(active_plugins=None, options=None):
 
47
def upgrade():
47
48
    # In order to sanitize the data during migration,
48
49
    # the current records in the table need to be verified
49
50
    # and all the duplicate records which violate the PK
50
51
    # constraint need to be removed.
51
52
    context = op.get_context()
52
 
    if context.bind.dialect.name == 'postgresql':
 
53
    if context.bind.dialect.name in ('postgresql', 'ibm_db_sa'):
53
54
        op.execute('DELETE FROM %(table)s WHERE id in ('
54
55
                   'SELECT %(table)s.id FROM %(table)s LEFT OUTER JOIN '
55
56
                   '(SELECT MIN(id) as id, router_id, l3_agent_id '
65
66
 
66
67
    op.drop_column(TABLE_NAME, 'id')
67
68
 
 
69
    # DB2 doesn't support nullable column in primary key
 
70
    if context.bind.dialect.name == 'ibm_db_sa':
 
71
        op.alter_column(
 
72
            table_name=TABLE_NAME,
 
73
            column_name='router_id',
 
74
            nullable=False
 
75
        )
 
76
        op.alter_column(
 
77
            table_name=TABLE_NAME,
 
78
            column_name='l3_agent_id',
 
79
            nullable=False
 
80
        )
 
81
 
68
82
    op.create_primary_key(
69
83
        name=PK_NAME,
70
84
        table_name=TABLE_NAME,
72
86
    )
73
87
 
74
88
 
75
 
def downgrade(active_plugins=None, options=None):
 
89
def downgrade():
76
90
 
77
91
    context = op.get_context()
78
92
    dialect = context.bind.dialect.name
79
93
 
80
94
    # Drop the existed foreign key constraints
81
95
    # In order to perform primary key changes
82
 
    op.drop_constraint(
83
 
        name=fk_names[dialect]['l3_agent_id'],
84
 
        table_name=TABLE_NAME,
85
 
        type_='foreignkey'
86
 
    )
87
 
    op.drop_constraint(
88
 
        name=fk_names[dialect]['router_id'],
89
 
        table_name=TABLE_NAME,
90
 
        type_='foreignkey'
91
 
    )
 
96
    db2fks = {}
 
97
    if dialect == 'ibm_db_sa':
 
98
        # NOTE(mriedem): In DB2 the foreign key names are randomly generated
 
99
        # if you didn't originally explicitly name them, so the name is like
 
100
        # SQLxxxxx where the suffix is a random integer.  Therefore we go
 
101
        # through and just drop all of the foreign keys and save them so we
 
102
        # can re-create them later after the primary key is dropped.
 
103
        inspector = reflection.Inspector.from_engine(op.get_bind().engine)
 
104
        db2fks = inspector.get_foreign_keys(TABLE_NAME)
 
105
        for fk in db2fks:
 
106
            op.drop_constraint(
 
107
                name=fk.get('name'),
 
108
                table_name=TABLE_NAME,
 
109
                type_='foreignkey'
 
110
            )
 
111
    else:
 
112
        op.drop_constraint(
 
113
            name=fk_names[dialect]['l3_agent_id'],
 
114
            table_name=TABLE_NAME,
 
115
            type_='foreignkey'
 
116
        )
 
117
        op.drop_constraint(
 
118
            name=fk_names[dialect]['router_id'],
 
119
            table_name=TABLE_NAME,
 
120
            type_='foreignkey'
 
121
        )
92
122
 
93
123
    op.drop_constraint(
94
124
        name=PK_NAME,
101
131
        sa.Column('id', sa.String(32))
102
132
    )
103
133
 
 
134
    if dialect == 'ibm_db_sa':
 
135
        # DB2 doesn't support nullable column in primary key
 
136
        op.alter_column(
 
137
            table_name=TABLE_NAME,
 
138
            column_name='id',
 
139
            nullable=False
 
140
        )
 
141
 
 
142
    op.create_primary_key(
 
143
        name=PK_NAME,
 
144
        table_name=TABLE_NAME,
 
145
        cols=['id']
 
146
    )
 
147
 
104
148
    # Restore the foreign key constraints
105
 
    op.create_foreign_key(
106
 
        name=fk_names[dialect]['router_id'],
107
 
        source=TABLE_NAME,
108
 
        referent='routers',
109
 
        local_cols=['router_id'],
110
 
        remote_cols=['id'],
111
 
        ondelete='CASCADE'
112
 
    )
113
 
 
114
 
    op.create_foreign_key(
115
 
        name=fk_names[dialect]['l3_agent_id'],
116
 
        source=TABLE_NAME,
117
 
        referent='agents',
118
 
        local_cols=['l3_agent_id'],
119
 
        remote_cols=['id'],
120
 
        ondelete='CASCADE'
121
 
    )
122
 
 
123
 
    op.create_primary_key(
124
 
        name=PK_NAME,
125
 
        table_name=TABLE_NAME,
126
 
        cols=['id']
127
 
    )
 
149
    if dialect == 'ibm_db_sa':
 
150
        for fk in db2fks:
 
151
            op.create_foreign_key(
 
152
                name=fk.get('name'),
 
153
                source=TABLE_NAME,
 
154
                referent=fk.get('referred_table'),
 
155
                local_cols=fk.get('constrained_columns'),
 
156
                remote_cols=fk.get('referred_columns'),
 
157
                ondelete='CASCADE'
 
158
            )
 
159
    else:
 
160
        op.create_foreign_key(
 
161
            name=fk_names[dialect]['router_id'],
 
162
            source=TABLE_NAME,
 
163
            referent='routers',
 
164
            local_cols=['router_id'],
 
165
            remote_cols=['id'],
 
166
            ondelete='CASCADE'
 
167
        )
 
168
 
 
169
        op.create_foreign_key(
 
170
            name=fk_names[dialect]['l3_agent_id'],
 
171
            source=TABLE_NAME,
 
172
            referent='agents',
 
173
            local_cols=['l3_agent_id'],
 
174
            remote_cols=['id'],
 
175
            ondelete='CASCADE'
 
176
        )