Optimize sign in: Identity #2061 (#2231)

* Add unique index to identity

* Conditional uniqueness validations on identities

* Remove redundant Identity specs
This commit is contained in:
Anna Buianova 2019-03-28 23:10:23 +03:00 committed by Ben Halpern
parent 9f74fd3794
commit 4b02fe87a4
4 changed files with 11 additions and 7 deletions

View file

@ -1,9 +1,8 @@
class Identity < ApplicationRecord
belongs_to :user
validates :uid, :provider, presence: true
validates :uid, uniqueness: { scope: :provider }
validates :provider, uniqueness: { scope: :uid }
validates :user_id, uniqueness: { scope: :provider }
validates :uid, uniqueness: { scope: :provider }, if: proc { |i| i.uid_changed? || i.provider_changed? }
validates :user_id, uniqueness: { scope: :provider }, if: proc { |i| i.user_id_changed? || i.provider_changed? }
validates :provider, inclusion: { in: %w[github twitter] }
serialize :auth_data_dump

View file

@ -0,0 +1,6 @@
class AddIndexesToIndentities < ActiveRecord::Migration[5.1]
def change
add_index :identities, %i[provider uid], unique: true
add_index :identities, %i[provider user_id], unique: true
end
end

View file

@ -1,5 +1,3 @@
# frozen_string_literal: true
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@ -12,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190318223433) do
ActiveRecord::Schema.define(version: 20190327090030) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -412,6 +410,8 @@ ActiveRecord::Schema.define(version: 20190318223433) do
t.string "uid"
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["provider", "uid"], name: "index_identities_on_provider_and_uid", unique: true
t.index ["provider", "user_id"], name: "index_identities_on_provider_and_user_id", unique: true
end
create_table "job_opportunities", force: :cascade do |t|

View file

@ -5,7 +5,6 @@ RSpec.describe Identity, type: :model do
it { is_expected.to validate_presence_of(:uid) }
it { is_expected.to validate_presence_of(:provider) }
it { is_expected.to validate_uniqueness_of(:uid).scoped_to(:provider) }
it { is_expected.to validate_uniqueness_of(:provider).scoped_to(:uid) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:provider) }
it { is_expected.to validate_inclusion_of(:provider).in_array(%w[github twitter]) }
it { is_expected.to serialize(:auth_data_dump) }