From 9ddf6a42171be3d063f14698158ac2bf30081b54 Mon Sep 17 00:00:00 2001 From: Vanilor Date: Wed, 17 Jun 2026 21:04:46 +0200 Subject: [PATCH] fix(macros): added SQLX_SQLITE_REGISTER_REGEXP environment variable - fix issue #3070 - allows sqlx-cli prepare to work with sqlite regexp --- Cargo.toml | 2 +- sqlx-macros-core/Cargo.toml | 2 ++ sqlx-macros/Cargo.toml | 2 ++ sqlx-sqlite/src/options/mod.rs | 7 ++++++- tests/sqlite/macros.rs | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3738b814ca..aa559cf3ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -160,7 +160,7 @@ mac_address = ["sqlx-core/mac_address", "sqlx-macros?/mac_address", "sqlx-postgr rust_decimal = ["sqlx-core/rust_decimal", "sqlx-macros?/rust_decimal", "sqlx-mysql?/rust_decimal", "sqlx-postgres?/rust_decimal"] time = ["sqlx-core/time", "sqlx-macros?/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"] uuid = ["sqlx-core/uuid", "sqlx-macros?/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"] -regexp = ["sqlx-sqlite?/regexp"] +regexp = ["sqlx-sqlite?/regexp", "sqlx-macros?/regexp"] bstr = ["sqlx-core/bstr"] [workspace.dependencies] diff --git a/sqlx-macros-core/Cargo.toml b/sqlx-macros-core/Cargo.toml index 42bf0f5e8c..01c99d38d3 100644 --- a/sqlx-macros-core/Cargo.toml +++ b/sqlx-macros-core/Cargo.toml @@ -54,6 +54,8 @@ rust_decimal = ["sqlx-core/rust_decimal", "sqlx-mysql?/rust_decimal", "sqlx-post time = ["sqlx-core/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"] uuid = ["sqlx-core/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"] +regexp = ["sqlx-sqlite?/regexp"] + [dependencies] sqlx-core = { workspace = true, features = ["offline"] } sqlx-mysql = { workspace = true, features = ["offline", "migrate"], optional = true, default-features = false } diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index bd90da9608..028b5331f6 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -53,6 +53,8 @@ time = ["sqlx-macros-core/time"] uuid = ["sqlx-macros-core/uuid"] json = ["sqlx-macros-core/json"] +regexp = ["sqlx-macros-core/regexp"] + [dependencies] sqlx-core = { workspace = true, features = ["any"] } sqlx-macros-core = { workspace = true } diff --git a/sqlx-sqlite/src/options/mod.rs b/sqlx-sqlite/src/options/mod.rs index 0cafc4d08c..d2c441051c 100644 --- a/sqlx-sqlite/src/options/mod.rs +++ b/sqlx-sqlite/src/options/mod.rs @@ -193,6 +193,11 @@ impl SqliteConnectOptions { // Soft limit on the number of rows that `ANALYZE` touches per index. pragmas.insert("analysis_limit".into(), None); + #[cfg(feature = "regexp")] + let register_regexp_function = std::env::var("SQLX_SQLITE_REGISTER_REGEXP") + .ok() + .is_some_and(|val| val.eq_ignore_ascii_case("true") || val == "1"); + Self { filename: Cow::Borrowed(Path::new(":memory:")), in_memory: false, @@ -214,7 +219,7 @@ impl SqliteConnectOptions { row_channel_size: 50, optimize_on_close: OptimizeOnClose::Disabled, #[cfg(feature = "regexp")] - register_regexp_function: false, + register_regexp_function, } } diff --git a/tests/sqlite/macros.rs b/tests/sqlite/macros.rs index 0ab4dd56bf..0c2571b042 100644 --- a/tests/sqlite/macros.rs +++ b/tests/sqlite/macros.rs @@ -358,4 +358,20 @@ async fn test_column_override_exact_nullable() -> anyhow::Result<()> { Ok(()) } +/// Requires SQLX_SQLITE_REGISTER_REGEXP environment variable to compile and succeed. +#[cfg(feature = "regexp")] +#[sqlx_macros::test] +async fn test_regexp_function_is_registered_via_env() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let record = + sqlx::query!(r#"select id as "id?: MyInt" from tweet where text regexp 'is pretty cool'"#) + .fetch_one(&mut conn) + .await?; + + assert_eq!(record.id, Some(MyInt(1))); + + Ok(()) +} + // we don't emit bind parameter typechecks for SQLite so testing the overrides is redundant