diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go index bd16f87..83dd2e7 100644 --- a/internal/database/sqlite.go +++ b/internal/database/sqlite.go @@ -34,6 +34,16 @@ func (db *DB) openSQLite() error { return errors.Wrap(err, "enable wal") } + // When Autobrr does not cleanly shutdown, the WAL will still be present and not committed. + // This is a no-op if the WAL is empty, and a commit when the WAL is not to start fresh. + // When commits hit 1000, PRAGMA wal_checkpoint(PASSIVE); is invoked which tries its best + // to commit from the WAL (and can fail to commit all pending operations). + // Forcing a PRAGMA wal_checkpoint(RESTART); in the future on a "quiet period" could be + // considered. + if _, err = db.handler.Exec(`PRAGMA wal_checkpoint(TRUNCATE);`); err != nil { + return errors.Wrap(err, "commit wal") + } + // Enable foreign key checks. For historical reasons, SQLite does not check // foreign key constraints by default. There's some overhead on inserts to // verify foreign key integrity, but it's definitely worth it.