-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(sqlx.toml): support SQLite extensions in macros and sqlx-cli #3917
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
Merged
Merged
Changes from all commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
39e5a5a
feat: create `sqlx.toml` format
abonander fdce6b8
feat: add support for ignored_chars config to sqlx_core::migrate
abonander 44db470
chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)
abonander 62c0c6f
refactor: make `Config` always compiled
abonander d7b6cd2
refactor: add origin information to `Column`
abonander c0ed389
feat(macros): implement `type_override` and `column_override` from `s…
abonander ff3b532
refactor(sqlx.toml): make all keys kebab-case, create `macros.preferr…
abonander 0791e06
feat: make macros aware of `macros.preferred-crates`
abonander 6b0df6d
feat: make `sqlx-cli` aware of `database-url-var`
abonander c377de7
feat: teach macros about `migrate.table-name`, `migrations-dir`
abonander d41aa07
feat: teach macros about `migrate.ignored-chars`
abonander 57c75b4
feat: teach `sqlx-cli` about `migrate.defaults`
abonander b4efb9e
feat: teach `sqlx-cli` about `migrate.migrations-dir`
abonander 528a7d4
feat: teach `sqlx-cli` about `migrate.table-name`
abonander 2ec3698
feat: introduce `migrate.create-schemas`
abonander a8d9ac2
WIP feat: create multi-tenant database example
abonander f415b08
SQLite extension loading via sqlx.toml for CLI and query macros
djarb 2231769
fix: allow start_database to function when the SQLite database file d…
djarb 6e55a88
Added example demonstrating migration and compile-time checking with …
djarb 8126ae1
remove accidentally included db file
djarb d36b9f9
Update sqlx-core/src/config/common.rs
djarb 15c837e
feat: create `sqlx.toml` format
abonander f5ed721
feat: add support for ignored_chars config to sqlx_core::migrate
abonander 0a7dfea
chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)
abonander 2a35d6a
refactor: make `Config` always compiled
abonander 2bed2ee
refactor: add origin information to `Column`
abonander d687017
feat(macros): implement `type_override` and `column_override` from `s…
abonander 3a22958
refactor(sqlx.toml): make all keys kebab-case, create `macros.preferr…
abonander a6947b2
feat: make macros aware of `macros.preferred-crates`
abonander ff5374e
feat: make `sqlx-cli` aware of `database-url-var`
abonander f069470
feat: teach macros about `migrate.table-name`, `migrations-dir`
abonander 77db4af
feat: teach macros about `migrate.ignored-chars`
abonander 199429d
feat: teach `sqlx-cli` about `migrate.defaults`
abonander 217742a
feat: teach `sqlx-cli` about `migrate.migrations-dir`
abonander c6fea5b
feat: teach `sqlx-cli` about `migrate.table-name`
abonander b43a957
feat: introduce `migrate.create-schemas`
abonander 396b23a
fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared …
abonander 59cd288
feat: progress on axum-multi-tenant example
abonander 08b2364
feat(config): better errors for mislabeled fields
abonander bda547c
WIP feat: filling out axum-multi-tenant example
abonander abff167
feat: multi-tenant example
abonander 85b4507
chore(ci): test multi-tenant example
abonander 2b1a271
fixup after merge
abonander d998be1
fix: CI, README for `multi-tenant`
abonander 7ab599c
fix: clippy warnings
abonander 0694026
fix: multi-tenant README
abonander ec20110
fix: sequential versioning inference for migrations
abonander 1893336
fix: migration versioning with explicit overrides
abonander 52103a0
fix: only warn on ambiguous crates if the invocation relies on it
abonander 0342c6f
fix: remove unused imports
abonander 8f1a8b0
fix: `sqlx mig add` behavior and tests
abonander 41df655
fix: restore original type-checking order
abonander 57b711f
fix: deprecation warning in `tests/postgres/macros.rs`
abonander 0d87749
feat: create postgres/multi-database example
abonander 19d1e4a
fix: examples/postgres/multi-database
abonander 9b60b10
fix: cargo fmt
abonander d903aef
chore: add tests for config `migrate.defaults`
abonander 502d2b3
fix: sqlx-cli/tests/add.rs
abonander 21bd242
feat(cli): add `--config` override to all relevant commands
abonander 635670f
chore: run `sqlx mig add` test with `RUST_BACKTRACE=1`
abonander 12b823e
fix: properly canonicalize config path for `sqlx mig add` test
abonander b4cc0d3
fix: get `sqlx mig add` test passing
abonander 2d007c0
fix(cli): test `migrate.ignored-chars`, fix bugs
abonander 5453589
feat: create `macros.preferred-crates` example
abonander 929939a
fix(examples): use workspace `sqlx`
abonander aa43ced
fix: examples
abonander 35b6a79
fix: run `cargo fmt`
abonander f57ee86
fix: more example fixes
abonander daca7a0
fix(ci): preferred-crates setup
abonander 756ee0c
fix: axum-multi-tenant example locked to specific sqlx version
djarb 1956256
import anyhow::Context trait in sqlx-cli/src/lib.rs since it was bein…
djarb 94c93d8
rebased on upstream/main
djarb 89c63ae
make cargo fmt happy
djarb 4bf945a
make clippy happy
djarb 6d4db03
make clippy happier still
djarb c04c328
fix: improved error reporting, added parsing test, removed sqlx-toml …
djarb fe9916f
switched to kebab-case for the config key
djarb 55567c3
switched to kebab-case for the config key
djarb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "sqlx-example-sqlite-extension" | ||
version = "0.1.0" | ||
license.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
authors.workspace = true | ||
|
||
[dependencies] | ||
sqlx = { path = "../../../", features = [ "sqlite", "runtime-tokio", "tls-native-tls", "sqlx-toml"] } | ||
tokio = { version = "1.20.0", features = ["rt", "macros"]} | ||
anyhow = "1.0" | ||
|
||
[lints] | ||
workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
# This grabs a pre-compiled version of the extension used in this | ||
# example, and stores it in a temporary directory. That's a bit | ||
# unusual. Normally, any extensions you need will be installed into a | ||
# directory on the library search path, either by using the system | ||
# package manager or by compiling and installing it yourself. | ||
|
||
mkdir /tmp/sqlite3-lib && wget -O /tmp/sqlite3-lib/ipaddr.so https://github.com/nalgeon/sqlean/releases/download/0.15.2/ipaddr.so |
25 changes: 25 additions & 0 deletions
25
examples/sqlite/extension/migrations/20250203094951_addresses.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
create table addresses (address text, family integer); | ||
|
||
-- The `ipfamily` function is provided by the | ||
-- [ipaddr](https://github.com/nalgeon/sqlean/blob/main/docs/ipaddr.md) | ||
-- sqlite extension, and so this migration can not run if that | ||
-- extension is not loaded. | ||
insert into addresses (address, family) values | ||
('fd04:3d29:9f41::1', ipfamily('fd04:3d29:9f41::1')), | ||
('10.0.0.1', ipfamily('10.0.0.1')), | ||
('10.0.0.2', ipfamily('10.0.0.2')), | ||
('fd04:3d29:9f41::2', ipfamily('fd04:3d29:9f41::2')), | ||
('fd04:3d29:9f41::3', ipfamily('fd04:3d29:9f41::3')), | ||
('10.0.0.3', ipfamily('10.0.0.3')), | ||
('fd04:3d29:9f41::4', ipfamily('fd04:3d29:9f41::4')), | ||
('fd04:3d29:9f41::5', ipfamily('fd04:3d29:9f41::5')), | ||
('fd04:3d29:9f41::6', ipfamily('fd04:3d29:9f41::6')), | ||
('10.0.0.4', ipfamily('10.0.0.4')), | ||
('10.0.0.5', ipfamily('10.0.0.5')), | ||
('10.0.0.6', ipfamily('10.0.0.6')), | ||
('10.0.0.7', ipfamily('10.0.0.7')), | ||
('fd04:3d29:9f41::7', ipfamily('fd04:3d29:9f41::7')), | ||
('fd04:3d29:9f41::8', ipfamily('fd04:3d29:9f41::8')), | ||
('10.0.0.8', ipfamily('10.0.0.8')), | ||
('fd04:3d29:9f41::9', ipfamily('fd04:3d29:9f41::9')), | ||
('10.0.0.9', ipfamily('10.0.0.9')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[common.drivers.sqlite] | ||
# Including the full path to the extension is somewhat unusual, | ||
# because normally an extension will be installed in a standard | ||
# directory which is part of the library search path. If that were the | ||
# case here, the load-extensions value could just be `["ipaddr"]` | ||
# | ||
# When the extension file is installed in a non-standard location, as | ||
# in this example, there are two options: | ||
# * Provide the full path the the extension, as seen below. | ||
# * Add the non-standard location to the library search path, which on | ||
# Linux means adding it to the LD_LIBRARY_PATH environment variable. | ||
load-extensions = ["/tmp/sqlite3-lib/ipaddr"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::str::FromStr; | ||
|
||
use sqlx::{ | ||
query, | ||
sqlite::{SqliteConnectOptions, SqlitePool}, | ||
}; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> anyhow::Result<()> { | ||
let opts = SqliteConnectOptions::from_str(&std::env::var("DATABASE_URL")?)? | ||
// The sqlx.toml file controls loading extensions for the CLI | ||
// and for the query checking macros, *not* for the | ||
// application while it's running. Thus, if we want the | ||
// extension to be available during program execution, we need | ||
// to load it. | ||
// | ||
// Note that while in this case the extension path is the same | ||
// when checking the program (sqlx.toml) and when running it | ||
// (here), this is not required. The runtime environment can | ||
// be entirely different from the development one. | ||
// | ||
// The extension can be described with a full path, as seen | ||
// here, but in many cases that will not be necessary. As long | ||
// as the extension is installed in a directory on the library | ||
// search path, it is sufficient to just provide the extension | ||
// name, like "ipaddr" | ||
.extension("/tmp/sqlite3-lib/ipaddr"); | ||
|
||
let db = SqlitePool::connect_with(opts).await?; | ||
|
||
// We're not running the migrations here, for the sake of brevity | ||
// and to confirm that the needed extension was loaded during the | ||
// CLI migrate operation. It would not be unusual to run the | ||
// migrations here as well, though, using the database connection | ||
// we just configured. | ||
|
||
query!( | ||
"insert into addresses (address, family) values (?1, ipfamily(?1))", | ||
"10.0.0.10" | ||
) | ||
.execute(&db) | ||
.await?; | ||
|
||
println!("Query which requires the extension was successfully executed."); | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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've got a better idea of how to do this but it's a nontrivial change so I'm going to go ahead and merge. Just know that this might look a bit different in the final release.