Skip to content

Commit

Permalink
Merge pull request #1 from gParshav/changeSchemaMigrations
Browse files Browse the repository at this point in the history
Updating migration files for changeShema
  • Loading branch information
ameghana authored Mar 22, 2024
2 parents d0066a0 + 34bca95 commit acefb1b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 39 deletions.

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions db/migrate/20240316201609_populate_participant_id.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class RenameTeamsUsersToTeamsParticipants < ActiveRecord::Migration[5.1]
def change
# Check if the 'teams_users' table exists and 'teams_participants' does not exist
if table_exists?(:teams_users) && !table_exists?(:teams_participants)
rename_table :teams_users, :teams_participants
elsif table_exists?(:teams_participants)
puts "'teams_participants' table already exists. Migration skipped."
else
puts "'teams_users' table does not exist. Migration skipped."
end
end

def down
# Check if the 'teams_participants' table exists and 'teams_users' does not exist
if table_exists?(:teams_participants) && !table_exists?(:teams_users)
rename_table :teams_participants, :teams_users
elsif table_exists?(:teams_users)
puts "'teams_users' table already exists. Reverse migration skipped."
else
puts "'teams_participants' table does not exist. Reverse migration skipped."
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddParticipantIdToTeamsParticipants < ActiveRecord::Migration[5.1]
def change
unless column_exists?(:teams_participants, :participant_id)
add_column :teams_participants, :participant_id, :integer
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PopulateParticipantIdInTeamsParticipants < ActiveRecord::Migration[5.1]
def change
TeamsParticipant.find_each do |tp|
next unless Team.exists?(tp.team_id)

team = Team.find(tp.team_id)
# Assuming `parent_id` in `teams` refers to an `assignment_id`
assignment_id = team.parent_id

# Find a participant with the same `user_id` and `parent_id` (assignment)
participant = Participant.find_by(user_id: tp.user_id, parent_id: assignment_id)

if participant
# Update the teams_participants record with the found participant's ID
tp.update_column(:participant_id, participant.id)
else
# Handle cases where no participant is found (e.g., log or take corrective action)
puts "No participant found for TeamsParticipant with ID: #{tp.id}"
end
end
end

def down
# Optionally, clear the participant_id column if rolling back
TeamsParticipant.update_all(participant_id: nil)
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20240316195618) do
ActiveRecord::Schema.define(version: 20240322002855) do

create_table "account_requests", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name"
Expand Down

0 comments on commit acefb1b

Please sign in to comment.