]> git.openstreetmap.org Git - rails.git/blob - db/migrate/046_alter_user_roles_and_blocks.rb
Merge remote-tracking branch 'upstream/pull/3177'
[rails.git] / db / migrate / 046_alter_user_roles_and_blocks.rb
1 class AlterUserRolesAndBlocks < ActiveRecord::Migration[4.2]
2   class UserRole < ApplicationRecord
3   end
4
5   def self.up
6     # the initial granter IDs can be "self" - there are none of these
7     # in the current live DB, but there may be some in people's own local
8     # copies.
9     add_column :user_roles, :granter_id, :bigint
10     UserRole.update_all("granter_id = user_id")
11     change_column :user_roles, :granter_id, :bigint, :null => false
12     add_foreign_key :user_roles, :users, :column => :granter_id, :name => "user_roles_granter_id_fkey"
13
14     # make sure that [user_id, role] is unique
15     add_index :user_roles, [:user_id, :role], :name => "user_roles_id_role_unique", :unique => true
16
17     # change the user_blocks to have a creator_id rather than moderator_id
18     rename_column :user_blocks, :moderator_id, :creator_id
19
20     # change the "end_at" column to the more grammatically correct "ends_at"
21     rename_column :user_blocks, :end_at, :ends_at
22   end
23
24   def self.down
25     rename_column :user_blocks, :ends_at, :end_at
26     rename_column :user_blocks, :creator_id, :moderator_id
27     remove_index :user_roles, :name => "user_roles_id_role_unique"
28     remove_column :user_roles, :granter_id
29   end
30 end