From ffacc9e3ac38c8ab44173dd0798a0a92400e6ed9 Mon Sep 17 00:00:00 2001 From: Vyacheslav1557 Date: Sat, 8 Mar 2025 19:01:45 +0500 Subject: [PATCH] fix(tester): fix schema&queries --- internal/tester/repository/pg_contests_repository.go | 9 ++++++--- migrations/20240727123308_initial.sql | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/tester/repository/pg_contests_repository.go b/internal/tester/repository/pg_contests_repository.go index be6038b..d00074c 100644 --- a/internal/tester/repository/pg_contests_repository.go +++ b/internal/tester/repository/pg_contests_repository.go @@ -67,13 +67,16 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error { return nil } -const addTaskQuery = "INSERT INTO tasks (problem_id, contest_id, position) VALUES (?, ?,COALESCE(SELECT MAX(position) FROM task WHERE contest_id = ? ,0) + 1) RETURNING id" +const addTaskQuery = `INSERT INTO tasks (problem_id, contest_id, position) +VALUES (?, ?, COALESCE((SELECT MAX(position) FROM tasks WHERE contest_id = ?), 0) + 1) +RETURNING id +` -func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, problem_id int32) (int32, error) { +func (r *ContestRepository) AddTask(ctx context.Context, contestId int32, problemId int32) (int32, error) { const op = "ContestRepository.AddTask" query := r.db.Rebind(addTaskQuery) - rows, err := r.db.QueryxContext(ctx, query, problem_id, contestId, contestId) + rows, err := r.db.QueryxContext(ctx, query, problemId, contestId, contestId) if err != nil { return 0, handlePgErr(err, op) } diff --git a/migrations/20240727123308_initial.sql b/migrations/20240727123308_initial.sql index 0ca2e5b..d8058da 100644 --- a/migrations/20240727123308_initial.sql +++ b/migrations/20240727123308_initial.sql @@ -5,7 +5,7 @@ CREATE FUNCTION updated_at_update() RETURNS TRIGGER $$ BEGIN NEW.updated_at = NOW(); -RETURN NEW; + RETURN NEW; END; $$; @@ -54,8 +54,8 @@ EXECUTE PROCEDURE updated_at_update(); CREATE TABLE IF NOT EXISTS tasks ( id serial NOT NULL, - problem_id integer NOT NULL REFERENCES problems (id), - contest_id integer NOT NULL REFERENCES contests (id), + problem_id integer REFERENCES problems (id) ON DELETE SET NULL, + contest_id integer REFERENCES contests (id) ON DELETE SET NULL, position integer NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), @@ -93,8 +93,8 @@ EXECUTE PROCEDURE updated_at_update(); CREATE TABLE IF NOT EXISTS solutions ( id serial NOT NULL, - task_id integer NOT NULL REFERENCES tasks (id), - participant_id integer NOT NULL REFERENCES participants (id), + task_id integer REFERENCES tasks (id) ON DELETE SET NULL, + participant_id integer REFERENCES participants (id) ON DELETE SET NULL, solution varchar(1048576) NOT NULL, state integer NOT NULL DEFAULT 1, score integer NOT NULL,