From 8edcb52e10361d803f728e509d205db3ca98c59b Mon Sep 17 00:00:00 2001 From: "Eason(G Ray)" <30045503+Eason0729@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:09:29 +0800 Subject: [PATCH] update entity to use on delete for foreign key --- backend/migration/src/lib.rs | 5 +- .../src/m20231207_000001_create_table.rs | 50 ++++++++++++------- backend/src/endpoint/contest.rs | 11 ---- backend/src/endpoint/problem.rs | 13 +---- backend/src/entity/user_contest.rs | 2 +- 5 files changed, 39 insertions(+), 42 deletions(-) diff --git a/backend/migration/src/lib.rs b/backend/migration/src/lib.rs index 44c939b..1b2e95c 100644 --- a/backend/migration/src/lib.rs +++ b/backend/migration/src/lib.rs @@ -8,6 +8,9 @@ pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - 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), + ] } } diff --git a/backend/migration/src/m20231207_000001_create_table.rs b/backend/migration/src/m20231207_000001_create_table.rs index 4131c0a..d67aa05 100644 --- a/backend/migration/src/m20231207_000001_create_table.rs +++ b/backend/migration/src/m20231207_000001_create_table.rs @@ -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) @@ -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()) @@ -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) @@ -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) @@ -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) @@ -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()) @@ -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( @@ -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) @@ -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) diff --git a/backend/src/endpoint/contest.rs b/backend/src/endpoint/contest.rs index a31f7ef..87241d6 100644 --- a/backend/src/endpoint/contest.rs +++ b/backend/src/endpoint/contest.rs @@ -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()? @@ -248,15 +246,6 @@ impl Contest for ArcServer { .await .map_err(Into::::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); } diff --git a/backend/src/endpoint/problem.rs b/backend/src/endpoint/problem.rs index 56f15f8..4d08581 100644 --- a/backend/src/endpoint/problem.rs +++ b/backend/src/endpoint/problem.rs @@ -192,24 +192,13 @@ impl Problem for ArcServer { async fn remove(&self, req: Request) -> Result, 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); } diff --git a/backend/src/entity/user_contest.rs b/backend/src/entity/user_contest.rs index fa1000e..3183411 100644 --- a/backend/src/entity/user_contest.rs +++ b/backend/src/entity/user_contest.rs @@ -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")]