mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00

* feat: Implement `get_transaction_depth` for drivers * test: Verify `get_transaction_depth()` on postgres * Refactor: `TransactionManager` delegation without BC SQLite implementation is currently WIP * Fix: Avoid breaking changes on `AnyConnectionBackend` * Refactor: Remove verbose `SqliteConnection` typing * Feat: Implementation for SQLite I have included `AtomicUsize` in `WorkerSharedState`. Ideally, it is not desirable to execute `load` and `fetch_add` in two separate steps, but we decided to allow it here since there is only one thread writing. To prevent writing from other threads, the field itself was made private, and a getter method was provided with `pub(crate)`. * Refactor: Same approach for `cached_statements_size` ref: a66787d36d62876b55475ef2326d17bade817aed * Fix: Add missing `is_in_transaction` for backend * Doc: Remove verbose "synchronously" word * Fix: Remove useless `mut` qualifier * feat: add Connection::begin_with This patch completes the plumbing of an optional statement from these methods to `TransactionManager::begin` without any validation of the provided statement. There is a new `Error::InvalidSavePoint` which is triggered by any attempt to call `Connection::begin_with` when we are already inside of a transaction. * feat: add Pool::begin_with and Pool::try_begin_with * feat: add Error::BeginFailed and validate that custom "begin" statements are successful * chore: add tests of Error::BeginFailed * chore: add tests of Error::InvalidSavePointStatement * chore: test begin_with works for all SQLite "BEGIN" statements * chore: improve comment on Connection::begin_with * feat: add default impl of `Connection::begin_with` This makes the new method a non-breaking change. * refactor: combine if statement + unwrap_or_else into one match * feat: use in-memory SQLite DB to avoid conflicts across tests run in parallel * feedback: remove public wrapper for sqlite3_txn_state Move the wrapper directly into the test that uses it instead. * fix: cache Status on MySqlConnection * fix: compilation errors * fix: format * fix: postgres test * refactor: delete `Connection::get_transaction_depth` * fix: tests --------- Co-authored-by: mpyw <ryosuke_i_628@yahoo.co.jp> Co-authored-by: Duncan Fairbanks <duncanfairbanks6@gmail.com>
103 lines
2.8 KiB
Rust
103 lines
2.8 KiB
Rust
use sqlx::{error::ErrorKind, mysql::MySql, Connection, Error};
|
|
use sqlx_test::new;
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_unique_violation() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let mut tx = conn.begin().await?;
|
|
|
|
sqlx::query("INSERT INTO tweet(id, text, owner_id) VALUES (1, 'Foo', 1)")
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
|
|
let res: Result<_, sqlx::Error> = sqlx::query("INSERT INTO tweet VALUES (1, NOW(), 'Foo', 1);")
|
|
.execute(&mut *tx)
|
|
.await;
|
|
let err = res.unwrap_err();
|
|
|
|
let err = err.into_database_error().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::UniqueViolation);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_foreign_key_violation() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let mut tx = conn.begin().await?;
|
|
|
|
let res: Result<_, sqlx::Error> =
|
|
sqlx::query("INSERT INTO tweet_reply (tweet_id, text) VALUES (1, 'Reply!');")
|
|
.execute(&mut *tx)
|
|
.await;
|
|
let err = res.unwrap_err();
|
|
|
|
let err = err.into_database_error().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::ForeignKeyViolation);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_not_null_violation() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let mut tx = conn.begin().await?;
|
|
|
|
let res: Result<_, sqlx::Error> = sqlx::query("INSERT INTO tweet (text) VALUES (null);")
|
|
.execute(&mut *tx)
|
|
.await;
|
|
let err = res.unwrap_err();
|
|
|
|
let err = err.into_database_error().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::NotNullViolation);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_check_violation() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let mut tx = conn.begin().await?;
|
|
|
|
let res: Result<_, sqlx::Error> =
|
|
sqlx::query("INSERT INTO products VALUES (1, 'Product 1', 0);")
|
|
.execute(&mut *tx)
|
|
.await;
|
|
let err = res.unwrap_err();
|
|
|
|
let err = err.into_database_error().unwrap();
|
|
|
|
assert_eq!(err.kind(), ErrorKind::CheckViolation);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_begin_failed() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let res = conn.begin_with("SELECT * FROM tweet").await;
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert!(matches!(err, Error::BeginFailed), "{err:?}");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_fails_with_invalid_save_point_statement() -> anyhow::Result<()> {
|
|
let mut conn = new::<MySql>().await?;
|
|
let mut txn = conn.begin().await?;
|
|
let txn_conn = sqlx::Acquire::acquire(&mut txn).await?;
|
|
let res = txn_conn.begin_with("BEGIN").await;
|
|
|
|
let err = res.unwrap_err();
|
|
|
|
assert!(matches!(err, Error::InvalidSavePointStatement), "{err}");
|
|
|
|
Ok(())
|
|
}
|