Skip to content

Commit 50b653a

Browse files
committed
Fix invalid schools in our database
We've added validation that makes existing schools we already have in our database. This means that that updates to those schools may fail and cause errors. Generally, when we add validation we should make sure it doesn't invalidate existing records. Here I've done two things: + Add `on: :create` to the new validations where schools have some invalid records + Add `unless: :rejected` to items with a unique condition where new signups may overlap with rejected schools. I've documented the errors in this sheet[1]. Some of the optional invalid codes could be fixed manually by nulling them if they aren't required, but since that can't be done for the required codes, I've handled them all the same way. https://docs.google.com/spreadsheets/d/1ZwkCrRkVeUcgUaDuDxSZljlhntmK1Az8J5--Y0bSBrI/edit?gid=0#gid=0
1 parent ec60963 commit 50b653a

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

app/models/school.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ class School < ApplicationRecord
1616
validates :website, presence: true, format: { with: VALID_URL_REGEX, message: I18n.t('validations.school.website') }
1717
validates :address_line_1, presence: true
1818
validates :municipality, presence: true
19-
validates :administrative_area, presence: true
19+
validates :administrative_area, presence: true, on: :create
2020
validates :postal_code, presence: true
2121
validates :country_code, presence: true, inclusion: { in: ISO3166::Country.codes }
2222
validates :reference,
2323
uniqueness: { conditions: -> { where(rejected_at: nil) }, case_sensitive: false, allow_blank: true, message: I18n.t('validations.school.reference_urn_exists') },
2424
format: { with: /\A\d{5,6}\z/, allow_nil: true, message: I18n.t('validations.school.reference') },
25-
if: :united_kingdom?
25+
if: :united_kingdom?, on: :create, unless: :rejected?
2626
validates :district_nces_id,
2727
format: { with: /\A\d{7}\z/, allow_nil: true, message: I18n.t('validations.school.district_nces_id') },
28-
if: :united_states?
28+
if: :united_states?, on: :create
2929
validates :district_name, presence: true, if: :united_states?
3030
validates :school_roll_number,
3131
uniqueness: { conditions: -> { where(rejected_at: nil) }, case_sensitive: false, allow_blank: true, message: I18n.t('validations.school.school_roll_number_exists') },
3232
format: { with: /\A[0-9]+[A-Z]+\z/, allow_nil: true, message: I18n.t('validations.school.school_roll_number') },
33-
presence: true,
34-
if: :ireland?
33+
presence: true, on: :create, if: :ireland?, unless: :rejected?
3534
validates :creator_id,
3635
presence: true,
3736
uniqueness: {
3837
conditions: -> { where(rejected_at: nil) }
39-
}
38+
}, unless: :rejected?
4039
validates :creator_agree_authority, presence: true, acceptance: true
4140
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
4241
validates :creator_agree_responsible_safeguarding, presence: true, acceptance: true

spec/models/school_spec.rb

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@
135135
expect(another_school.errors[:creator_id]).to include('has already been taken')
136136
end
137137

138+
it 'schools can re-use creator_ids if the original school is rejected' do
139+
rejected_school = create(:school, creator_id: SecureRandom.uuid, rejected_at: Time.zone.now)
140+
other_school = build(:school, creator_id: rejected_school.creator_id)
141+
expect(rejected_school).to be_valid
142+
expect(other_school).to be_valid
143+
end
144+
138145
it 'rejects a badly formed url for website' do
139146
school.website = 'http://.example.com'
140147
expect(school).not_to be_valid
@@ -184,20 +191,20 @@
184191
expect(school).to be_valid
185192
end
186193

187-
it 'rejects a reference with non-digit characters' do
188-
school.reference = 'URN-123'
194+
it 'rejects new schools with a reference containing non-digit characters' do
195+
school = build(:school, reference: 'URN-123')
189196
expect(school).not_to be_valid
190197
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
191198
end
192199

193-
it 'rejects a reference with too few digits' do
194-
school.reference = '1234'
200+
it 'rejects new schools with a reference that has too few digits' do
201+
school = build(:school, reference: '1234')
195202
expect(school).not_to be_valid
196203
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
197204
end
198205

199-
it 'rejects a reference with too many digits' do
200-
school.reference = '1234567'
206+
it 'rejects new schools with a reference that has too many digits' do
207+
school = build(:school, reference: '1234567')
201208
expect(school).not_to be_valid
202209
expect(school.errors[:reference]).to include('must be 5-6 digits (e.g., 100000)')
203210
end
@@ -255,14 +262,14 @@
255262
expect(us_school).to be_valid
256263
end
257264

258-
it 'rejects a district_nces_id with non-digit characters' do
259-
us_school.district_nces_id = '010000A'
265+
it 'rejects new schools with a district_nces_id containing non-digit characters' do
266+
us_school = build(:school, country_code: 'US', district_nces_id: '010000A')
260267
expect(us_school).not_to be_valid
261268
expect(us_school.errors[:district_nces_id]).to include('must be 7 digits (e.g., 0100000)')
262269
end
263270

264-
it 'rejects a district_nces_id with wrong length' do
265-
us_school.district_nces_id = '123456'
271+
it 'rejects new schools with a district_nces_id with wrong length' do
272+
us_school = build(:school, country_code: 'US', district_nces_id: '123456')
266273
expect(us_school).not_to be_valid
267274
expect(us_school.errors[:district_nces_id]).to include('must be 7 digits (e.g., 0100000)')
268275
end
@@ -281,8 +288,8 @@
281288
expect(school).to be_valid
282289
end
283290

284-
it 'requires school_roll_number for Ireland schools' do
285-
ireland_school.school_roll_number = nil
291+
it 'requires school_roll_number for Ireland for new schools' do
292+
ireland_school = build(:school, country_code: 'IE', school_roll_number: nil)
286293
expect(ireland_school).not_to be_valid
287294
expect(ireland_school.errors[:school_roll_number]).to include("can't be blank")
288295
end
@@ -308,20 +315,20 @@
308315
expect(ireland_school).to be_valid
309316
end
310317

311-
it 'rejects a school_roll_number with only numbers' do
312-
ireland_school.school_roll_number = '01572'
318+
it 'rejects new schools with a school_roll_number that contains only numbers' do
319+
ireland_school = build(:school, country_code: 'IE', school_roll_number: '01572')
313320
expect(ireland_school).not_to be_valid
314321
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
315322
end
316323

317-
it 'rejects a school_roll_number with only letters' do
318-
ireland_school.school_roll_number = 'ABCDE'
324+
it 'rejects new schools with a school_roll_number containing only letters' do
325+
ireland_school = build(:school, country_code: 'IE', school_roll_number: 'ABCDE')
319326
expect(ireland_school).not_to be_valid
320327
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
321328
end
322329

323-
it 'rejects a school_roll_number with special characters' do
324-
ireland_school.school_roll_number = '01572-D'
330+
it 'rejects new schools with a school_roll_number containing special characters' do
331+
ireland_school = build(:school, country_code: 'IE', school_roll_number: '01572-D')
325332
expect(ireland_school).not_to be_valid
326333
expect(ireland_school.errors[:school_roll_number]).to include('must be numbers followed by letters (e.g., 01572D)')
327334
end
@@ -356,8 +363,8 @@
356363
expect(school).not_to be_valid
357364
end
358365

359-
it 'requires an administrative_area' do
360-
school.administrative_area = ' '
366+
it 'requires an administrative_area for new schools' do
367+
school = build(:school, administrative_area: ' ')
361368
expect(school).not_to be_valid
362369
end
363370

0 commit comments

Comments
 (0)