* fix(macros): smarter `.env` loading, caching, and invalidation
* feat(mysql): test `.env` loading in CI
* feat(postgres): test `.env` loading in CI
* feat(macros): allow `DATABASE_URL` to be empty
* fix(examples/postgres): make `cargo-sqlx` executable
* fix(examples/postgres): `cargo sqlx` invocation
* feat(examples/postgres): check offline prepare on more examples
* fix(examples/postgres): the name of this step
* fix(cli): don't suppress error from `dotenv()`
* fix(ci/examples/postgres): don't use heredoc in this step
* fix(ci/examples/postgres): multi-tenant
* fix(ci/examples/sqlite): test `.env` loading
* chore: add CHANGELOG entry
* build TlsConnector in blocking threadpool
The openssl TlsConnector synchronously loads certificates from files.
Loading these files can block for tens of milliseconds.
* Update sqlx-core/src/net/tls/tls_native_tls.rs
---------
Co-authored-by: David Übler <david.uebler@puzzleyou.de>
Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
* chore(sqlx-postgres): fix typo in `migrate.rs` comment
* feat(sqlite): support `no_tx` migrations
SQLite includes several SQL statements that are useful during migrations but
must be executed outside of a transaction to take effect, such as `PRAGMA
foreign_keys = ON|OFF` or `VACUUM`. Additionally, advanced migrations may want
more precise control over how statements are grouped into transactions or
savepoints to achieve the desired atomicity for different parts of the
migration.
While SQLx already supports marking migrations to run outside explicit
transactions through a `-- no-transaction` comment, this feature is currently
only available for `PgConnection`'s `Migrate` implementation, leaving SQLite and
MySQL without this capability. Although it's possible to work around this
limitation by implementing custom migration logic instead of executing
`Migrator#run`, this comes at a cost of significantly reduced developer
ergonomics: code that relies on the default migration logic, such as
`#[sqlx::test]` or `cargo sqlx database setup`, won't support these migrations.
These changes extend `SqliteConnection`'s `Migrate` implementation to support
`no_tx` migrations in the same way as PostgreSQL, addressing this feature gap. I
also considered implementing the same functionality for MySQL, but since I
haven't found a practical use case for it yet, and every
non-transaction-friendly statement I could think about in MySQL triggers
implicit commits anyway, I determined it wasn't necessary at this time and could
be considered an overreach.
* test(sqlite): add test for `no_tx` migrations
* chore(sqlx-sqlite): bring back useful comment
* chore(sqlx-sqlite): unify SQL dialect in annotation comments
* restore fallback to `async-io` for `connect_tcp()` when `runtime-tokio` feature is enabled
* `smol` and `async-global-executor` both use `async-task`, so `JoinHandle` impls can be consolidated
* no need for duplicate `yield_now()` impls
* delete `impl Socket for ()`
* Yield while salting password for PG SASL
To prevent spenting too much time doing synchronous work on the
event loop, we yield every few iterations of the hmac-sha256.
* Remove unused bench
Once a connection to the database is lost all future macro evaluations
will fail. This is fine for normal compilation since it tends to be
short but causes issues with rust-analyzer since it keeps the macro
binaries loaded for a long time.
This commit changes the macro implementation to drop the cached
connection when it encounters an IO or protocol error. In practice these
seem to be the errors that show up when the connection is lost and
dumping the connection on every error would have unnecessary overhead.
* sqlx-postgres(tests): fix positive_int domain type
* sqlx-postgres(tests): Update no pg array test
* sqlx-core: Remove conflicting Type impl for Box<JsonRawValue>
* Add support for `Box<JsonRawValue>` types.
This allows keeping structs that implement FromRow lifetime-free,
previously you had to use `&'a JsonRawValue`.
```rust
struct Foo {
bar: Box<JsonRawValue>,
}
```
* Add Encode impls for `JsonRawValue`.
* Allow single-field named structs to be transparent
This more closely matches the criteria for e.g. #[repr(transparent)]
and #[serde(transparent)].
* Add tests, fix error messages
In the previous code the worker would always assume that the custom command worked. However the higher level code would run a check and notice that a transaction was not actually started and raise an error without rolling back the transaction.
This improves the code by moving the transaction check into the worker to ensure that the transaction depth tracker is only modified if the user's custom command actually started a transaction.
Fixes: https://github.com/launchbadge/sqlx/issues/3932
Previously if the transaction was dropped while the transaction was being set up it was possible that the transaction was successfully opened but not closed. In common usage this would result in returning an open transaction to the connection pool which would have unexpected effects ranging from errors due to trying to nest transaction or serious bugs such as intended changes not occurring as they were unexpectedly inside a transaction that would never commit.
This resolves the issue by constructing the `Transaction` object (which activates the drop handler) before starting to open the transaction. In the worst case this could result in trying to `ROLLBACK` a transaction that was never started but this just results in a harmless error which is much better than leaving an unexpected open transaction active on the connection.
Fixes: https://github.com/launchbadge/sqlx/issues/3932
* fix race condition in pool close (#3217)
* fix deadlock in the postgres/listen example
* Only acquire as many permits as a child pool can offer
---------
Co-authored-by: Adam Cigánek <adam.ciganek@proton.me>
We need to be able to supply this variable via the environment when built via
Nix Crane, which uses cargo vendor, which will filter the .sqlx path. Renaming
is necessary in that case.
Unlike with CPU synchronization primitives, there is no semantic requirement
that the guard borrows the lock object.
We preserve the optimization of memoizing the release query by wrapping it in
an Arc. This way all instances of `PgAdvisoryLockGuard` that originate from the
same lock will share a single instance of the release query.
Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
* feat: create `sqlx.toml` format
* feat: add support for ignored_chars config to sqlx_core::migrate
* chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)
https://en.wikipedia.org/wiki/Byte_order_mark
* refactor: make `Config` always compiled
simplifies usage while still making parsing optional for less generated code
* refactor: add origin information to `Column`
* feat(macros): implement `type_override` and `column_override` from `sqlx.toml`
* refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates`
* feat: make macros aware of `macros.preferred-crates`
* feat: make `sqlx-cli` aware of `database-url-var`
* feat: teach macros about `migrate.table-name`, `migrations-dir`
* feat: teach macros about `migrate.ignored-chars`
* feat: teach `sqlx-cli` about `migrate.defaults`
* feat: teach `sqlx-cli` about `migrate.migrations-dir`
* feat: teach `sqlx-cli` about `migrate.table-name`
* feat: introduce `migrate.create-schemas`
* WIP feat: create multi-tenant database example
* SQLite extension loading via sqlx.toml for CLI and query macros
* fix: allow start_database to function when the SQLite database file does not already exist
* Added example demonstrating migration and compile-time checking with SQLite extensions
* remove accidentally included db file
* Update sqlx-core/src/config/common.rs
Doc formatting tweak
Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
* feat: create `sqlx.toml` format
* feat: add support for ignored_chars config to sqlx_core::migrate
* chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)
https://en.wikipedia.org/wiki/Byte_order_mark
* refactor: make `Config` always compiled
simplifies usage while still making parsing optional for less generated code
* refactor: add origin information to `Column`
* feat(macros): implement `type_override` and `column_override` from `sqlx.toml`
* refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates`
* feat: make macros aware of `macros.preferred-crates`
* feat: make `sqlx-cli` aware of `database-url-var`
* feat: teach macros about `migrate.table-name`, `migrations-dir`
* feat: teach macros about `migrate.ignored-chars`
* feat: teach `sqlx-cli` about `migrate.defaults`
* feat: teach `sqlx-cli` about `migrate.migrations-dir`
* feat: teach `sqlx-cli` about `migrate.table-name`
* feat: introduce `migrate.create-schemas`
* fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements
* feat: progress on axum-multi-tenant example
* feat(config): better errors for mislabeled fields
* WIP feat: filling out axum-multi-tenant example
* feat: multi-tenant example
No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.
* chore(ci): test multi-tenant example
* fixup after merge
* fix: CI, README for `multi-tenant`
* fix: clippy warnings
* fix: multi-tenant README
* fix: sequential versioning inference for migrations
* fix: migration versioning with explicit overrides
* fix: only warn on ambiguous crates if the invocation relies on it
* fix: remove unused imports
* fix: `sqlx mig add` behavior and tests
* fix: restore original type-checking order
* fix: deprecation warning in `tests/postgres/macros.rs`
* feat: create postgres/multi-database example
* fix: examples/postgres/multi-database
* fix: cargo fmt
* chore: add tests for config `migrate.defaults`
* fix: sqlx-cli/tests/add.rs
* feat(cli): add `--config` override to all relevant commands
* chore: run `sqlx mig add` test with `RUST_BACKTRACE=1`
* fix: properly canonicalize config path for `sqlx mig add` test
* fix: get `sqlx mig add` test passing
* fix(cli): test `migrate.ignored-chars`, fix bugs
* feat: create `macros.preferred-crates` example
* fix(examples): use workspace `sqlx`
* fix: examples
* fix: run `cargo fmt`
* fix: more example fixes
* fix(ci): preferred-crates setup
* fix: axum-multi-tenant example locked to specific sqlx version
* import anyhow::Context trait in sqlx-cli/src/lib.rs since it was being used and causing a compile error
* rebased on upstream/main
* make cargo fmt happy
* make clippy happy
* make clippy happier still
* fix: improved error reporting, added parsing test, removed sqlx-toml flag use
* switched to kebab-case for the config key
* switched to kebab-case for the config key
---------
Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
* Introduce `debug_row` function
* Use `debug_row` to implement `Debug` for `SqliteRow`
* Use `debug_row` in `PgRow`'s `Debug` implementation
* Match `MySqlRow`'s `Debug` implementation