diff --git a/config-example.yaml b/config-example.yaml index 2632555d..a22226a1 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -168,6 +168,11 @@ database: # https://www.sqlite.org/wal.html write_ahead_log: true + # Maximum number of WAL file frames before the WAL file is automatically checkpointed. + # https://www.sqlite.org/c3ref/wal_autocheckpoint.html + # Set to 0 to disable automatic checkpointing. + wal_autocheckpoint: 1000 + # # Postgres config # Please note that using Postgres is highly discouraged as it is only supported for legacy reasons. # See database.type for more information. diff --git a/hscontrol/db/db.go b/hscontrol/db/db.go index b7661ab2..65253650 100644 --- a/hscontrol/db/db.go +++ b/hscontrol/db/db.go @@ -543,10 +543,10 @@ func openDB(cfg types.DatabaseConfig) (*gorm.DB, error) { } if cfg.Sqlite.WriteAheadLog { - if err := db.Exec(` + if err := db.Exec(fmt.Sprintf(` PRAGMA journal_mode=WAL; - PRAGMA wal_autocheckpoint=0; - `).Error; err != nil { + PRAGMA wal_autocheckpoint=%d; + `, cfg.Sqlite.WALAutoCheckPoint)).Error; err != nil { return nil, fmt.Errorf("setting WAL mode: %w", err) } } diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 5895ebc9..3d6abff7 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -103,8 +103,9 @@ type Nameservers struct { } type SqliteConfig struct { - Path string - WriteAheadLog bool + Path string + WriteAheadLog bool + WALAutoCheckPoint int } type PostgresConfig struct { @@ -271,6 +272,7 @@ func LoadConfig(path string, isFile bool) error { viper.SetDefault("database.postgres.conn_max_idle_time_secs", 3600) viper.SetDefault("database.sqlite.write_ahead_log", true) + viper.SetDefault("database.sqlite.wal_autocheckpoint", 1000) // SQLite default viper.SetDefault("oidc.scope", []string{oidc.ScopeOpenID, "profile", "email"}) viper.SetDefault("oidc.only_start_if_oidc_is_available", true) @@ -543,7 +545,8 @@ func databaseConfig() DatabaseConfig { Path: util.AbsolutePathFromConfigPath( viper.GetString("database.sqlite.path"), ), - WriteAheadLog: viper.GetBool("database.sqlite.write_ahead_log"), + WriteAheadLog: viper.GetBool("database.sqlite.write_ahead_log"), + WALAutoCheckPoint: viper.GetInt("database.sqlite.wal_autocheckpoint"), }, Postgres: PostgresConfig{ Host: viper.GetString("database.postgres.host"),