Debian Codegraph @

neutron_fwaas/db/migration/alembic_migrations/versions/540142f314f4_fwaas_router_insertion.py

raw symbols SCIP
1 # Copyright 2014 OpenStack Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15
16 """FWaaS router insertion
17
18 Revision ID: 540142f314f4
19 Revises: 4202e3047e47
20 Create Date: 2015-02-06 17:02:24.279337
21
22 """
23
24 from alembic import op
25 import sqlalchemy as sa
26 from sqlalchemy.engine import reflection
27
28 # revision identifiers, used by Alembic.
29 revision = '540142f314f4'
30 down_revision = '4202e3047e47'
31
32 SQL_STATEMENT = (
33 "insert into firewall_router_associations "
34 "select "
35 "f.id as fw_id, r.id as router_id "
36 "from firewalls f, routers r "
37 "where "
38 "f.tenant_id=r.%s"
39 )
40
41
42 def upgrade():
43 op.create_table('firewall_router_associations',
44 sa.Column('fw_id', sa.String(length=36), nullable=False),
45 sa.Column('router_id', sa.String(
46 length=36), nullable=False),
47 sa.ForeignKeyConstraint(['fw_id'], ['firewalls.id'],
48 ondelete='CASCADE'),
49 sa.ForeignKeyConstraint(['router_id'], ['routers.id'],
50 ondelete='CASCADE'),
51 sa.PrimaryKeyConstraint('fw_id', 'router_id'),
52 )
53
54 # Depending on when neutron-fwaas is installed with neutron, this script
55 # may be run before or after the neutron core tables have had their
56 # tenant_id columns renamed to project_id. Account for both scenarios.
57 bind = op.get_bind()
58 insp = reflection.Inspector.from_engine(bind)
59 columns = insp.get_columns('routers')
60 if 'tenant_id' in [c['name'] for c in columns]:
61 op.execute(SQL_STATEMENT % 'tenant_id')
62 else:
63 op.execute(SQL_STATEMENT % 'project_id')