Ensure all PG tables on init; fix migration commits per table.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-01 08:22:54 +08:00
parent e418c2dcec
commit 6abe06d935
2 changed files with 18 additions and 11 deletions
+9 -11
View File
@@ -38,14 +38,10 @@ def _pg_columns(pg_conn, table: str) -> list[str]:
def _reset_sequences(pg_conn, table: str, pk: str = "id") -> None:
try:
pg_conn.execute(
f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), "
f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)"
)
pg_conn.commit()
except Exception:
rollback_if_postgres(pg_conn)
pg_conn.execute(
f"SELECT setval(pg_get_serial_sequence('{table}', '{pk}'), "
f"COALESCE((SELECT MAX({pk}) FROM {table}), 1), true)"
)
def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
@@ -94,9 +90,11 @@ def migrate(*, sqlite_path: str | None = None, dry_run: bool = False) -> dict:
for row in rows:
dst.execute(insert_sql, tuple(row[c] for c in cols))
if "id" in cols:
_reset_sequences(dst, table, "id")
else:
dst.commit()
try:
_reset_sequences(dst, table, "id")
except Exception:
rollback_if_postgres(dst)
dst.commit()
stats[table] = len(rows)
print(f" {table}: {len(rows)}")
except Exception as exc: