2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
15 """standardattributes migration
17 Revision ID: 8a6d8bdae39
19 Create Date: 2015-09-10 03:12:04.012457
23 # revision identifiers, used by Alembic.
24 revision = '8a6d8bdae39'
25 down_revision = '1b294093239c'
26 depends_on = ('32e5974ada25',)
28 from alembic import op
29 import sqlalchemy as sa
32 # basic model of the tables with required field for migration
33 TABLES = ('ports', 'networks', 'subnets', 'subnetpools', 'securitygroups',
34 'floatingips', 'routers', 'securitygrouprules')
36 (table, sa.Table(table, sa.MetaData(),
37 sa.Column('id', sa.String(length=36), nullable=False),
38 sa.Column('standard_attr_id', sa.BigInteger(),
43 standardattrs = sa.Table(
44 'standardattributes', sa.MetaData(),
45 sa.Column('id', sa.BigInteger(), primary_key=True, autoincrement=True),
46 sa.Column('resource_type', sa.String(length=255), nullable=False))
50 generate_records_for_existing()
51 for table, model in TABLE_MODELS:
52 # add constraint(s) now that everything is populated on that table.
53 # note that some MariaDB versions will *not* allow the ALTER to
54 # NOT NULL on a column that has an FK constraint, so we set NOT NULL
55 # first, then the FK constraint.
56 op.alter_column(table, 'standard_attr_id', nullable=False,
57 existing_type=sa.BigInteger(), existing_nullable=True,
58 existing_server_default=False)
59 op.create_foreign_key(
60 constraint_name=None, source_table=table,
61 referent_table='standardattributes',
62 local_cols=['standard_attr_id'], remote_cols=['id'],
64 op.create_unique_constraint(
65 constraint_name='uniq_%s0standard_attr_id' % table,
66 table_name=table, columns=['standard_attr_id'])
69 def generate_records_for_existing():
70 session = sa.orm.Session(bind=op.get_bind())
72 with session.begin(subtransactions=True):
73 for table, model in TABLE_MODELS:
74 for row in session.query(model):
75 # NOTE(kevinbenton): without this disabled, pylint complains
76 # about a missing 'dml' argument.
77 #pylint: disable=no-value-for-parameter
78 res = session.execute(
79 standardattrs.insert().values(resource_type=table))
81 model.update().values(
82 standard_attr_id=res.inserted_primary_key).where(
83 model.c.id == row[0]))
84 # this commit is necessary to allow further operations