scinote-web/db/migrate/20160205192344_migrate_organizations_structure.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2017-06-30 16:23:28 +08:00
class MigrateOrganizationsStructure < ActiveRecord::Migration[4.2]
2016-02-12 23:52:43 +08:00
def up
# Update estimated size of all assets
Asset.includes(:asset_text_datum).find_each do |asset|
asset.update_estimated_size
asset.update(file_present: true)
end
2017-01-26 23:30:31 +08:00
# Calculate teams' taken space
Team.find_each do |team|
team.calculate_space_taken
team.save
2016-02-12 23:52:43 +08:00
end
2017-01-26 23:30:31 +08:00
# Finally, the trickiest task: Re-define teams
demo_team = Team.find_by(name: "Demo team")
if demo_team
demo_team.user_teams.each do |uo|
2016-02-12 23:52:43 +08:00
uo.destroy
end
end
2017-01-26 23:30:31 +08:00
Team.find_each do |team|
user = team.users.first
team.update(created_by: user)
2016-02-12 23:52:43 +08:00
end
2017-01-26 23:30:31 +08:00
UserTeam.find_each do |uo|
2016-02-12 23:52:43 +08:00
uo.update(role: 2)
end
end
def down
2017-01-26 23:30:31 +08:00
# We cannot re-assign users to demo team or re-update
# their previous user-team roles
2016-02-12 23:52:43 +08:00
2017-01-26 23:30:31 +08:00
# But we can remove created_by field from teams
Team.find_each do |team|
team.update(created_by: nil)
2016-02-12 23:52:43 +08:00
end
2017-01-26 23:30:31 +08:00
# Resetting calculated assets & teams' space
2016-02-12 23:52:43 +08:00
# to 0 doesn't make much sense even when downgrading migration
end
end