Skip to content

Commit

Permalink
update entity to use on delete for foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Aug 23, 2024
1 parent 82c1bdb commit 8edcb52
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 42 deletions.
5 changes: 4 additions & 1 deletion backend/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20231207_000001_create_table::Migration), Box::new(m20240821_000001_create_tag::Migration)]
vec![
Box::new(m20231207_000001_create_table::Migration),
Box::new(m20240821_000001_create_tag::Migration),
]
}
}
50 changes: 33 additions & 17 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-announcement-contest")
.from(Announcement::Table, Announcement::ContestId)
.to(Contest::Table, Contest::Id),
.to(Contest::Table, Contest::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Announcement::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-announcement-user")
.from(Announcement::Table, Announcement::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(
ColumnDef::new(Announcement::CreateAt)
Expand Down Expand Up @@ -233,7 +235,8 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-announcement-user-hoster")
.from(Contest::Table, Contest::Hoster)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(ColumnDef::new(Contest::Begin).date_time().not_null())
.col(ColumnDef::new(Contest::End).date_time().not_null())
Expand Down Expand Up @@ -284,14 +287,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-education-problem")
.from(Education::Table, Education::ProblemId)
.to(Problem::Table, Problem::Id),
.to(Problem::Table, Problem::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(ColumnDef::new(Education::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-education-user")
.from(Education::Table, Education::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(
ColumnDef::new(Education::Tags)
Expand Down Expand Up @@ -326,14 +331,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-problem-user")
.from(Problem::Table, Problem::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(ColumnDef::new(Problem::ContestId).integer())
.col(ColumnDef::new(Problem::ContestId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-problem-contest")
.from(Problem::Table, Problem::ContestId)
.to(Contest::Table, Contest::Id),
.to(Contest::Table, Contest::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.col(
ColumnDef::new(Problem::AcceptCount)
Expand Down Expand Up @@ -409,14 +416,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-submit-user")
.from(Submit::Table, Submit::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Submit::ProblemId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-submit-problem")
.from(Submit::Table, Submit::ProblemId)
.to(Problem::Table, Problem::Id),
.to(Problem::Table, Problem::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(
ColumnDef::new(Submit::UploadAt)
Expand Down Expand Up @@ -473,14 +482,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-test-user")
.from(Testcase::Table, Testcase::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Testcase::ProblemId).integer().null())
.foreign_key(
ForeignKey::create()
.name("fk-test-user")
.from(Testcase::Table, Testcase::ProblemId)
.to(Problem::Table, Problem::Id),
.to(Problem::Table, Problem::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Testcase::Input).binary().not_null())
.col(ColumnDef::new(Testcase::Output).binary().not_null())
Expand Down Expand Up @@ -516,7 +527,8 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-token-user")
.from(Token::Table, Token::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Token::Rand).binary().not_null())
.col(
Expand Down Expand Up @@ -586,14 +598,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-pivot-contest-user")
.from(UserContest::Table, UserContest::ContestId)
.to(Contest::Table, Contest::Id),
.to(Contest::Table, Contest::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(UserContest::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-pivot-user-contest")
.from(UserContest::Table, UserContest::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(
ColumnDef::new(UserContest::Score)
Expand Down Expand Up @@ -622,14 +636,16 @@ impl MigrationTrait for Migration {
ForeignKey::create()
.name("fk-chat-user")
.from(Chat::Table, Chat::UserId)
.to(User::Table, User::Id),
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(ColumnDef::new(Chat::ProblemId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-chat-problem")
.from(Chat::Table, Chat::ProblemId)
.to(Problem::Table, Problem::Id),
.to(Problem::Table, Problem::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.col(
ColumnDef::new(Chat::CreateAt)
Expand Down
11 changes: 0 additions & 11 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ impl Contest for ArcServer {
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.get_or_insert(|req| async move {
let txn = self.db.begin().await?;

let result = Entity::delete_by_id(req.id)
.with_auth(&auth)
.write()?
Expand All @@ -248,15 +246,6 @@ impl Contest for ArcServer {
.await
.map_err(Into::<Error>::into)?;

problem::Entity::update_many()
.col_expr(problem::Column::ContestId, Expr::value(Value::Int(None)))
.filter(crate::entity::testcase::Column::ProblemId.eq(req.id))
.exec(&txn)
.instrument(info_span!("remove_child"))
.await?;

txn.commit().await.map_err(|_| Error::Retry)?;

if result.rows_affected == 0 {
return Err(Error::NotInDB);
}
Expand Down
13 changes: 1 addition & 12 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,13 @@ impl Problem for ArcServer {
async fn remove(&self, req: Request<RemoveRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self.rate_limit(req).in_current_span().await?;
req.get_or_insert(|req| async move {
let txn = self.db.begin().await?;

let result = Entity::delete_by_id(req.id)
.with_auth(&auth)
.write()?
.exec(&txn)
.exec(self.db.deref())
.instrument(info_span!("remove").or_current())
.await?;

testcase::Entity::update_many()
.col_expr(testcase::Column::ProblemId, Expr::value(Value::Int(None)))
.filter(testcase::Column::ProblemId.eq(req.id))
.exec(&txn)
.instrument(info_span!("remove_child"))
.await?;

txn.commit().await.map_err(|_| Error::Retry)?;

if result.rows_affected == 0 {
return Err(Error::NotInDB);
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entity/user_contest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sea_orm::entity::prelude::*;
use super::*;
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user_contest")]
Expand Down

0 comments on commit 8edcb52

Please sign in to comment.