-
Notifications
You must be signed in to change notification settings - Fork 60
GH-56 Open sqlite db in readonly mode to avoid changes to .shm and .wal files #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
96c0705
to
01d56cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution.
pkg/sqlite3/sqlite3.go
Outdated
@@ -36,7 +37,8 @@ func Open(path string) (*SQLite3, error) { | |||
return nil, ErrorInvalidSQLite3 | |||
} | |||
|
|||
db, err := sql.Open("sqlite", path) | |||
// open sqlite3 database in read-only mode | |||
db, err := sql.Open("sqlite", fmt.Sprintf("file:%s?mode=ro&immutable=1&_mutex=no", path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_mutex
is an extended parameter available only in github.com/mattn/go-sqlite3, right? Or is it a standard param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_mutex
is an extended parameter specific to github.com/mattn/go-sqlite3. It is not a standard SQLite parameter. This parameter is used to control the mutex mode for SQLite connections, such as enabling or disabling thread safety. If a different SQLite driver is used, _mutex
may not be supported. would you like me to remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to remove it until we need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed it in this force-push.
Here are some more info on this that I found. The library sets the mutex to SQLITE_OPEN_FULLMUTEX
by default.
https://www.sqlite.org/c3ref/open.html
Maybe there could be a parameter added to the sqlite3 Open method that defaults to SQLITE_OPEN_FULLMUTEX
and makes mutex configurable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@knqyf263 the PR is ready for your review!
Ensure the SQLite database is opened in read-only mode to prevent modifications to `.shm` and `.wal` files. This change is particularly important when the code is executed with elevated privileges, as it avoids unintended changes to the database state.
01d56cc
to
aacf192
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Issue #, if available: #56
Description of changes:
When using the library to query the RPM database from rpmdb.sqlite, the library appears to remove the .shm (shared memory) and .wal (write-ahead log) files from the /var/lib/rpm directory. This behavior causes subsequent rpm commands (e.g., rpm -qa) to fail with errors until the rpm command is run with sudo privileges, which recreates the missing files.
This PR opens the db in readonly mode to avoid this issue!
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Verification
Before this change:
After this change: