Skip to content

Commit 58c5ac1

Browse files
authored
Respect file scheme URIs for SQLite. (#387)
1 parent 6a8e827 commit 58c5ac1

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ opengist-home:
1717
# Secret key used for session store & encrypt MFA data on database. Default: <randomized 32 bytes>
1818
secret-key:
1919

20-
# URI of the database. Default: opengist.db (SQLite)
21-
# SQLite: file name
20+
# URI of the database. Default: opengist.db (SQLite) is placed in opengist-home
21+
# SQLite: file:/path/to/database
2222
# PostgreSQL: postgres://user:password@host:port/database
2323
# MySQL/MariaDB: mysql://user:password@host:port/database
2424
db-uri: opengist.db

docs/configuration/databases/sqlite.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ By default, Opengist uses SQLite as the database backend.
55
Because SQLite is a file-based database, there is not much configuration to tweak.
66

77
The configuration `db-uri`/`OG_DB_URI` refers to the path of the SQLite database file relative in the `$opengist-home/` directory (default `opengist.db`),
8-
although it can be left untouched.
8+
although it can be left untouched. You can also use an absolute path outside the `$opengist-home/` directory.
99

1010
The SQLite journal mode is set to [`WAL` (Write-Ahead Logging)](https://www.sqlite.org/pragma.html#pragma_journal_mode) by default and can be changed.
1111

@@ -14,6 +14,9 @@ The SQLite journal mode is set to [`WAL` (Write-Ahead Logging)](https://www.sqli
1414
# default
1515
db-uri: opengist.db
1616
sqlite.journal-mode: WAL
17+
18+
# absolute path outside the $opengist-home/ directory
19+
db-uri: file:/home/user/opengist.db
1720
```
1821
1922
#### Environment variable

internal/db/db.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,24 @@ var DatabaseInfo *databaseInfo
4646
func parseDBURI(uri string) (*databaseInfo, error) {
4747
info := &databaseInfo{}
4848

49-
if !strings.Contains(uri, "://") {
50-
info.Type = SQLite
51-
if uri == "file::memory:" {
52-
info.Database = "file::memory:"
53-
return info, nil
54-
}
55-
info.Database = filepath.Join(config.GetHomeDir(), uri)
56-
return info, nil
57-
}
5849
u, err := url.Parse(uri)
5950
if err != nil {
6051
return nil, fmt.Errorf("invalid URI: %v", err)
6152
}
6253

54+
if u.Scheme == "" {
55+
info.Type = SQLite
56+
info.Database = filepath.Join(config.GetHomeDir(), uri)
57+
return info, nil
58+
}
59+
6360
switch u.Scheme {
6461
case "postgres", "postgresql":
6562
info.Type = PostgreSQL
6663
case "mysql", "mariadb":
6764
info.Type = MySQL
65+
case "file":
66+
info.Type = SQLite
6867
default:
6968
return nil, fmt.Errorf("unknown database: %v", err)
7069
}
@@ -83,6 +82,8 @@ func parseDBURI(uri string) (*databaseInfo, error) {
8382
switch info.Type {
8483
case PostgreSQL, MySQL:
8584
info.Database = strings.TrimPrefix(u.Path, "/")
85+
case SQLite:
86+
info.Database = u.String()
8687
default:
8788
return nil, fmt.Errorf("unknown database: %v", err)
8889
}
@@ -190,12 +191,21 @@ func setupSQLite(dbInfo databaseInfo, sharedCache bool) error {
190191
log.Warn().Msg("Invalid SQLite journal mode: " + journalMode)
191192
}
192193

193-
sharedCacheStr := ""
194-
if sharedCache {
195-
sharedCacheStr = "&cache=shared"
194+
u, err := url.Parse(dbInfo.Database)
195+
if err != nil {
196+
return err
196197
}
197198

198-
db, err = gorm.Open(sqlite.Open(dbInfo.Database+"?_fk=true&_journal_mode="+journalMode+sharedCacheStr), &gorm.Config{
199+
u.Scheme = "file"
200+
q := u.Query()
201+
q.Set("_fk", "true")
202+
q.Set("_journal_mode", journalMode)
203+
if sharedCache {
204+
q.Set("cache", "shared")
205+
}
206+
u.RawQuery = q.Encode()
207+
dsn := u.String()
208+
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{
199209
Logger: logger.Default.LogMode(logger.Silent),
200210
TranslateError: true,
201211
})

0 commit comments

Comments
 (0)