Skip to content

Commit

Permalink
Add unique index on saved annotations title
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed May 11, 2022
1 parent 6e54792 commit ce9fe83
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/saved_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# updated_at :datetime not null
#
class SavedAnnotation < ApplicationRecord
validates :title, presence: true
validates :title, presence: true, uniqueness: { scope: %i[user_id exercise_id course_id] }
validates :annotation_text, presence: true

belongs_to :user
Expand Down
4 changes: 3 additions & 1 deletion db/migrate/20220502130036_create_saved_annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ def change
create_table :saved_annotations do |t|
t.string :title, null: false
t.text :annotation_text, size: :medium
t.integer :user_id, foreign_key: true, type: :integer, null: false
t.references :user, foreign_key: true, type: :integer, null: false
t.references :exercise, foreign_key: { to_table: :activities }, type: :integer, null: false
t.references :course, foreign_key: true, type: :integer, null: false

t.timestamps

t.index [:title, :user_id, :exercise_id, :course_id], unique: true, name: "index_saved_annotations_title_uid_eid_cid"
end

change_table :annotations do |t|
Expand Down
3 changes: 3 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@
t.datetime "updated_at", null: false
t.index ["course_id"], name: "index_saved_annotations_on_course_id"
t.index ["exercise_id"], name: "index_saved_annotations_on_exercise_id"
t.index ["title", "user_id", "exercise_id", "course_id"], name: "index_saved_annotations_title_uid_eid_cid", unique: true
t.index ["user_id"], name: "index_saved_annotations_on_user_id"
end

create_table "score_items", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
Expand Down Expand Up @@ -539,6 +541,7 @@
add_foreign_key "rights_requests", "users"
add_foreign_key "saved_annotations", "activities", column: "exercise_id"
add_foreign_key "saved_annotations", "courses"
add_foreign_key "saved_annotations", "users"
add_foreign_key "score_items", "evaluation_exercises"
add_foreign_key "scores", "feedbacks"
add_foreign_key "scores", "score_items"
Expand Down

0 comments on commit ce9fe83

Please sign in to comment.