Skip to content

Commit 97b75f0

Browse files
committed
Always close the database whether there is error when initializing
1 parent 62f83a0 commit 97b75f0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/sqlrunner.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ func initialize(schema string) (filename string, err error) {
254254
if err != nil {
255255
return "", fmt.Errorf("open sqlite: %w", err)
256256
}
257+
defer func() {
258+
if err := drv.Close(); err != nil {
259+
slog.Warn("close sqlite", slog.Any("error", err))
260+
}
261+
}()
257262

258263
if _, err := drv.Exec("PRAGMA foreign_keys = ON;"); err != nil {
259264
return "", fmt.Errorf("enable foreign keys: %w", err)
@@ -263,10 +268,6 @@ func initialize(schema string) (filename string, err error) {
263268
return "", NewSchemaError(err)
264269
}
265270

266-
if err := drv.Close(); err != nil {
267-
return "", fmt.Errorf("close sqlite: %w", err)
268-
}
269-
270271
// Rename the file to the final name
271272
if err := os.Rename(schemaFilename+".tmp", schemaFilename); err != nil {
272273
return "", fmt.Errorf("persistent schema: %w", err)

0 commit comments

Comments
 (0)