-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from gParshav/changeSchemaMigrations
Updating migration files for changeShema
- Loading branch information
Showing
7 changed files
with
58 additions
and
39 deletions.
There are no files selected for viewing
5 changes: 0 additions & 5 deletions
5
db/migrate/20240316195618_rename_teams_users_to_teams_participants.rb
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
db/migrate/20240316201208_add_participant_id_to_teams_participants.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
db/migrate/20240322002343_rename_teams_users_to_teams_participants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
db/migrate/20240322002444_add_participant_id_to_teams_participants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
27 changes: 27 additions & 0 deletions
27
db/migrate/20240322002855_populate_participant_id_in_teams_participants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters