Skip to content

Commit

Permalink
Fix bug children's foreign key not deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Aug 21, 2024
1 parent a499cf3 commit acf6613
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
22 changes: 16 additions & 6 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;

use crate::entity::{
contest::{Paginator, *},
*,
problem, *,
};
use sea_orm::sea_query::Expr;

use grpc::backend::contest_server::*;

Expand Down Expand Up @@ -238,6 +238,8 @@ 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 @@ -246,12 +248,20 @@ 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 {
Err(Error::NotInDB)
} else {
tracing::info!(counter.contest = -1, id = req.id);
Ok(())
return Err(Error::NotInDB);
}
tracing::info!(counter.contest = -1, id = req.id);
Ok(())
})
.await
.with_grpc()
Expand Down
20 changes: 16 additions & 4 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::*;
use grpc::backend::problem_server::*;
use sea_orm::sea_query::Expr;

use crate::entity::{contest, problem::Paginator, problem::*};
use crate::entity::{contest, problem::Paginator, problem::*, testcase};

impl<'a> From<WithAuth<'a, Model>> for ProblemFullInfo {
fn from(value: WithAuth<'a, Model>) -> Self {
Expand Down Expand Up @@ -191,17 +192,28 @@ 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(self.db.deref())
.exec(&txn)
.instrument(info_span!("remove").or_current())
.await
.map_err(Into::<Error>::into)?;
.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);
}
tracing::info!(count.problem = -1, id = req.id);
Ok(())
})
.await
Expand Down
7 changes: 4 additions & 3 deletions backend/src/util/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub enum Error {
Unreachable(&'static str),
#[error("Number too large(or small)")]
NumberTooLarge,
// #[error("Buffer `{0}` too large")]
// BufferTooLarge(&'static str),
#[error("`{0}` Already exist")]
AlreadyExist(&'static str),
#[error("require permission `{0}`")]
Expand All @@ -47,6 +45,8 @@ pub enum Error {
Judger(#[from] judger::Error),
#[error("token error: `{0}`")]
Token(#[from] token::Error),
#[error("retry later")]
Retry,
}

impl From<sea_orm::DbErr> for Error {
Expand Down Expand Up @@ -103,14 +103,15 @@ impl From<Error> for Status {
Error::Image(x) => report_internal!(error, "{}", x),
Error::Judger(x) => x.into(),
Error::Token(x) => x.into(),
Error::Retry => Status::aborted("Should retry"),
}
}
}

/// Tracing information for error
///
/// useful to log the tracing information to client
/// without exposing the server's internal erro
/// without exposing the server's internal error
pub struct Tracing {
trace_id: TraceId,
span_id: SpanId,
Expand Down

0 comments on commit acf6613

Please sign in to comment.