3e9675b35dfc_cascades   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 55
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A downgrade() 0 2 1
A _get_convention() 0 6 2
B upgrade() 0 46 3
1
"""cascades
2
3
Revision ID: 3e9675b35dfc
4
Revises: cb568ec81000
5
Create Date: 2020-08-12 14:48:35.592316
6
7
"""
8
from alembic import op
9
10
# revision identifiers, used by Alembic.
11
revision = "3e9675b35dfc"
12
down_revision = "cb568ec81000"
13
14
15
def _get_convention():
16
    is_sqlite = op.get_bind().dialect.name == "sqlite"
17
    return (
18
        {"fk": "FK_%(table_name)s_%(referred_table_name)s"}
19
        if is_sqlite
20
        else {"fk": "%(table_name)s_%(column_0_name)s_fkey"}
21
    )
22
23
24
def upgrade():
25
26
    args = [
27
        ("conceptscheme_source", "conceptscheme", "conceptscheme_id", "id"),
28
        ("conceptscheme_source", "source", "source_id", "id"),
29
        ("concept_source", "concept", "concept_id", "id"),
30
        ("concept_source", "source", "source_id", "id"),
31
        ("conceptscheme_language", "conceptscheme", "conceptscheme_id", "id"),
32
        ("match", "concept", "concept_id", "id"),
33
        ("label", "language", "language_id", "id"),
34
        ("note", "language", "language_id", "id"),
35
        ("collection_concept", "concept", "concept_id", "id"),
36
        ("collection_concept", "concept", "collection_id", "id"),
37
        ("conceptscheme_note", "conceptscheme", "conceptscheme_id", "id"),
38
        ("conceptscheme_label", "conceptscheme", "conceptscheme_id", "id"),
39
        ("visitation", "concept", "concept_id", "id"),
40
        ("visitation", "conceptscheme", "conceptscheme_id", "id"),
41
        ("concept_note", "concept", "concept_id", "id"),
42
        ("concept_note", "note", "note_id", "id"),
43
        ("concept_related_concept", "concept", "concept_id_from", "id"),
44
        ("concept_related_concept", "concept", "concept_id_to", "id"),
45
        ("concept_hierarchy_concept", "concept", "concept_id_narrower", "id"),
46
        ("concept_hierarchy_concept", "concept", "concept_id_broader", "id"),
47
        ("concept_hierarchy_collection", "concept", "concept_id_broader", "id"),
48
        ("concept_hierarchy_collection", "concept", "collection_id_narrower", "id"),
49
        ("concept_label", "concept", "concept_id", "id"),
50
        ("concept_label", "label", "label_id", "id"),
51
    ]
52
    convention = _get_convention()
53
54
    for source_table, referent_table, local_col, remote_col in args:
55
        with op.batch_alter_table(
56
            source_table, naming_convention=convention
57
        ) as batch_op:
58
            constraint_name = convention["fk"] % {
59
                "table_name": source_table,
60
                "referred_table_name": referent_table,
61
                "column_0_name": local_col,
62
            }
63
            batch_op.drop_constraint(constraint_name)
64
            batch_op.create_foreign_key(
65
                constraint_name,
66
                referent_table,
67
                [local_col],
68
                [remote_col],
69
                ondelete="cascade",
70
            )
71
72
73
def downgrade():
74
    pass
75