From 4b02fe87a428455de3387b7dcf802ed83f803209 Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Thu, 28 Mar 2019 23:10:23 +0300 Subject: [PATCH] Optimize sign in: Identity #2061 (#2231) * Add unique index to identity * Conditional uniqueness validations on identities * Remove redundant Identity specs --- app/models/identity.rb | 5 ++--- db/migrate/20190327090030_add_indexes_to_indentities.rb | 6 ++++++ db/schema.rb | 6 +++--- spec/models/identity_spec.rb | 1 - 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20190327090030_add_indexes_to_indentities.rb diff --git a/app/models/identity.rb b/app/models/identity.rb index 98c5929ef..ed5a2b082 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -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 diff --git a/db/migrate/20190327090030_add_indexes_to_indentities.rb b/db/migrate/20190327090030_add_indexes_to_indentities.rb new file mode 100644 index 000000000..b7fdfec03 --- /dev/null +++ b/db/migrate/20190327090030_add_indexes_to_indentities.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1399d61c3..7d0750938 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/models/identity_spec.rb b/spec/models/identity_spec.rb index d3a90db5f..d4ffa1ca3 100644 --- a/spec/models/identity_spec.rb +++ b/spec/models/identity_spec.rb @@ -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) }