Ensure all PG tables on init; fix migration commits per table.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user