mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 11:57:07 +09:00
db: turn off WAL when database.sqlite.write_ahead_log is false
Since 0.27.0 false only skipped wal_autocheckpoint. Now DELETE journal, synchronous=FULL; a database left in WAL mode is converted on open. Updates #1926
This commit is contained in:
1 parent
5a5290c174
commit
b7995a311c
3 files changed
+51
-5
No files matched your search
+6
-1
@@ -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()
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user