-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99f865e
commit 0c6cddf
Showing
4 changed files
with
40 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
db/migrate/20240707094123_add_unique_index_on_evaluation_semester.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters