Chore: Enable Users to create multiple accounts (#440)
Addresses: #402 - migrations to split roles and other attributes from users table - make changes in code to accommodate this change Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
42
db/migrate/20200121190901_create_account_users.rb
Normal file
42
db/migrate/20200121190901_create_account_users.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class CreateAccountUsers < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :account_users do |t|
|
||||
t.references :account, foreign_key: true, index: true
|
||||
t.references :user, foreign_key: true, index: true
|
||||
t.integer :role, default: 0
|
||||
t.bigint :inviter_id
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
migrate_to_account_users
|
||||
|
||||
remove_column :users, :account_id, :bigint
|
||||
remove_column :users, :role, :integer
|
||||
remove_column :users, :inviter_id, :bigint
|
||||
end
|
||||
|
||||
def migrate_to_account_users
|
||||
::User.find_in_batches.each do |users|
|
||||
users.each do |user|
|
||||
account_user = ::AccountUser.find_by(account_id: user.account_id, user_id: user.id, role: user.role)
|
||||
|
||||
notification_setting = ::NotificationSetting.find_by(account_id: user.account_id, user_id: user.id)
|
||||
selected_email_flags = notification_setting.selected_email_flags
|
||||
notification_setting.destroy!
|
||||
|
||||
next if account_user.present?
|
||||
|
||||
::AccountUser.create!(
|
||||
account_id: user.account_id,
|
||||
user_id: user.id,
|
||||
role: user[:role], # since we are overriding role method, lets fetch value from attribute
|
||||
inviter_id: user.inviter_id
|
||||
)
|
||||
|
||||
updated_notification_setting = ::NotificationSetting.find_by(account_id: user.account_id, user_id: user.id)
|
||||
updated_notification_setting.selected_email_flags = selected_email_flags
|
||||
updated_notification_setting.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddUniquenessConstraintToAccountUsers < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_index :account_users, [:account_id, :user_id], unique: true, name: 'uniq_user_id_per_account_id'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user