Passed
Push — develop ( 1f6ef3...42aeec )
by Koen
01:25
created

atramhasis.scripts.delete_scheme   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 70
dl 0
loc 118
rs 10
c 0
b 0
f 0
1
import argparse
2
import logging
3
from builtins import input
4
5
from pyramid.paster import get_appsettings
6
from pyramid.paster import setup_logging
7
from pytz import timezone
8
from sqlalchemy import engine_from_config
9
from sqlalchemy.sql.expression import text
10
11
timezone_brussels = timezone('Europe/Brussels')
12
log = logging.getLogger(__name__)
13
14
15
def delete_scheme(settings, scheme_id):
16
    engine = engine_from_config(settings, 'sqlalchemy.')
17
    with engine.begin() as con:
18
        concept_ids = con.execute(
19
            text(f'select id from concept where conceptscheme_id={scheme_id}')
20
        )
21
        for row in concept_ids:
22
            concept_id = row[0]
23
            delete_concept(concept_id, con)
24
25
        con.execute(
26
            text(
27
                f'delete from note where note.id in'
28
                f'(select note_id from conceptscheme_note '
29
                f'where conceptscheme_id={scheme_id})'
30
            )
31
        )
32
        con.execute(
33
            text(
34
                f'delete from source where source.id in'
35
                f'(select source_id from conceptscheme_source '
36
                f'where conceptscheme_id={scheme_id})'
37
            )
38
        )
39
        con.execute(
40
            text(
41
                f'delete from label where label.id in'
42
                f'(select label_id from conceptscheme_label '
43
                f'where conceptscheme_id={scheme_id})'
44
            )
45
        )
46
        con.execute(text(f'delete from conceptscheme where id = {scheme_id}'))
47
48
49
def delete_concept(concept_id, con):
50
    con.execute(
51
        text(
52
            f'delete from note where note.id in'
53
            f'(select note_id from concept_note where concept_id={concept_id})'
54
        )
55
    )
56
    con.execute(
57
        text(
58
            f'delete from source where source.id in'
59
            f'(select source_id from concept_source '
60
            f'where concept_id={concept_id})'
61
        )
62
    )
63
    con.execute(
64
        text(
65
            f'delete from label where label.id in'
66
            f'(select label_id from concept_label '
67
            f'where concept_id={concept_id})'
68
        )
69
    )
70
    delete_child_concepts(concept_id, con)
71
    con.execute(text(f'delete from concept where id = {concept_id}'))
72
73
74
def delete_child_concepts(concept_id, con):
75
    select_children = (
76
        text(
77
            f'select collection_id_narrower as child '
78
            f'from concept_hierarchy_collection '
79
            f'where concept_id_broader = {concept_id} '
80
            f'union '
81
            f'select concept_id_narrower as child '
82
            f'from concept_hierarchy_concept '
83
            f'where concept_id_broader = {concept_id}'
84
        )
85
    )
86
    children = con.execute(select_children)
87
    for row in children:
88
        child_id = row[0]
89
        delete_concept(child_id, con)
90
91
92
def main():
93
    parser = argparse.ArgumentParser(
94
        description="Delete a conceptscheme. ",
95
        usage="remove_schema development.ini --id=1")
96
    parser.add_argument('settings_file',
97
                        help="<The location of the settings file>#<app-name>")
98
    parser.add_argument("--id", type=int, required=True,
99
                        help="the conceptscheme id")
100
    parser.add_argument("--no-input", action='store_true',
101
                        help="Don't stop script for user input")
102
    args = parser.parse_args()
103
104
    config_uri = args.settings_file
105
    setup_logging(config_uri)
106
    settings = get_appsettings(config_uri)
107
    print(
108
        f"The conceptscheme with id {args.id} will be deleted"
109
    )
110
    if not args.no_input:
111
        input("Press [Enter] to continue.")
112
113
    delete_scheme(settings, args.id)
114
115
116
if __name__ == '__main__':  # pragma: no cover
117
    main()
118