fix(migrate): cast SQLite bool 0/1 → PostgreSQL bool, rollback on row error
This commit is contained in:
@@ -72,6 +72,16 @@ def migrate(sqlite_path: str, pg_dsn: str):
|
||||
# Disabilita temporaneamente i trigger di FK per inserimento sicuro
|
||||
cur.execute("SET session_replication_role = replica;")
|
||||
|
||||
# Colonne booleane per tabella (SQLite le salva come 0/1)
|
||||
BOOL_COLS = {
|
||||
"users": {"is_admin"},
|
||||
"characters": {"is_active"},
|
||||
"affiliate_links": {"is_active"},
|
||||
"editorial_plans": {"is_active"},
|
||||
"social_accounts": {"is_active"},
|
||||
"scheduled_posts": set(),
|
||||
}
|
||||
|
||||
for table in TABLES_ORDER:
|
||||
rows = data.get(table, [])
|
||||
if not rows:
|
||||
@@ -82,14 +92,18 @@ def migrate(sqlite_path: str, pg_dsn: str):
|
||||
placeholders = ", ".join(["%s"] * len(cols))
|
||||
col_names = ", ".join(cols)
|
||||
sql = f"INSERT INTO {table} ({col_names}) VALUES ({placeholders}) ON CONFLICT DO NOTHING"
|
||||
bool_cols = BOOL_COLS.get(table, set())
|
||||
|
||||
inserted = 0
|
||||
for row in rows:
|
||||
values = []
|
||||
for col in cols:
|
||||
v = row[col]
|
||||
# Cast SQLite 0/1 → Python bool for PostgreSQL boolean columns
|
||||
if col in bool_cols and isinstance(v, int):
|
||||
v = bool(v)
|
||||
# SQLite stores JSON as string — psycopg2 needs Python objects for JSON columns
|
||||
if isinstance(v, str) and v and v[0] in ("{", "["):
|
||||
elif isinstance(v, str) and v and v[0] in ("{", "["):
|
||||
try:
|
||||
v = json.loads(v)
|
||||
v = psycopg2.extras.Json(v)
|
||||
@@ -100,6 +114,7 @@ def migrate(sqlite_path: str, pg_dsn: str):
|
||||
cur.execute(sql, values)
|
||||
inserted += 1
|
||||
except Exception as e:
|
||||
pg.rollback()
|
||||
print(f" WARN riga in {table}: {e} — saltata")
|
||||
|
||||
print(f" {table}: {inserted}/{len(rows)} righe inserite")
|
||||
|
||||
Reference in New Issue
Block a user