Skip to content

Commit

Permalink
prevent evaluation duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
SepsiLaszlo committed Jul 7, 2024
1 parent 99f865e commit 0c6cddf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/models/evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# id :bigint not null, primary key
# entry_request_status :string(255)
# explanation :text
# idx_in_semester :integer default(0), not null
# is_considered :boolean default(FALSE), not null
# justification :text not null
# last_evaulation :datetime
Expand All @@ -21,9 +22,10 @@
#
# Indexes
#
# ert_semester_idx (semester)
# next_version_idx (next_version)
# unique_idx (group_id,semester,next_version NULLS FIRST) UNIQUE
# ert_semester_idx (semester)
# index_evaluations_on_group_id_and_semester_and_idx_in_semester (group_id,semester,idx_in_semester) UNIQUE
# next_version_idx (next_version)
# unique_idx (group_id,semester,next_version NULLS FIRST) UNIQUE
#
# Foreign Keys
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AddUniqueIndexOnEvaluationSemester < ActiveRecord::Migration[6.0]
# We have multiple evaluations for the same group in the same semester from previous years,
# this prevents us from using a unique index on [:group_id, :semester].
# The idx_in_semester columns solves this by setting an arbitrary value where duplication is present and
# 0 for cases with no duplication.'
def up
add_column :evaluations, :idx_in_semester, :integer, default: 0, null: false

evaluation_groups = Evaluation.all.group_by { |evaluation| [evaluation.group_id, evaluation.semester] }
evaluation_groups.each do |id, evaluations|
evaluations.each.with_index do |evaluation, index|
evaluation.update!(idx_in_semester: index)
end
end

add_index :evaluations, [:group_id, :semester, :idx_in_semester], unique: true
end

def down
remove_index :evaluations, column: [:group_id, :semester, :idx_in_semester]
remove_column :evaluations, :idx_in_semester
end
end
13 changes: 11 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ CREATE TABLE public.evaluations (
next_version bigint,
explanation text,
optlock integer DEFAULT 0 NOT NULL,
is_considered boolean DEFAULT false NOT NULL
is_considered boolean DEFAULT false NOT NULL,
idx_in_semester integer DEFAULT 0 NOT NULL
);


Expand Down Expand Up @@ -1248,6 +1249,13 @@ CREATE INDEX idx_groups_grp_type ON public.groups USING btree (grp_type);
CREATE UNIQUE INDEX index_entry_requests_on_evaluation_id_and_user_id ON public.entry_requests USING btree (evaluation_id, user_id);


--
-- Name: index_evaluations_on_group_id_and_semester_and_idx_in_semester; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_evaluations_on_group_id_and_semester_and_idx_in_semester ON public.evaluations USING btree (group_id, semester, idx_in_semester);


--
-- Name: index_notifications_on_group_owner_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1754,6 +1762,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20221206081834'),
('20221209072919'),
('20221209100356'),
('20240419155504');
('20240419155504'),
('20240707094123');


2 changes: 1 addition & 1 deletion spec/requests/evalutations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

before(:each) do
evaluation.update!(id:2)
previous_evaluation = create(:evaluation, group: group, id: 1 )
previous_evaluation = create(:evaluation, group: group, id: 1, semester: '201720182')
Principle.create!(evaluation: previous_evaluation,
name: 'Previous principle',
type: Principle::WORK,
Expand Down

0 comments on commit 0c6cddf

Please sign in to comment.