Skip to content

Commit 096a44e

Browse files
author
Adriano Sanges
committed
Refactor database initialization: rename create_tables to init_db, add initialization check, and create _initialized table to prevent re-execution.
1 parent 66a1184 commit 096a44e

File tree

2 files changed

+65
-59
lines changed

2 files changed

+65
-59
lines changed

init_db.py

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,66 @@
22

33
import os
44

5-
def create_tables(conn):
6-
dirname = os.path.dirname(os.path.abspath(__file__))
7-
filename = os.path.join(dirname, 'data', 'QI_1106117_1_20241_VALORI.csv')
8-
filename_luoghi = os.path.join(dirname, 'data', 'QI_1106117_1_20241_ZONE.csv')
9-
create_prezzi_statement = f"CREATE OR REPLACE TABLE prezzi AS select * from '{filename}'"
10-
create_luoghi_statement = f"CREATE OR REPLACE TABLE luoghi AS select * from '{filename_luoghi}'"
11-
create_joined_data_statement = """
12-
CREATE OR REPLACE TABLE joined_data AS
13-
SELECT
14-
p.Area_territoriale,
15-
p.Regione,
16-
p.Prov,
17-
p.Comune_ISTAT,
18-
p.Comune_cat,
19-
p.Sez,
20-
p.Comune_amm,
21-
p.Comune_descrizione,
22-
p.Fascia,
23-
p.Zona,
24-
p.LinkZona,
25-
p.Cod_Tip,
26-
p.Descr_Tipologia,
27-
p.Stato,
28-
p.Stato_prev,
29-
p.Compr_min,
30-
p.Compr_max,
31-
p.Sup_NL_compr,
32-
p.Loc_min,
33-
p.Loc_max,
34-
p.Sup_NL_loc,
35-
p.column21,
36-
l.Zona_Descr,
37-
l.Cod_tip_prev,
38-
l.Descr_tip_prev,
39-
l.Stato_prev AS Stato_prev_luoghi,
40-
l.Microzona,
41-
l.column16
42-
FROM
43-
prezzi AS p
44-
LEFT JOIN
45-
luoghi AS l
46-
ON
47-
p.Area_territoriale = l.Area_territoriale AND
48-
p.Regione = l.Regione AND
49-
p.Prov = l.Prov AND
50-
p.Comune_ISTAT = l.Comune_ISTAT AND
51-
p.Comune_cat = l.Comune_cat AND
52-
p.Sez = l.Sez AND
53-
p.Comune_amm = l.Comune_amm AND
54-
p.Comune_descrizione = l.Comune_descrizione AND
55-
p.Fascia = l.Fascia AND
56-
p.Zona = l.Zona AND
57-
p.LinkZona = l.LinkZona;
58-
"""
59-
conn.sql(create_prezzi_statement)
60-
conn.sql(create_luoghi_statement)
61-
conn.sql(create_joined_data_statement)
5+
def init_db(conn):
6+
try:
7+
conn.execute("SELECT 1 FROM _initialized LIMIT 1")
8+
except:
9+
dirname = os.path.dirname(os.path.abspath(__file__))
10+
filename = os.path.join(dirname, 'data', 'QI_1106117_1_20241_VALORI.csv')
11+
filename_luoghi = os.path.join(dirname, 'data', 'QI_1106117_1_20241_ZONE.csv')
12+
create_prezzi_statement = f"CREATE OR REPLACE TABLE prezzi AS select * from '{filename}'"
13+
create_luoghi_statement = f"CREATE OR REPLACE TABLE luoghi AS select * from '{filename_luoghi}'"
14+
create_joined_data_statement = """
15+
CREATE OR REPLACE TABLE joined_data AS
16+
SELECT
17+
p.Area_territoriale,
18+
p.Regione,
19+
p.Prov,
20+
p.Comune_ISTAT,
21+
p.Comune_cat,
22+
p.Sez,
23+
p.Comune_amm,
24+
p.Comune_descrizione,
25+
p.Fascia,
26+
p.Zona,
27+
p.LinkZona,
28+
p.Cod_Tip,
29+
p.Descr_Tipologia,
30+
p.Stato,
31+
p.Stato_prev,
32+
p.Compr_min,
33+
p.Compr_max,
34+
p.Sup_NL_compr,
35+
p.Loc_min,
36+
p.Loc_max,
37+
p.Sup_NL_loc,
38+
p.column21,
39+
l.Zona_Descr,
40+
l.Cod_tip_prev,
41+
l.Descr_tip_prev,
42+
l.Stato_prev AS Stato_prev_luoghi,
43+
l.Microzona,
44+
l.column16
45+
FROM
46+
prezzi AS p
47+
LEFT JOIN
48+
luoghi AS l
49+
ON
50+
p.Area_territoriale = l.Area_territoriale AND
51+
p.Regione = l.Regione AND
52+
p.Prov = l.Prov AND
53+
p.Comune_ISTAT = l.Comune_ISTAT AND
54+
p.Comune_cat = l.Comune_cat AND
55+
p.Sez = l.Sez AND
56+
p.Comune_amm = l.Comune_amm AND
57+
p.Comune_descrizione = l.Comune_descrizione AND
58+
p.Fascia = l.Fascia AND
59+
p.Zona = l.Zona AND
60+
p.LinkZona = l.LinkZona;
61+
"""
62+
conn.sql(create_prezzi_statement)
63+
conn.sql(create_luoghi_statement)
64+
conn.sql(create_joined_data_statement)
65+
conn.execute("CREATE TABLE _initialized (initialized_at TIMESTAMP);")
66+
conn.execute("INSERT INTO _initialized VALUES (CURRENT_TIMESTAMP);")
67+

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import duckdb
44
import streamlit as st
5-
from init_db import create_tables
5+
from init_db import init_db
66

77
@st.cache_data(hash_funcs={duckdb.DuckDBPyConnection: id})
88
def get_regioni(conn):
@@ -19,7 +19,7 @@ def main():
1919
"""
2020
try:
2121
conn = duckdb.connect(database = "dati-immobiliari.duckdb")
22-
create_tables(conn)
22+
init_db(conn)
2323

2424
st.set_page_config(layout="wide")
2525
st.title("Dati immobiliari")

0 commit comments

Comments
 (0)