-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verifier-alliance-database): initialize
Extract all verifier-alliance database manipulations into separate crate. Will be used further by eth-bytecode-db service
- Loading branch information
1 parent
63ea25e
commit 1164e09
Showing
29 changed files
with
2,626 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
[package] | ||
name = "verifier-alliance-database" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
blockscout-display-bytes = { workspace = true } | ||
keccak-hash = { workspace = true } | ||
sea-orm = { workspace = true } | ||
serde_json = { workspace = true } | ||
sha2 = { workspace = true } | ||
sha3 = { workspace = true } | ||
strum = { workspace = true } | ||
verification-common-v1 = { workspace = true } | ||
verifier-alliance-entity-v1 = { workspace = true } | ||
serde = { workspace = true } | ||
|
||
[dev-dependencies] | ||
blockscout-service-launcher = { workspace = true, features = ["test-database"] } | ||
pretty_assertions = { workspace = true } | ||
serde_json = { workspace = true } | ||
serde_with = { workspace = true } | ||
tokio = { workspace = true, features = ["macros"] } | ||
verifier-alliance-migration-v1 = { workspace = true } |
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,69 @@ | ||
macro_rules! insert_then_select { | ||
( $txn:expr, $entity_module:ident, $active_model:expr, $update_on_conflict:expr, [ $( ($column:ident, $value:expr) ),+ $(,)? ] ) => { | ||
{ | ||
use anyhow::Context; | ||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; | ||
|
||
let result: Result<_, sea_orm::DbErr> = $entity_module::Entity::insert($active_model.clone()) | ||
.on_conflict(sea_orm::sea_query::OnConflict::new().do_nothing().to_owned()) | ||
.exec($txn) | ||
.await; | ||
|
||
// Returns the model and the bool flag showing whether the model was actually inserted. | ||
match result { | ||
Ok(res) => { | ||
let last_insert_id = res.last_insert_id; | ||
let model = $entity_module::Entity::find_by_id(last_insert_id.clone()) | ||
.one($txn) | ||
.await | ||
.context(format!("select from \"{}\" by \"id\"", stringify!($entity_module)))? | ||
.ok_or(anyhow::anyhow!( | ||
"select from \"{}\" by \"id\"={:?} returned no data", | ||
stringify!($entity_module), | ||
last_insert_id | ||
))?; | ||
|
||
Ok((model, true)) | ||
} | ||
Err(sea_orm::DbErr::RecordNotInserted) => { | ||
let mut model = | ||
$entity_module::Entity::find() | ||
$( | ||
.filter($entity_module::Column::$column.eq($value)) | ||
)* | ||
.one($txn) | ||
.await | ||
.context(format!("select from \"{}\" by unique columns", stringify!($entity_module)))? | ||
.ok_or(anyhow::anyhow!("select from \"{}\" by unique columns returned no data", stringify!($entity_module)))?; | ||
// The active model have not been inserted. | ||
// Thus, there were a value already that we need to update. | ||
if $update_on_conflict { | ||
let mut active_model_to_update = $active_model; | ||
for primary_key in <$entity_module::PrimaryKey as sea_orm::Iterable>::iter() { | ||
let column = sea_orm::PrimaryKeyToColumn::into_column(primary_key); | ||
let value = sea_orm::ModelTrait::get(&model, column); | ||
sea_orm::ActiveModelTrait::set(&mut active_model_to_update, column, value); | ||
} | ||
let updated_model = sea_orm::ActiveModelTrait::update( | ||
active_model_to_update, $txn | ||
).await.context(format!("update on conflict in \"{}\"", stringify!($entity_module)))?; | ||
|
||
if updated_model != model { | ||
// tracing::warn!( | ||
// model=?model, | ||
// updated_model=?updated_model, | ||
// "the \"{}\" model has been updated", | ||
// stringify!($entity_module) | ||
// ); | ||
model = updated_model; | ||
} | ||
} | ||
|
||
Ok((model, false)) | ||
} | ||
Err(err) => Err(err).context(format!("insert into \"{}\"", stringify!($entity_module))), | ||
} | ||
} | ||
}; | ||
} | ||
pub(crate) use insert_then_select; |
Oops, something went wrong.