Skip to content

Commit 8a330b6

Browse files
noerwzeripath
andauthored
Add setting SQLITE_JOURNAL_MODE to enable WAL (#20535)
Co-authored-by: Andrew Thornton <[email protected]>
1 parent ae3dde1 commit 8a330b6

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

custom/conf/app.example.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ USER = root
313313
;DB_TYPE = sqlite3
314314
;PATH= ; defaults to data/gitea.db
315315
;SQLITE_TIMEOUT = ; Query timeout defaults to: 500
316+
;SQLITE_JOURNAL_MODE = ; defaults to sqlite database default (often DELETE), can be used to enable WAL mode. https://www.sqlite.org/pragma.html#pragma_journal_mode
316317
;;
317318
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318319
;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
382382
- `verify-ca`: Enable TLS with verification of the database server certificate against its root certificate.
383383
- `verify-full`: Enable TLS and verify the database server name matches the given certificate in either the `Common Name` or `Subject Alternative Name` fields.
384384
- `SQLITE_TIMEOUT`: **500**: Query timeout for SQLite3 only.
385+
- `SQLITE_JOURNAL_MODE`: **""**: Change journal mode for SQlite3. Can be used to enable [WAL mode](https://www.sqlite.org/wal.html) when high load causes write congestion. See [SQlite3 docs](https://www.sqlite.org/pragma.html#pragma_journal_mode) for possible values. Defaults to the default for the database file, often DELETE.
385386
- `ITERATE_BUFFER_SIZE`: **50**: Internal buffer size for iterating.
386387
- `CHARSET`: **utf8mb4**: For MySQL only, either "utf8" or "utf8mb4". NOTICE: for "utf8mb4" you must use MySQL InnoDB > 5.6. Gitea is unable to check this.
387388
- `PATH`: **data/gitea.db**: For SQLite3 only, the database file path.

modules/setting/database.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
LogSQL bool
4040
Charset string
4141
Timeout int // seconds
42+
SQLiteJournalMode string
4243
UseSQLite3 bool
4344
UseMySQL bool
4445
UseMSSQL bool
@@ -91,6 +92,8 @@ func InitDBConfig() {
9192

9293
Database.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "gitea.db"))
9394
Database.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
95+
Database.SQLiteJournalMode = sec.Key("SQLITE_JOURNAL_MODE").MustString("")
96+
9497
Database.MaxIdleConns = sec.Key("MAX_IDLE_CONNS").MustInt(2)
9598
if Database.UseMySQL {
9699
Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFETIME").MustDuration(3 * time.Second)
@@ -136,7 +139,12 @@ func DBConnStr() (string, error) {
136139
if err := os.MkdirAll(path.Dir(Database.Path), os.ModePerm); err != nil {
137140
return "", fmt.Errorf("Failed to create directories: %v", err)
138141
}
139-
connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate", Database.Path, Database.Timeout)
142+
journalMode := ""
143+
if Database.SQLiteJournalMode != "" {
144+
journalMode = "&_journal_mode=" + Database.SQLiteJournalMode
145+
}
146+
connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate%s",
147+
Database.Path, Database.Timeout, journalMode)
140148
default:
141149
return "", fmt.Errorf("Unknown database type: %s", Database.Type)
142150
}

0 commit comments

Comments
 (0)