Skip to content

sqlx 0.9#246

Open
peterschutt wants to merge 2 commits into
serverlesstechnology:mainfrom
peterschutt:sqlx-0.9-upgrade
Open

sqlx 0.9#246
peterschutt wants to merge 2 commits into
serverlesstechnology:mainfrom
peterschutt:sqlx-0.9-upgrade

Conversation

@peterschutt
Copy link
Copy Markdown

@peterschutt peterschutt commented Jun 2, 2026

transact-rs/sqlx#3723 introduces SqlStr as the arg type to query*() functions.

The only included implementation is SqlSafeStr for &'static str, so dynamically built queries like those found in SqlQueryFactory have to be constructed via AssertSqlSafe.

PR changes SqlQueryFactory to hold owned SqlStr and return clones of those from getters where the clones return a SqlStr that owns an Arc<&str> of the original owned String. This is the SqlStr clone impl (ref):

impl Clone for SqlStr {
    fn clone(&self) -> Self {
        Self(match &self.0 {
            Repr::Static(s) => Repr::Static(s),
            Repr::Arced(s) => Repr::Arced(s.clone()),
            // If `.clone()` gets called once, assume it might get called again.
            _ => Repr::Arced(self.as_str().into()),
        })
    }
}

SqlQueryFactory becomes:

pub(crate) struct SqlQueryFactory {
    event_table: &'static str,
    select_events: SqlStr,
    insert_event: SqlStr,
    all_events: SqlStr,
    insert_snapshot: SqlStr,
    update_snapshot: SqlStr,
    select_snapshot: SqlStr,
}

and the getters return clones of the owned SqlStrs:

impl SqlQueryFactory {
    ...
    pub fn select_events(&self) -> SqlStr {
        self.select_events.clone()
    }
    ...
}

As we are now passing event_table and snapshot_table interpolations directly into AssertSqlSafe I changed their types to impl SqlSafeStr so we are making the same safety assumption that sqlx does.

sqlx 0.9 is msrv 1.94.0 per sqlx msrv policy, so I've made the same change here.

Also, sqlx removed runtime + TLS combined features so the PR reconstructs the same combinations so that this crates features remain stable.

transact-rs/sqlx#3723 introduces `SqlStr` as the arg type to `query*()` functions.

The only included implementation is `SqlSafeStr for &'static str`, so dynamically built queries like those found in `SqlQueryFactory` have to be constructed via `AssertSqlSafe`.

PR changes `SqlQueryFactory` to hold owned `SqlStr` and return clones of those from getters where the clones return a `SqlStr` that owns an `Arc<&str>` of the original owned `String`. This is the `SqlStr` clone impl ([ref](https://github.com/transact-rs/sqlx/blob/75bc0487eb661da811bb7a3c5d158f1bd463fef4/sqlx-core/src/sql_str.rs#L129-L154)):

```rs
impl Clone for SqlStr {
    fn clone(&self) -> Self {
        Self(match &self.0 {
            Repr::Static(s) => Repr::Static(s),
            Repr::Arced(s) => Repr::Arced(s.clone()),
            // If `.clone()` gets called once, assume it might get called again.
            _ => Repr::Arced(self.as_str().into()),
        })
    }
}
```

`SqlQueryFactory` becomes:

```rs
pub(crate) struct SqlQueryFactory {
    event_table: &'static str,
    select_events: SqlStr,
    insert_event: SqlStr,
    all_events: SqlStr,
    insert_snapshot: SqlStr,
    update_snapshot: SqlStr,
    select_snapshot: SqlStr,
}
```

and the getters return clones of the owned `SqlStr`s:

```rs
impl SqlQueryFactory {
    ...
    pub fn select_events(&self) -> SqlStr {
        self.select_events.clone()
    }
    ...
}
```

As we are now passing `event_table` and `snapshot_table` interpolations directly into `AssertSqlSafe` I changed their types to `&'static str` so we are making the same safety assumption [that sqlx does](https://github.com/transact-rs/sqlx/blob/75bc0487eb661da811bb7a3c5d158f1bd463fef4/sqlx-core/src/sql_str.rs#L52).

sqlx 0.9 is msrv 1.94.0 per [sqlx msrv policy](https://github.com/transact-rs/sqlx/blob/75bc0487eb661da811bb7a3c5d158f1bd463fef4/sqlx-core/src/sql_str.rs#L52), so I've made the same change here.

Also, sqlx removed runtime + TLS combined features so the PR reconstructs the same combinations so that this crates features remain stable.
@davegarred
Copy link
Copy Markdown
Collaborator

Thanks for this work @peterschutt , I had not followed those changes. I'll take a look this evening.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 2, 2026

Codecov Report

❌ Patch coverage is 98.56115% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.54%. Comparing base (1c1c4b9) to head (fd4f3cc).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
persistence/mysql-es/src/event_repository.rs 92.85% 1 Missing ⚠️
persistence/postgres-es/src/event_repository.rs 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #246      +/-   ##
==========================================
+ Coverage   85.40%   85.54%   +0.14%     
==========================================
  Files          33       33              
  Lines        3433     3467      +34     
==========================================
+ Hits         2932     2966      +34     
  Misses        501      501              
Flag Coverage Δ
cqrs 84.40% <ø> (ø)
mysql 87.15% <98.57%> (+0.30%) ⬆️
postgres 87.05% <98.55%> (+0.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread persistence/postgres-es/src/view_repository.rs
Comment thread Cargo.toml
Users passing `&'static str` to the constructor methods will continue to
work unaffected.

Users passing dynamically generated `&str` values to the constructors
will need to wrap with `AssertSqlSafe`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants