From 36bb4d2b752aab6a2d55cf44142015c771eed8b2 Mon Sep 17 00:00:00 2001 From: Michele Date: Wed, 1 Apr 2026 17:13:33 +0200 Subject: [PATCH] =?UTF-8?q?fix(migrate):=20cast=20SQLite=20bool=200/1=20?= =?UTF-8?q?=E2=86=92=20PostgreSQL=20bool,=20rollback=20on=20row=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/migrate_sqlite_to_pg.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/migrate_sqlite_to_pg.py b/scripts/migrate_sqlite_to_pg.py index ec2346b..db7c1e6 100644 --- a/scripts/migrate_sqlite_to_pg.py +++ b/scripts/migrate_sqlite_to_pg.py @@ -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")