Fixed read-only DBS

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
ItalyPaleAle 2023-02-03 21:04:34 +00:00
parent e8eec3f4c4
commit aba965751c
2 changed files with 55 additions and 2 deletions

View File

@ -127,6 +127,27 @@ func (a *sqliteDBAccess) getConnectionString() (string, error) {
qs = make(url.Values, 2)
}
// Check if the database is read-only or immutable
isReadOnly := false
if len(qs["mode"]) > 0 {
// Keep the first value only
qs["mode"] = []string{
qs["mode"][0],
}
if qs["mode"][0] == "ro" {
isReadOnly = true
}
}
if len(qs["immutable"]) > 0 {
// Keep the first value only
qs["immutable"] = []string{
qs["immutable"][0],
}
if qs["immutable"][0] == "1" {
isReadOnly = true
}
}
// We do not want to override a _txlock if set, but we'll show a warning if it's not "immediate"
if len(qs["_txlock"]) > 0 {
// Keep the first value only
@ -161,8 +182,8 @@ func (a *sqliteDBAccess) getConnectionString() (string, error) {
if isMemoryDB {
// For in-memory databases, set the journal to MEMORY, the only allowed option besides OFF (which would make transactions ineffective)
qs["_pragma"] = append(qs["_pragma"], "journal_mode(MEMORY)")
} else if a.metadata.DisableWAL {
// Set the journaling mode to "DELETE" (the default) if WAL is disabled
} else if a.metadata.DisableWAL || isReadOnly {
// Set the journaling mode to "DELETE" (the default) if WAL is disabled or if the database is read-only
qs["_pragma"] = append(qs["_pragma"], "journal_mode(DELETE)")
} else {
// Enable WAL

View File

@ -217,6 +217,38 @@ func TestGetConnectionString(t *testing.T) {
}
assert.Equal(t, "file::memory:?"+values.Encode(), connString)
})
t.Run("default to use DELETE for read-only databases", func(t *testing.T) {
logDest.Reset()
db.metadata.reset()
db.metadata.ConnectionString = "file:test.db?mode=ro"
connString, err := db.getConnectionString()
require.NoError(t, err)
values := url.Values{
"_txlock": []string{"immediate"},
"_pragma": []string{"busy_timeout(2000)", "journal_mode(DELETE)"},
"mode": []string{"ro"},
}
assert.Equal(t, "file:test.db?"+values.Encode(), connString)
})
t.Run("default to use DELETE for immutable databases", func(t *testing.T) {
logDest.Reset()
db.metadata.reset()
db.metadata.ConnectionString = "file:test.db?immutable=1"
connString, err := db.getConnectionString()
require.NoError(t, err)
values := url.Values{
"_txlock": []string{"immediate"},
"_pragma": []string{"busy_timeout(2000)", "journal_mode(DELETE)"},
"immutable": []string{"1"},
}
assert.Equal(t, "file:test.db?"+values.Encode(), connString)
})
})
}