-
Notifications
You must be signed in to change notification settings - Fork 281
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
feat(sql) Adding support for Sqlite database encryption + Other pragma (journal, read only...) + In memory database #1441
base: v2
Are you sure you want to change the base?
Conversation
…ory, journal mode selection, read only...
Thanks for contributing and sorry for the long delay in your first PR!
We could expose this as a feature flag in the plugin (similar to the sqlite/mysql/psql flags).
Same for this with the About the options:
|
- Add features for bundled-sqlcipher - Updated Readme
Hello @FabianLars, I did change the commit to use directly the SqliteConnectOptions struct from sqlx, it does make sense to use it directly. I'm not really sure how you want to make the connect options work with the JS backend. Would be nice to load dynamically any database with the connect options but I'm not really sure how to do that. I've tried to implement it so that it is possible to specify an in-memory database in the SQLite options. It could eventually be possible to use arbitrary file paths, but i still use the path_mapper to build the path in this pull request. I also added the feature flags for sqlcipher. Sqlite optionsAdding Sqlite optionsSimilarly as adding migrations, it is possible to add Sqlite options such as database encryption or regular Sqlite options. use tauri_plugin_sql::{Builder, Migration, MigrationKind, SqliteConnectOptions, SqliteJournalMode};
fn main() {
let migrations = vec![
// Define your migrations here
Migration {
version: 1,
description: "create_initial_tables",
sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);",
kind: MigrationKind::Up,
}
];
tauri::Builder::default()
.plugin(
tauri_plugin_sql::Builder::default()
.add_migrations("sqlite:mydatabase.db", migrations)
.add_sqlite_options("sqlite:mydatabase.db", SqliteConnectOptions::new().pragma("key", "my_database_key").journal_mode(SqliteJournalMode::Off))
.build(),
)
...
} Refer to the SqliteConnectOptions doc of the sqlx crate to view all possible options. In memoryIt is possible to use an in-memory database using SqliteConnectOptions::from_url with use tauri_plugin_sql::{Builder, Migration, MigrationKind, SqliteConnectOptions, SqliteJournalMode};
fn main() {
let migrations = vec![
// Define your migrations here
Migration {
version: 1,
description: "create_initial_tables",
sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);",
kind: MigrationKind::Up,
}
];
tauri::Builder::default()
.plugin(
tauri_plugin_sql::Builder::default()
.add_migrations("sqlite:mydatabase.db", migrations)
.add_sqlite_options("sqlite:mydatabase.db", SqliteConnectOptions::from_url("sqlite::memory:"))
.build(),
)
...
} SqlCipherTo make it work with SqlCipher, you'll need to use the bundled-sqlcipher or bundled-sqlcipher-vendored-openssl instead of the sqlite feature depending on your system. [dependencies.tauri-plugin-sql]
features = ["bundled-sqlcipher"] You'll also need openssl to build. Please refer to libsqlite3-sys documentation to understand how to install openssl. On windows, I recommend building and installing openssl using vcpkg https://github.com/Microsoft/vcpkg
|
Oh man, i really didn't think this through at first. I totally forgot that you can open multiple databases at the same time... I'll try allocate a bit of time this week to think about this plugin. There are a few things i'd like to change and it probably makes sense to combine some of them. Would it be okay for you if i combine your changes into another PR and add you as a co-author (if it comes to that)? |
Hello @FabianLars, No problem on my side, please feel free to combine whatever part of this contribution which is useful to you. Feel free to also add code from #1458 if you find it useful. Edouard. |
Thanks! I think we can merge 1458 seperately before that tbh. It should (hopefully) be unrelated enough 😅 |
Hey @eleroy, i tried to use your plugin to decrypt my sqlite database. Unfortunately I am not able to get it work. Here is my cargo.toml [build-dependencies]
tauri-build = { version = "2.0.0-beta", features = [] }
[dependencies]
tauri = { version = "2.0.0-beta", features = [] }
tauri-plugin-shell = "2.0.0-beta"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
libsqlite3-sys = { version = "*", features = ["bundled-sqlcipher"] }
[dependencies.tauri-plugin-sql]
features = ["sqlite"] # or "postgres", or "mysql"
version = "2.0.0-beta"
git = "https://github.com/eleroy/plugins-workspace"
branch = "v2" Here the main.rs fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![])
.run(tauri::generate_context!())
.expect("error while running tauri application");
} The method .add_sqlite_options() is not available in my project. fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_sql::Builder::default()
.add_sqlite_options("sqlite:mydatabase.db", SqliteConnectOptions::new().pragma("key", "my_database_key").journal_mode(SqliteJournalMode::Off))
.build())
.invoke_handler(tauri::generate_handler![])
.run(tauri::generate_context!())
.expect("error while running tauri application");
} Am I doing something wrong? Thanks in advance! |
Hello, Ah yes, I need to update the readme. I moved the PR to another branch:
also you can remove Also on Mac os I might not work out of the box (I have not tested). I think it is best to use the feature Look into the docs here if you have more trouble https://github.com/rusqlite/rusqlite#notes-on-building-rusqlite-and-libsqlite3-sys. Let me know if it works or not. |
Hey! Thank you, it works! I tried the "bundled-sqlcipher-vendored-openssl" - This one didn't work for me on MacOS. Best wishes |
Hey @eleroy, how do you handle the database connection when reloading the client? When Tauri is initializing the plugins, accessing the Database works fine. On reload I first load my Database and then I use my select statements. Do you know a workaround? Best wishes! |
Hello, Yes it is pretty annoying having to reload the whole app. I don't know how to handle this. From my experience, this happens with several tauri plugins. Maybe you could search in tauri plugin issues. You can maybe try to put all the database logic in a separated file and then import the database and select method to your App. Then when you reload, it won't reload the database logic unless the specific file is modified (I guess...). |
Hi everyone,
Here is a pull request referring to issues #7 #875 and PR #877 (in-memory database).
This is my first pull request, and I'm very new to rust so if the code and PR are not well written/formatted please feel free to edit it.
I propose a way to specify SqlLite options similarly to Migrations. For this I had to review also how non-existing database are created. Here is how it works:
Sqlite options
Adding Sqlite options
Similarly as adding migrations, it is possible to add Sqlite options such as database encryption or regular Sqlite options.
All the options are specified in the struct definition
In memory
A database name containing :memory will be loaded as an in-memory database.
SqlCipher
To make it work with SqlCipher, you'll need to make additionnal changes to you cargo.toml:
You'll also need openssl to build. Please refer to libsqlite3-sys documentation to understand how to build.
On windows, I recommend building and installing openssl using vcpkg https://github.com/Microsoft/vcpkg