fix: reset SERIAL sequences after SQLite migration to prevent UniqueViolation
Migration script now resets SERIAL sequences (audit_log, context_files, etc.) to max(existing_id) after copying rows. Without this, the sequence is still at its post-schema-creation value and every INSERT hits a duplicate key error.
This commit is contained in:
@@ -179,6 +179,23 @@ def migrate():
|
||||
print(f" context_files: {fts_cf} entries")
|
||||
print(f" user_profiles: {fts_up} entries")
|
||||
|
||||
# Step 4: Reset SERIAL sequences to max(existing_id) + 1
|
||||
print()
|
||||
print("Resetting SERIAL sequences...")
|
||||
for table, col in [
|
||||
("audit_log", "entry_id"),
|
||||
("context_files", "file_id"),
|
||||
("project_permissions", "id"),
|
||||
("reviews", "review_id"),
|
||||
("workspace_files", "file_id"),
|
||||
]:
|
||||
try:
|
||||
pconn.execute(f"SELECT setval(pg_get_serial_sequence('{table}', '{col}'), COALESCE((SELECT MAX({col}) FROM {table}), 1))")
|
||||
print(f" {table}.{col}: seq reset")
|
||||
except Exception as e:
|
||||
print(f" {table}.{col}: skipped ({e})")
|
||||
pconn.commit()
|
||||
|
||||
print()
|
||||
print("=" * 60)
|
||||
print(f"Migration complete! {total_rows} total rows migrated.")
|
||||
|
||||
Reference in New Issue
Block a user