Просмотр исходного кода

fix(db): only perform foreign key checking when a migration was applied (#10397)

Tommy van der Vorst 3 месяцев назад
Родитель
Сommit
a64c5396e9
1 измененных файлов с 7 добавлено и 5 удалено
  1. 7 5
      internal/db/sqlite/basedb.go

+ 7 - 5
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 {