-
Notifications
You must be signed in to change notification settings - Fork 5
Change creator_id to only need to be unique for schools where rejecte… #742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
400d122
aa91052
c84d839
d5b80e7
9c5b01c
4e3a399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,11 @@ class School < ApplicationRecord | |
| format: { with: /\A[0-9]+[A-Z]+\z/, allow_nil: true, message: I18n.t('validations.school.school_roll_number') }, | ||
| presence: true, | ||
| if: :ireland? | ||
| validates :creator_id, presence: true, uniqueness: true | ||
| validates :creator_id, | ||
| presence: true, | ||
| uniqueness: { | ||
| conditions: -> { where(rejected_at: nil) } | ||
| } | ||
|
jamiebenstead marked this conversation as resolved.
Comment on lines
+34
to
+38
|
||
| validates :creator_agree_authority, presence: true, acceptance: true | ||
| validates :creator_agree_terms_and_conditions, presence: true, acceptance: true | ||
| validates :creator_agree_responsible_safeguarding, presence: true, acceptance: true | ||
|
jamiebenstead marked this conversation as resolved.
|
||
|
|
@@ -53,7 +57,7 @@ class School < ApplicationRecord | |
| after_create :generate_code!, if: -> { FeatureFlags.immediate_school_onboarding? } | ||
|
|
||
| def self.find_for_user!(user) | ||
| school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id) | ||
| school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id, rejected_at: nil) | ||
| raise ActiveRecord::RecordNotFound unless school | ||
|
Comment on lines
59
to
61
|
||
|
|
||
| school | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||
| class UpdateCreatorIdIndexOnSchools < ActiveRecord::Migration[7.2] | ||||||||||||||||||||
| def up | ||||||||||||||||||||
| remove_index :schools, name: "index_schools_on_creator_id" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| add_index :schools, | ||||||||||||||||||||
| :creator_id, | ||||||||||||||||||||
| unique: true, | ||||||||||||||||||||
| where: "rejected_at IS NULL", | ||||||||||||||||||||
| name: "index_schools_on_creator_id_active_only" | ||||||||||||||||||||
|
||||||||||||||||||||
| name: "index_schools_on_creator_id_active_only" | |
| name: "index_schools_on_creator_id" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a preference either way. I don't see any problem with the more descriptive name as you only tend to refer to index names from within other migrations. I think there is a limit to the name length but this would likely be within it.
I would choose what you think is clearer.
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The down migration recreates a global unique index on creator_id. After this change is deployed, the table may legitimately contain multiple rows with the same creator_id (as long as only one is active), so rolling back would fail when re-adding this index. Consider marking the migration as irreversible, or adding a rollback strategy (e.g., clean up/merge duplicate rows or raise with a clear message before attempting to add the unique index).
| remove_index :schools, name: "index_schools_on_creator_id_active_only" | |
| add_index :schools, | |
| :creator_id, | |
| unique: true, | |
| name: "index_schools_on_creator_id" | |
| raise ActiveRecord::IrreversibleMigration, | |
| "Cannot safely restore global unique index on creator_id because " \ | |
| "multiple rows per creator_id may exist after this migration." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This risk here is that if we did roll back, and the remove_index succeeded, but the add_index didn't, we would could get duplicates.
This is a small risk as it only applies if we decide to manually roll back the code and migrations so I don't have a strong preference as to if it's worth changing or not - up to you.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.