From 14e2d4954a9df20ecef6ec18f10139f3fd26dd9e Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Thu, 4 Jun 2026 16:57:09 +0200 Subject: [PATCH] fix(migrate-db): drop legacy client_traffics FK before Postgres copy (#4882) AutoMigrate re-creates the client_traffics -> inbounds foreign key, but the running panel drops it and tolerates client_traffics rows whose inbound was deleted. Migrating a DB with such orphaned rows failed with an fk_inbounds_client_stats violation. Drop the constraint on the destination right after AutoMigrate so the copy matches runtime behavior. --- database/migrate_data.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/migrate_data.go b/database/migrate_data.go index d4e6cdec..89c8387c 100644 --- a/database/migrate_data.go +++ b/database/migrate_data.go @@ -86,6 +86,14 @@ func MigrateData(srcPath, dstDSN string) error { } } + // AutoMigrate re-creates the legacy client_traffics -> inbounds foreign key, + // but the running panel drops it (see dropLegacyForeignKeys) and tolerates + // client_traffics rows whose inbound was deleted. Drop it here too so copying + // such orphaned rows can't fail with an fk_inbounds_client_stats violation. + if err := dst.Exec("ALTER TABLE client_traffics DROP CONSTRAINT IF EXISTS fk_inbounds_client_stats").Error; err != nil { + return fmt.Errorf("drop legacy foreign key: %w", err) + } + // Empty the destination tables so the migration is idempotent: a fresh // PostgreSQL DB already holds an auto-seeded admin (id=1) from any prior // panel start, and a partially-failed earlier run leaves rows behind. Either