diff --git a/hscontrol/db/db.go b/hscontrol/db/db.go index 8f81774b9..b7c4bd3c7 100644 --- a/hscontrol/db/db.go +++ b/hscontrol/db/db.go @@ -522,8 +522,13 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) { // Build SQLite configuration with pragmas set at connection time sqliteConfig := sqliteconfig.Default(sqlitePath) if cfg.Sqlite.WriteAheadLog { - sqliteConfig.JournalMode = sqliteconfig.JournalModeWAL sqliteConfig.WALAutocheckpoint = cfg.Sqlite.WALAutoCheckPoint + } else { + // The journal mode persists in the file, so leaving it unset keeps + // a database an earlier run put in WAL mode there. FULL is SQLite's + // default for a rollback journal; NORMAL can corrupt it on power loss. + sqliteConfig.JournalMode = sqliteconfig.JournalModeDelete + sqliteConfig.Synchronous = sqliteconfig.SynchronousFull } connectionURL, err := sqliteConfig.ToURL() diff --git a/hscontrol/db/db_test.go b/hscontrol/db/db_test.go index f3ab078df..05bb96a16 100644 --- a/hscontrol/db/db_test.go +++ b/hscontrol/db/db_test.go @@ -1126,6 +1126,46 @@ func TestOpenDBReturnsOpenError(t *testing.T) { } } +// TestSQLiteWriteAheadLog switches one database between modes, because SQLite +// stores the journal mode in the file and turning WAL off has to undo it. +func TestSQLiteWriteAheadLog(t *testing.T) { + path := filepath.Join(t.TempDir(), "headscale.db") + + open := func(wal bool) (string, int) { + t.Helper() + + cfg := sqliteTestConfig(path) + cfg.Database.Sqlite.WriteAheadLog = wal + + hsdb, err := NewHeadscaleDatabase(cfg) + require.NoError(t, err) + + var ( + mode string + synchronous int + ) + + require.NoError(t, hsdb.DB.Raw("PRAGMA journal_mode").Scan(&mode).Error) + require.NoError(t, hsdb.DB.Raw("PRAGMA synchronous").Scan(&synchronous).Error) + require.NoError(t, hsdb.Close()) + + return mode, synchronous + } + + const normal, full = 1, 2 + + mode, synchronous := open(true) + assert.Equal(t, "wal", mode) + assert.Equal(t, normal, synchronous) + + mode, synchronous = open(false) + assert.Equal(t, "delete", mode) + assert.Equal(t, full, synchronous) + + mode, _ = open(true) + assert.Equal(t, "wal", mode) +} + func TestLastControlAddressMigration(t *testing.T) { dbPath := t.TempDir() + "/headscale.db" cfg := &types.Config{ diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 14a36b062..8b34d65aa 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -483,10 +483,11 @@ type SqliteConfig struct { // `:memory:` keeps the database in memory. Path string `example:"\"/opt/headscale/db.sqlite\"" koanf:"path" nix:"external"` - // WriteAheadLog applies `database.sqlite.wal_autocheckpoint` and - // compacts the database (`VACUUM`) on shutdown. SQLite runs in WAL - // mode either way; false keeps SQLite's default checkpoint of 1000 - // frames. + // WriteAheadLog runs SQLite in write-ahead log (WAL) mode, applies + // `database.sqlite.wal_autocheckpoint` and compacts the database + // (`VACUUM`) on shutdown. False uses SQLite's rollback journal + // (`DELETE`) with `synchronous=FULL`, and converts a database left + // in WAL mode when headscale opens it. WriteAheadLog bool `koanf:"write_ahead_log"` // WALAutoCheckPoint is the number of WAL frames after which the WAL