From e461ec504fd08a67a5b9376fbb19631807869371 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 18 Apr 2024 19:03:35 +0300 Subject: [PATCH] Add migration for existing volunteers --- ...144517_add_volunteer_team_to_volunteers.rb | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb diff --git a/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb b/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb new file mode 100644 index 0000000..e0eddde --- /dev/null +++ b/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb @@ -0,0 +1,30 @@ +class AddVolunteerTeamToVolunteers < ActiveRecord::Migration[7.1] + class VolunteerTeam < ActiveRecord::Base + has_and_belongs_to_many :volunteers + end + + class Volunteer < ActiveRecord::Base + belongs_to :volunteer_team + has_and_belongs_to_many :volunteer_teams + end + + def change + add_reference :volunteers, :volunteer_team, foreign_key: true + + reversible do |dir| + dir.up do + transaction do + Volunteer.all.find_each do |volunteer| + volunteer.update(volunteer_team: volunteer.volunteer_teams.first) + end + end + end + + dir.down do + # no-op + end + end + + change_column_null :volunteers, :volunteer_team_id, true + end +end