-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
Added enum for SQLx Error and basic functions for it #1707
Merged
Merged
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
01d6050
Added enum for SQL Error and basic functions for it
darkmmon e973517
fmt
darkmmon 6de76f9
Restructured code to allow multiple database system simultaneously
darkmmon fd337e1
updated code error and moved Eq to derive by compiler
darkmmon 268c6f2
added ForeignKeyViolation implementation
darkmmon 2230575
fmt
darkmmon 015172c
added type param
darkmmon 96201b8
changed type param and used unwrap_or_default instead of unwrap
darkmmon 1d84145
fmt
darkmmon f19f28b
refactor code and update documentation
darkmmon 90c109f
updated some error code and fixed formatting
darkmmon 64b5176
create SQL Err test for UniqueConstraintViolation
darkmmon 610a470
fmt
darkmmon bdd432f
updated error codes for mysql errors
darkmmon b693813
added test for ForeignKeyConstraintViolation and removed unused impor…
darkmmon acc9ed4
fmt
darkmmon 96e566e
updated doc and error message
darkmmon df52415
added static str in SqlErr
darkmmon 02dceff
changed to add error message into SqlErr as String
darkmmon 5fb5128
updated test file to check database type through connection
darkmmon 90279ae
fmt
darkmmon 6fe8c5b
updated test for SqlErr to match the error type only
darkmmon ae8067f
Removed comment and fixed grammar mistake
darkmmon 144976d
fmt
darkmmon da0660b
Update src/error.rs
tyt2y3 156e577
Refactoring
billy1624 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
This file contains 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 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,67 @@ | ||
pub mod common; | ||
pub use common::{bakery_chain::*, setup::*, TestContext}; | ||
use rust_decimal_macros::dec; | ||
pub use sea_orm::{ | ||
entity::*, error::DbErr, error::SqlErr, tests_cfg, DatabaseConnection, DbBackend, EntityName, | ||
ExecResult, | ||
}; | ||
use uuid::Uuid; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() { | ||
let ctx = TestContext::new("bakery_chain_sql_err_tests").await; | ||
create_tables(&ctx.db).await.unwrap(); | ||
test_error(&ctx.db).await; | ||
ctx.delete().await; | ||
} | ||
|
||
pub async fn test_error(db: &DatabaseConnection) { | ||
let mud_cake = cake::ActiveModel { | ||
name: Set("Moldy Cake".to_owned()), | ||
price: Set(dec!(10.25)), | ||
gluten_free: Set(false), | ||
serial: Set(Uuid::new_v4()), | ||
bakery_id: Set(None), | ||
..Default::default() | ||
}; | ||
|
||
let cake = mud_cake.save(db).await.expect("could not insert cake"); | ||
|
||
// if compiling without sqlx, this assignment will complain, | ||
// but the whole test is useless in that case anyway. | ||
#[allow(unused_variables)] | ||
let error: DbErr = cake | ||
.into_active_model() | ||
.insert(db) | ||
.await | ||
.expect_err("inserting should fail due to duplicate primary key"); | ||
|
||
assert!(matches!( | ||
error.sql_err(), | ||
Some(SqlErr::UniqueConstraintViolation(_)) | ||
)); | ||
|
||
let fk_cake = cake::ActiveModel { | ||
name: Set("fk error Cake".to_owned()), | ||
price: Set(dec!(10.25)), | ||
gluten_free: Set(false), | ||
serial: Set(Uuid::new_v4()), | ||
bakery_id: Set(Some(1000)), | ||
..Default::default() | ||
}; | ||
|
||
let fk_error = fk_cake | ||
.insert(db) | ||
.await | ||
.expect_err("create foreign key should fail with non-primary key"); | ||
|
||
assert!(matches!( | ||
fk_error.sql_err(), | ||
Some(SqlErr::ForeignKeyConstraintViolation(_)) | ||
)); | ||
} |
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.
Hey @darkmmon, I wonder why for MySQL we use
error_number
to determine the error type instead of using_error_code_expanded
just like other databases?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.
@billy1624
The reason is that the error code for MySQL is ambiguous. If I wanted to do it with .code() like for other databases, I would not be able to distinguish the violation between unique constraint and foreign key constraint (they both return 23000).
I can only think of using the exact error number for distinguishing these errors, leading to the clumsy code. If there is an elegant way it would definitely be welcomed!