Skip to content

Commit 1e05e35

Browse files
maebealeclaude
andauthored
Reconcile seeded FormBuilders by id and prune duplicates (#1571)
The canonical FormBuilders are referenced by fixed id elsewhere in the app, but the seed created them keyed on name+windows_type. When a WindowsType was renamed/recreated, reseeding spawned duplicate builders at fresh ids. Key the seed on the fixed id and reconcile name + windows_type on every run, then prune exact-name duplicates outside the pinned ids that own no forms — so reseeding is idempotent and form-bearing records are never touched. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 396b3fc commit 1e05e35

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

db/seeds.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,28 @@ def find_or_create_by_name!(klass, name, **attrs, &block)
274274
.first_or_create!(legacy_id: 3, short_name: "Combined")
275275

276276
puts "Creating FormBuilders…"
277-
FormBuilder.where(name: "Adult Monthly Report", windows_type: adult_type).first_or_create!(id: 4)
278-
FormBuilder.where(name: "Adult Workshop Log", windows_type: adult_type).first_or_create!(id: 3)
279-
FormBuilder.where(name: "Children's Monthly Report", windows_type: childrens_type).first_or_create!(id: 2)
280-
FormBuilder.where(name: "Children's Workshop Log", windows_type: childrens_type).first_or_create!(id: 1)
281-
FormBuilder.where(name: "Share a Story", windows_type: combined_type).first_or_create!(id: 7)
282-
FormBuilder.where(name: "Family Workshop Log", windows_type: combined_type).first_or_create!(id: 5)
277+
# Keyed on the fixed id (the app references these ids), reconciling name and
278+
# windows_type on every run so reseeding survives WindowsType records being
279+
# recreated (e.g. after a name change).
280+
form_builders = {
281+
4 => [ "Adult Monthly Report", adult_type ],
282+
3 => [ "Adult Workshop Log", adult_type ],
283+
2 => [ "Children's Monthly Report", childrens_type ],
284+
1 => [ "Children's Workshop Log", childrens_type ],
285+
7 => [ "Share a Story", combined_type ],
286+
5 => [ "Family Workshop Log", combined_type ]
287+
}
288+
form_builders.each do |id, (name, windows_type)|
289+
FormBuilder.find_or_initialize_by(id: id).update!(name: name, windows_type: windows_type)
290+
end
291+
292+
# Prune duplicate canonical form builders left by older seed versions that
293+
# created this set at auto-increment ids. Only exact name duplicates outside the
294+
# pinned ids with no dependent forms are removed, so form-bearing records (prod
295+
# builders are seeded via migrations and own forms) are never touched.
296+
FormBuilder.where(name: form_builders.values.map(&:first))
297+
.where.not(id: form_builders.keys)
298+
.each { |duplicate| duplicate.destroy! if duplicate.forms.empty? }
283299

284300
puts "Creating OrganizationStatuses…"
285301
OrganizationStatus::ORGANIZATION_STATUSES.each do |status|

0 commit comments

Comments
 (0)