diff --git a/app/models/ect_at_school_period.rb b/app/models/ect_at_school_period.rb index 7ba35a49..775664a3 100644 --- a/app/models/ect_at_school_period.rb +++ b/app/models/ect_at_school_period.rb @@ -15,6 +15,10 @@ class ECTAtSchoolPeriod < ApplicationRecord notify_email: true, allow_nil: true + validates :email, + email_uniqueness: true, + allow_nil: true + validates :started_on, presence: true diff --git a/db/migrate/20250120134632_add_unique_constraint_to_email.rb b/db/migrate/20250120134632_add_unique_constraint_to_email.rb new file mode 100644 index 00000000..8456539f --- /dev/null +++ b/db/migrate/20250120134632_add_unique_constraint_to_email.rb @@ -0,0 +1,6 @@ +class AddUniqueConstraintToEmail < ActiveRecord::Migration[8.0] + def change + add_index :ect_at_school_periods, :email, unique: true, where: "email IS NOT NULL", name: "unique_email_on_ect_at_school_periods" + add_index :mentor_at_school_periods, :email, unique: true, where: "email IS NOT NULL", name: "unique_email_on_mentor_at_school_periods" + end +end diff --git a/db/schema.rb b/db/schema.rb index 54404c2e..4930d554 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_01_20_095631) do +ActiveRecord::Schema[8.0].define(version: 2025_01_20_134632) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "pg_trgm" @@ -150,6 +150,7 @@ t.enum "working_pattern", enum_type: "working_pattern" t.string "email" t.index "teacher_id, ((finished_on IS NULL))", name: "index_ect_at_school_periods_on_teacher_id_finished_on_IS_NULL", unique: true, where: "(finished_on IS NULL)" + t.index ["email"], name: "unique_email_on_ect_at_school_periods", unique: true, where: "(email IS NOT NULL)" t.index ["school_id", "teacher_id", "started_on"], name: "index_ect_at_school_periods_on_school_id_teacher_id_started_on", unique: true t.index ["school_id"], name: "index_ect_at_school_periods_on_school_id" t.index ["teacher_id", "started_on"], name: "index_ect_at_school_periods_on_teacher_id_started_on", unique: true @@ -280,6 +281,7 @@ t.uuid "legacy_end_id" t.string "email" t.index "school_id, teacher_id, ((finished_on IS NULL))", name: "idx_on_school_id_teacher_id_finished_on_IS_NULL_dd7ee16a28", unique: true, where: "(finished_on IS NULL)" + t.index ["email"], name: "unique_email_on_mentor_at_school_periods", unique: true, where: "(email IS NOT NULL)" t.index ["school_id", "teacher_id", "started_on"], name: "idx_on_school_id_teacher_id_started_on_17d46e7783", unique: true t.index ["school_id"], name: "index_mentor_at_school_periods_on_school_id" t.index ["teacher_id", "started_on"], name: "index_mentor_at_school_periods_on_teacher_id_started_on", unique: true