-
Notifications
You must be signed in to change notification settings - Fork 1
/
migration.py
118 lines (105 loc) · 3.91 KB
/
migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from db_helper import db_helper
from organisation.organisation import OrganisationWithDetails, TypeOfOrganisationEnum
from organisation import db as db
from user import db as user_db
from user.user_account import UserAccount, PrivilegesEnum
# This script migrates the dashboard deelmobiliteit from the old user centric
# acl structure to the new organisation centric acl structure.
def query_rows(stmt):
with db_helper.get_resource() as (cur, conn):
try:
cur.execute(stmt)
return cur.fetchall()
except Exception as e:
print(e)
conn.rollback()
return False
stmt = """
SELECT organisation_id
FROM organisation
WHERE name = 'CROW';
"""
admin_organisation = query_rows(stmt=stmt)
if len(admin_organisation) != 1:
print("CROW organisation doesn't exists")
exit()
admin_organisation_id = admin_organisation[0]["organisation_id"]
# 1. Create organisations.
# a. operators
stmt = """
SELECT DISTINCT(system_id) as system_id FROM feeds;
"""
operators = query_rows(stmt)
operator_look_up = {}
for operator in operators:
new_organisation = OrganisationWithDetails(
name=operator["system_id"].capitalize(),
type_of_organisation=TypeOfOrganisationEnum.operator,
data_owner_of_operators=[operator["system_id"]]
)
new_organisation = db.create_organisation(organisation=new_organisation)
db.create_historical_organisation_detail(organisation=new_organisation)
operator_look_up[operator["system_id"]] = new_organisation.organisation_id
# b. municipalities
stmt = """
SELECT municipality as gmcode, name
FROM zones
WHERE municipality IN
(SELECT DISTINCT(municipality)
FROM acl_municipalities)
AND zone_type = 'municipality';
"""
municipalities = query_rows(stmt)
municipality_look_up = {}
for municipality in municipalities:
new_organisation = OrganisationWithDetails(
name=municipality["name"],
type_of_organisation=TypeOfOrganisationEnum.municipality,
data_owner_of_municipalities=[municipality["gmcode"]]
)
new_organisation = db.create_organisation(organisation=new_organisation)
db.create_historical_organisation_detail(organisation=new_organisation)
municipality_look_up[municipality["gmcode"]] = new_organisation.organisation_id
# c. create organisation for users where it's unclear to what organisation they belong.
new_organisation = OrganisationWithDetails(
name="Onbekend",
type_of_organisation=TypeOfOrganisationEnum.other_company
)
new_organisation = db.create_organisation(organisation=new_organisation)
db.create_historical_organisation_detail(organisation=new_organisation)
unknown_organisation_id = new_organisation.organisation_id
# 2. Import users
stmt = """
SELECT username, is_admin, is_contact_person_municipality,
(
SELECT array_agg(municipality)
FROM acl_municipalities
WHERE username = t1.username
) as municipalities,
(
SELECT array_agg(operator)
FROM acl_operator
WHERE username = t1.username
) as operators
FROM acl as t1;
"""
users = query_rows(stmt)
for user in users:
organisation_id = unknown_organisation_id
priviliges = []
if user["is_admin"]:
organisation_id = admin_organisation_id
elif user["municipalities"] and len(user["municipalities"]) == 1:
municipality = user["municipalities"][0]
organisation_id = municipality_look_up[municipality]
elif user["operators"] and len(user["operators"]) == 1:
operator = user["operators"][0]
organisation_id = operator_look_up[operator]
if user["is_contact_person_municipality"]:
priviliges = [PrivilegesEnum.core_group, PrivilegesEnum.microhub_edit, PrivilegesEnum.download_raw_data, PrivilegesEnum.organisation_admin]
new_user = UserAccount(
user_id=user["username"],
privileges=priviliges,
organisation_id=organisation_id
)
user_db.create_user(new_user)