]> review.fuel-infra Code Review - openstack-build/neutron-build.git/blob
b2a424ce0754bb4d68af6b0002adb92cf8795d4b
[openstack-build/neutron-build.git] /
1 #
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
5 #
6 #         http://www.apache.org/licenses/LICENSE-2.0
7 #
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
12 #    under the License.
13 #
14
15 """standardattributes migration
16
17 Revision ID: 8a6d8bdae39
18 Revises: 1b294093239c
19 Create Date: 2015-09-10 03:12:04.012457
20
21 """
22
23 # revision identifiers, used by Alembic.
24 revision = '8a6d8bdae39'
25 down_revision = '1b294093239c'
26 depends_on = ('32e5974ada25',)
27
28 from alembic import op
29 import sqlalchemy as sa
30
31
32 # basic model of the tables with required field for migration
33 TABLES = ('ports', 'networks', 'subnets', 'subnetpools', 'securitygroups',
34           'floatingips', 'routers', 'securitygrouprules')
35 TABLE_MODELS = [
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(),
39                                nullable=True)))
40     for table in TABLES
41 ]
42
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))
47
48
49 def upgrade():
50     generate_records_for_existing()
51     for table, model in TABLE_MODELS:
52         # add the constraint now that everything is populated on that table
53         op.create_foreign_key(
54             constraint_name=None, source_table=table,
55             referent_table='standardattributes',
56             local_cols=['standard_attr_id'], remote_cols=['id'],
57             ondelete='CASCADE')
58         op.alter_column(table, 'standard_attr_id', nullable=False,
59                         existing_type=sa.BigInteger(), existing_nullable=True,
60                         existing_server_default=False)
61         op.create_unique_constraint(
62             constraint_name='uniq_%s0standard_attr_id' % table,
63             table_name=table, columns=['standard_attr_id'])
64
65
66 def generate_records_for_existing():
67     session = sa.orm.Session(bind=op.get_bind())
68     values = []
69     with session.begin(subtransactions=True):
70         for table, model in TABLE_MODELS:
71             for row in session.query(model):
72                 # NOTE(kevinbenton): without this disabled, pylint complains
73                 # about a missing 'dml' argument.
74                 #pylint: disable=no-value-for-parameter
75                 res = session.execute(
76                     standardattrs.insert().values(resource_type=table))
77                 session.execute(
78                     model.update().values(
79                         standard_attr_id=res.inserted_primary_key).where(
80                             model.c.id == row[0]))
81     # this commit is necessary to allow further operations
82     session.commit()
83     return values