Skip to content

Commit 5ca312d

Browse files
committed
wip: migration
1 parent 47cafaf commit 5ca312d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (C) 2017 Google Inc.
2+
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
3+
4+
"""
5+
Add audit FK to assessments and issues
6+
7+
Create Date: 2017-03-02 15:57:57.006126
8+
"""
9+
# disable Invalid constant name pylint warning for mandatory Alembic variables.
10+
# pylint: disable=invalid-name
11+
12+
import sqlalchemy as sa
13+
14+
from alembic import op
15+
16+
17+
# revision identifiers, used by Alembic.
18+
revision = '2127ea770285'
19+
down_revision = '3f615f3b5192'
20+
21+
22+
def upgrade_table(table_name, model_name):
23+
op.add_column(
24+
table_name,
25+
sa.Column("audit_id", sa.Integer(), nullable=True)
26+
)
27+
op.execute("""
28+
UPDATE {table_name} AS t
29+
LEFT JOIN relationships AS r ON
30+
r.source_type = '{model_name}' AND
31+
r.source_id = t.id AND
32+
r.destination_type = 'Audit'
33+
LEFT JOIN audits as a ON
34+
a.id = r.destination_id
35+
SET
36+
t.audit_id = a.id
37+
WHERE
38+
t.audit_id is null
39+
""".format(
40+
table_name=table_name,
41+
model_name=model_name,
42+
))
43+
44+
op.execute("""
45+
UPDATE {table_name} AS t
46+
LEFT JOIN relationships AS r ON
47+
r.destination_type = '{model_name}' AND
48+
r.destination_id = t.id AND
49+
r.source_type = 'Audit'
50+
LEFT JOIN audits as a ON
51+
a.id = r.source_id
52+
SET
53+
t.audit_id = a.id
54+
WHERE
55+
t.audit_id is null
56+
""".format(
57+
table_name=table_name,
58+
model_name=model_name,
59+
))
60+
61+
op.create_foreign_key(
62+
"fk_{}_audits".format(table_name),
63+
table_name,
64+
"audits",
65+
["audit_id"],
66+
["id"],
67+
ondelete="RESTRICT"
68+
)
69+
70+
71+
def upgrade():
72+
"""Upgrade database schema and/or data, creating a new revision."""
73+
import ipdb
74+
ipdb.set_trace()
75+
upgrade_table("assessments", "Assessment")
76+
upgrade_table("issues", "Issue")
77+
print 5
78+
79+
80+
def downgrade():
81+
"""Downgrade database schema and/or data back to the previous revision."""

0 commit comments

Comments
 (0)