diff --git a/backend/sqlite_to_postgres.py b/backend/sqlite_to_postgres.py index db1d308..7a230d9 100644 --- a/backend/sqlite_to_postgres.py +++ b/backend/sqlite_to_postgres.py @@ -149,12 +149,18 @@ def migrate(sqlite_path: Path, media_src: Path, media_dest: Path, replace: bool) stats: dict[str, tuple[int, int]] = {} try: existing = pg.execute("SELECT COUNT(*) FROM profiles").fetchone()[0] + seeds = pg.execute("SELECT COUNT(*) FROM tiers").fetchone()[0] if existing and not replace: raise SystemExit( f"PostgreSQL already has {existing} profiles. Pass --replace after a probe copy, not on the only backup." ) - if existing and replace: - print(f"Replacing {existing} existing Postgres profiles") + if seeds and not replace: + raise SystemExit( + "PostgreSQL already has platform seeds from startup. " + "Pass --replace to load a SQLite snapshot onto this instance (no user profiles yet is OK)." + ) + if replace: + print(f"Replacing Postgres snapshot (profiles={existing}, seed_tiers={seeds})") for table in reversed(TABLES): pg.execute(f"TRUNCATE TABLE {table} CASCADE") @@ -179,6 +185,10 @@ def migrate(sqlite_path: Path, media_src: Path, media_dest: Path, replace: bool) count = pg.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0] print(f" {table}: {len(rows)} → {count}") stats[table] = (len(rows), int(count)) + pg.execute( + "INSERT INTO schema_migrations (id) VALUES (%s) ON CONFLICT (id) DO NOTHING", + ("022_postgres_runtime_baseline",), + ) pg.commit() except Exception: pg.rollback() @@ -195,6 +205,9 @@ def verify(stats: dict[str, tuple[int, int]]) -> None: failed = False print("\nVerification") for table, (src, dest) in stats.items(): + if table == "schema_migrations" and dest == src + 1: + print(f" ok {table:36} sqlite={src:5} postgres={dest:5} (incl. 022)") + continue mark = "ok" if src == dest else "MISMATCH" if src != dest: failed = True