Skip to content

Commit 2489c2e

Browse files
committed
gnrcloudtools and gnrsubscriptions after splitting dependent repos #6
1 parent df7419a commit 2489c2e

8 files changed

Lines changed: 548 additions & 11 deletions

File tree

instances/gnr_comm/config/instanceconfig.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
<gnrcommunication_wordpress pkgcode="gnrcommunication:wordpress"/>
1818
<gnrcommunication_dem pkgcode="gnrcommunication:dem"/>
1919
<gnrauth_oidc pkgcode="gnrauth:oidc"/>
20-
<community_comm pkgcode="community:comm"/>
21-
<repomgm_repomgm pkgcode="repomgm:repomgm"/>
20+
<gnrcloudtools_github pkgcode="gnrcloudtools:github"/>
21+
<gnrsubscriptions_sbs pkgcode="gnrsubscriptions:sbs"/>
22+
<community_comm pkgcode="community:comm"/>
2223
</packages>
2324

2425
<authentication pkg="adm">
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# encoding: utf-8
2+
"""Migrazione delle tabelle `repomgm` verso i nuovi package `github` e `sbs`.
3+
4+
Nota: le tabelle sorgente potrebbero non essere più nel model o avere nomi
5+
prefissati (es. public.repomgm_repository). Le individuiamo direttamente dal DB.
6+
"""
7+
8+
9+
def table_exists(db, full_name):
10+
"""
11+
True se la tabella esiste in DB (anche se non è più nel model),
12+
False altrimenti.
13+
"""
14+
res = db.execute("SELECT to_regclass(:tname)", dict(tname=full_name)).fetchone()
15+
return bool(res and res[0])
16+
17+
18+
def find_table(db, preferred_names, fallback_name=None):
19+
"""
20+
Restituisce il primo nome tabella esistente tra quelli preferiti.
21+
Se non trova nulla, prova a cercare per table_name (fallback_name).
22+
"""
23+
for name in preferred_names:
24+
if table_exists(db, name):
25+
return name
26+
27+
if not fallback_name:
28+
return None
29+
30+
found = db.execute(
31+
"""
32+
SELECT table_schema || '.' || table_name
33+
FROM information_schema.tables
34+
WHERE table_name=:tname
35+
ORDER BY table_schema
36+
LIMIT 1
37+
""",
38+
dict(tname=fallback_name),
39+
).fetchone()
40+
if found:
41+
return found[0]
42+
return None
43+
44+
45+
def fetch_rows(db, full_name):
46+
"""Legge tutte le righe dalla tabella sorgente come lista di dict."""
47+
cursor = db.execute(f"SELECT * FROM {full_name}")
48+
cols = [c[0] for c in cursor.description]
49+
return [dict(zip(cols, row)) for row in cursor.fetchall()]
50+
51+
52+
def migrate_table(db, source_candidates, dest_path, field_map, pkey_field="id"):
53+
"""
54+
Copia le righe dalla prima tabella trovata in `source_candidates` verso `dest_path`.
55+
56+
field_map è un dict {colonna_sorgente: colonna_destinazione}.
57+
Si inseriscono solo le righe che non esistono già (match sul pkey_field).
58+
"""
59+
source = find_table(
60+
db,
61+
preferred_names=source_candidates,
62+
fallback_name=source_candidates[-1].split(".")[-1],
63+
)
64+
if not source:
65+
print(
66+
f"Skip {source_candidates[0]}: tabella inesistente "
67+
f"(cercate varianti: {', '.join(source_candidates)})"
68+
)
69+
return 0
70+
71+
dest_tbl = db.table(dest_path)
72+
rows = fetch_rows(db, source)
73+
inserted = 0
74+
75+
for row in rows:
76+
pkey = row.get(pkey_field)
77+
if not pkey:
78+
continue
79+
80+
# evita duplicati se la riga è già stata migrata
81+
if dest_tbl.readColumns(pkey, columns=pkey_field):
82+
continue
83+
84+
record = dest_tbl.newrecord(
85+
**{dst: row[src] for src, dst in field_map.items() if src in row}
86+
)
87+
record[pkey_field] = pkey
88+
dest_tbl.insert(record)
89+
inserted += 1
90+
91+
db.commit()
92+
print(f"Migrate {source} -> {dest_path}: inserite {inserted} righe")
93+
return inserted
94+
95+
96+
def cleanup_repomgm(db, tables):
97+
"""Svuota le tabelle repomgm nell'ordine fornito."""
98+
for candidates in tables:
99+
source = find_table(
100+
db,
101+
preferred_names=candidates,
102+
fallback_name=candidates[-1].split(".")[-1],
103+
)
104+
if not source:
105+
continue
106+
db.execute(f"DELETE FROM {source}")
107+
db.commit()
108+
109+
110+
def main(db):
111+
migrations = [
112+
(
113+
[
114+
"repomgm.git_organization",
115+
"repomgm.repomgm_git_organization",
116+
"public.repomgm_git_organization",
117+
"public.repomgm.repomgm_git_organization",
118+
],
119+
"github.organization",
120+
{"id": "id", "code": "code", "description": "description"},
121+
),
122+
(
123+
[
124+
"repomgm.repository",
125+
"repomgm.repomgm_repository",
126+
"public.repomgm_repository",
127+
"public.repomgm.repomgm_repository",
128+
],
129+
"github.repository",
130+
{
131+
"id": "id",
132+
"code": "code",
133+
"title": "title",
134+
"logo": "logo",
135+
"description": "description",
136+
"organization_id": "organization_id",
137+
"metadata": "metadata",
138+
},
139+
),
140+
(
141+
[
142+
"repomgm.frequency",
143+
"repomgm.repomgm_frequency",
144+
"public.repomgm_frequency",
145+
"public.repomgm.repomgm_frequency",
146+
],
147+
"sbs.frequency",
148+
{
149+
"code": "code",
150+
"description": "description",
151+
"days_for_renewal": "days_for_renewal",
152+
},
153+
"code",
154+
),
155+
(
156+
[
157+
"repomgm.subscription_plan",
158+
"repomgm.repomgm_subscription_plan",
159+
"public.repomgm_subscription_plan",
160+
"public.repomgm.repomgm_subscription_plan",
161+
],
162+
"sbs.subscription_plan",
163+
{
164+
"id": "id",
165+
"repository_id": "repository_id",
166+
"description": "description",
167+
"full_description": "full_description",
168+
"price": "price",
169+
"currency": "currency",
170+
"frequency_code": "frequency_code",
171+
"start_date": "start_date",
172+
"end_date": "end_date",
173+
"enable_subscription": "enable_subscription",
174+
},
175+
),
176+
(
177+
[
178+
"repomgm.subscription",
179+
"repomgm.repomgm_subscription",
180+
"public.repomgm_subscription",
181+
"public.repomgm.repomgm_subscription",
182+
],
183+
"sbs.subscription",
184+
{
185+
"id": "id",
186+
"user_id": "user_id",
187+
"repository_id": "repository_id",
188+
"subscription_plan_id": "subscription_plan_id",
189+
"role_id": "role_id",
190+
"start_date": "start_date",
191+
"end_date": "end_date",
192+
"permission_level": "permission_level",
193+
},
194+
),
195+
]
196+
197+
for source_candidates, dest, field_map, *pkey in migrations:
198+
migrate_table(db, source_candidates, dest, field_map, pkey[0] if pkey else "id")
199+
200+
cleanup_repomgm(
201+
db,
202+
[
203+
[
204+
"repomgm.subscription",
205+
"repomgm.repomgm_subscription",
206+
"public.repomgm_subscription",
207+
"public.repomgm.repomgm_subscription",
208+
],
209+
[
210+
"repomgm.subscription_plan",
211+
"repomgm.repomgm_subscription_plan",
212+
"public.repomgm_subscription_plan",
213+
"public.repomgm.repomgm_subscription_plan",
214+
],
215+
[
216+
"repomgm.repository",
217+
"repomgm.repomgm_repository",
218+
"public.repomgm_repository",
219+
"public.repomgm.repomgm_repository",
220+
],
221+
[
222+
"repomgm.git_organization",
223+
"repomgm.repomgm_git_organization",
224+
"public.repomgm_git_organization",
225+
"public.repomgm.repomgm_git_organization",
226+
],
227+
[
228+
"repomgm.frequency",
229+
"repomgm.repomgm_frequency",
230+
"public.repomgm_frequency",
231+
"public.repomgm.repomgm_frequency",
232+
],
233+
],
234+
)

0 commit comments

Comments
 (0)