You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
919 B

  1. # frozen_string_literal: true
  2. class MigrateRoles < ActiveRecord::Migration[5.2]
  3. disable_ddl_transaction!
  4. class UserRole < ApplicationRecord; end
  5. class User < ApplicationRecord; end
  6. def up
  7. load Rails.root.join('db', 'seeds', '03_roles.rb')
  8. owner_role = UserRole.find_by(name: 'Owner')
  9. moderator_role = UserRole.find_by(name: 'Moderator')
  10. User.where(admin: true).in_batches.update_all(role_id: owner_role.id)
  11. User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id)
  12. end
  13. def down
  14. admin_role = UserRole.find_by(name: 'Admin')
  15. owner_role = UserRole.find_by(name: 'Owner')
  16. moderator_role = UserRole.find_by(name: 'Moderator')
  17. User.where(role_id: [admin_role.id, owner_role.id]).in_batches.update_all(admin: true) if admin_role
  18. User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role
  19. end
  20. end