Skip to content
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

Query Validation in readonly mode possible? #987

Closed
djmarcin opened this issue Jan 16, 2021 · 5 comments · Fixed by #1289
Closed

Query Validation in readonly mode possible? #987

djmarcin opened this issue Jan 16, 2021 · 5 comments · Fixed by #1289
Labels
db:sqlite Related to SQLite macros

Comments

@djmarcin
Copy link
Contributor

I'm trying to resolve an issue where my build system is generating the schema for query validation as a readonly sqlite file. Is it possible for query validation to operate on sqlite in readonly mode?

If not, perhaps an option to copy the database schema to an in-memory database for validation would be a possibility?

@abonander
Copy link
Collaborator

If you add ?mode=ro to your DATABASE_URL it should open the database in read-only mode. We can perhaps enable this by default for SQLite in the macros. @mehcode?

@abonander abonander added db:sqlite Related to SQLite macros labels Jan 16, 2021
@djmarcin
Copy link
Contributor Author

Hmm, that's weird. It's definitely not working for me with ?mode=ro, so it must be something subtle.

error: error returned from database: attempt to write a readonly database
  --> src/role.rs:42:18
   |
42 |           let id = sqlx::query!(
   |  __________________^
43 | |             "INSERT INTO roles (name, user_id, team_id) VALUES (?1, ?2, ?3)",
44 | |             self.name,
45 | |             self.user_id,
46 | |             self.team_id
47 | |         )
   | |_________^
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: error returned from database: attempt to write a readonly database
  --> src/user.rs:42:9
   |
42 | /         sqlx::query_as!(
43 | |             User,
44 | |             "SELECT id as \"id!\", uid, name FROM users WHERE uid = ?1 LIMIT 1",
45 | |             uid,
46 | |         )
   | |_________^
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

The directory itself is also not writable, so perhaps this is a misleading error and what's actually failing is some kind of locking or the WAL. For example, even accessing it with mode=ro from the command line fails, but setting immutable=true works:

$ echo "EXPLAIN SELECT * FROM sessions;" | sqlite3 "file:bazel-out/k8-fastbuild/bin/db/build_script.out_dir/query_checker.db?mode=ro"
Error: near line 1: attempt to write a readonly database
$ echo "EXPLAIN SELECT * FROM sessions;" | sqlite3 "file:bazel-out/k8-fastbuild/bin/db/build_script.out_dir/query_checker.db?immutable=true"
<explain output omitted>

The documentation says that this flag is for when the database file exists on read-only media. Still trying to dig into the code and see if supporting that flag would help here, but wanted to give an update.

@djmarcin
Copy link
Contributor Author

I put a patchset that seems to solve my problem in master...djmarcin:immutable

It appears to me that the "immutable" parameter only works when sent on the connection URI and there's no flag to enable it. I don't know how switching back to the URI affects any of the other connection options, so I'm not sure if this is mergeable or not. Let me know if you'd like me to send it as a PR, though.

@abonander
Copy link
Collaborator

@djmarcin if you want to add it as a flag to SqliteOptions and add support for parsing it, we'll gladly welcome a PR.

@mehcode
Copy link
Member

mehcode commented Jan 20, 2021

I can confirm that yes, immutable=true is impossible to pass to sqlite3_open and you must use the URI connection format. That feels crazy.

I agree with @abonander. We would accept a PR for that patch, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:sqlite Related to SQLite macros
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants