From a64c5396e95a5a2f3efe2477423a30165570a079 Mon Sep 17 00:00:00 2001 From: Tommy van der Vorst Date: Thu, 18 Sep 2025 14:22:35 +0200 Subject: [PATCH] fix(db): only perform foreign key checking when a migration was applied (#10397) --- internal/db/sqlite/basedb.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/db/sqlite/basedb.go b/internal/db/sqlite/basedb.go index b6c32de8b..2058fde88 100644 --- a/internal/db/sqlite/basedb.go +++ b/internal/db/sqlite/basedb.go @@ -125,7 +125,7 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript } ver, _ := db.getAppliedSchemaVersion(tx) - shouldVacuum := false + appliedMigrations := false if ver.SchemaVersion > 0 { filter := func(scr string) bool { scr = filepath.Base(scr) @@ -139,7 +139,7 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript } if int(n) > ver.SchemaVersion { slog.Info("Applying database migration", slogutil.FilePath(db.baseName), slog.String("script", scr)) - shouldVacuum = true + appliedMigrations = true return true } return false @@ -162,8 +162,10 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript } // Finally, ensure nothing we've done along the way has violated key integrity. - if _, err := conn.ExecContext(ctx, "PRAGMA foreign_key_check"); err != nil { - return nil, wrap(err) + if appliedMigrations { + if _, err := conn.ExecContext(ctx, "PRAGMA foreign_key_check"); err != nil { + return nil, wrap(err) + } } } @@ -176,7 +178,7 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript return nil, wrap(err) } - if shouldVacuum { + if appliedMigrations { // We applied migrations and should take the opportunity to vaccuum // the database. if err := db.vacuumAndOptimize(); err != nil {