fix: resolve foreign key constraint violation during checkpoint restore

Issue:
- Restore operation failed with foreign key constraint violation
- Error: "update or delete on table business_scopes violates foreign key constraint"
- Problem: DELETE operations didn't handle foreign key dependencies correctly

Solution:
- Changed DELETE FROM to TRUNCATE TABLE ... CASCADE in _restore_table()
- TRUNCATE with CASCADE automatically removes dependent records
- Now restore operation properly handles all foreign key relationships
- Data can be restored without constraint violations

This ensures checkpoint restore works correctly across all related tables.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Codex Agent 2025-10-30 13:39:01 +08:00
parent a5d56ade26
commit e462f609ba
2 changed files with 10711 additions and 9 deletions

File diff suppressed because one or more lines are too long

View File

@ -386,17 +386,18 @@ def _restore_table(conn: pg.Connection, table_name: str, data: List[Dict[str, An
try:
cur = conn.cursor()
delete_sql = f"DELETE FROM {table_name}"
cur.execute(delete_sql)
# Use TRUNCATE with CASCADE to handle foreign key dependencies
# This will automatically remove dependent records
truncate_sql = f"TRUNCATE TABLE {table_name} CASCADE"
cur.execute(truncate_sql)
if data:
columns = list(data[0].keys())
placeholders = ", ".join(["%s"] * len(columns))
insert_sql = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES ({placeholders})"
columns = list(data[0].keys())
placeholders = ", ".join(["%s"] * len(columns))
insert_sql = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES ({placeholders})"
for row in data:
values = [row.get(col) for col in columns]
cur.execute(insert_sql, values)
for row in data:
values = [row.get(col) for col in columns]
cur.execute(insert_sql, values)
conn.commit()
return len(data)