GRN2-xx: Switch the relation between users and roles to make queries cleaner and faster (#1299)

* First steps

* Fixes in account creation flow

* Fixed most testcases

* more test fixes

* Fixed more test cases

* Passing tests and rubocop

* Added rake task to remove rooms
This commit is contained in:
Ahmad Farhat
2020-05-06 15:23:28 -04:00
committed by GitHub
parent 8f454cad0e
commit 467947f1b5
37 changed files with 262 additions and 402 deletions

View File

@ -19,7 +19,7 @@ class RolifyCreateRoles < ActiveRecord::Migration[5.0]
add_index(:users_roles, [:user_id, :role_id])
User.all.each do |user|
user.add_role(:user) if user.roles.blank?
user.set_role(:user) if user.roles.blank?
end
end
end

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
class MigrationProduct < ActiveRecord::Base
self.table_name = :users
end
class SubMigrationProduct < ActiveRecord::Base
self.table_name = :roles
end
class AddRoleIdToUsers < ActiveRecord::Migration[5.2]
def change
reversible do |dir|
dir.up do
add_reference :users, :role, index: true
MigrationProduct.where(role_id: nil).each do |user|
highest_role = SubMigrationProduct.joins("INNER JOIN users_roles ON users_roles.role_id = roles.id")
.where("users_roles.user_id = '#{user.id}'").min_by(&:priority).id
user.update_attributes(role_id: highest_role) unless highest_role.nil?
end
end
dir.down do
remove_reference :users, :role, index: true
end
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_01_30_144841) do
ActiveRecord::Schema.define(version: 2020_04_13_150518) do
create_table "features", force: :cascade do |t|
t.integer "setting_id"
@ -120,11 +120,13 @@ ActiveRecord::Schema.define(version: 2020_01_30_144841) do
t.string "activation_digest"
t.datetime "activated_at"
t.boolean "deleted", default: false, null: false
t.integer "role_id"
t.index ["created_at"], name: "index_users_on_created_at"
t.index ["deleted"], name: "index_users_on_deleted"
t.index ["email"], name: "index_users_on_email"
t.index ["password_digest"], name: "index_users_on_password_digest", unique: true
t.index ["provider"], name: "index_users_on_provider"
t.index ["role_id"], name: "index_users_on_role_id"
t.index ["room_id"], name: "index_users_on_room_id"
end