From 46afcd934836f7ed914cfe339b58f77a2df3daf2 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 7 Dec 2021 18:20:47 +0530 Subject: [PATCH] fix: Index custom attribute uniqueness on account (#3516) - Move custom attribute uniqueness validation to account --- app/models/custom_attribute_definition.rb | 4 ++-- ...e_attribute_key_index_on_custom_attribute_definition.rb | 7 +++++++ db/schema.rb | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20211207113102_remove_attribute_key_index_on_custom_attribute_definition.rb diff --git a/app/models/custom_attribute_definition.rb b/app/models/custom_attribute_definition.rb index 94a41beea..cc8590f45 100644 --- a/app/models/custom_attribute_definition.rb +++ b/app/models/custom_attribute_definition.rb @@ -16,7 +16,7 @@ # # Indexes # -# attribute_key_model_index (attribute_key,attribute_model) UNIQUE +# attribute_key_index (attribute_key,account_id) UNIQUE # index_custom_attribute_definitions_on_account_id (account_id) # class CustomAttributeDefinition < ApplicationRecord @@ -25,7 +25,7 @@ class CustomAttributeDefinition < ApplicationRecord validates :attribute_key, presence: true, - uniqueness: { scope: :attribute_model } + uniqueness: { scope: :account_id } validates :attribute_display_type, presence: true validates :attribute_model, presence: true diff --git a/db/migrate/20211207113102_remove_attribute_key_index_on_custom_attribute_definition.rb b/db/migrate/20211207113102_remove_attribute_key_index_on_custom_attribute_definition.rb new file mode 100644 index 000000000..9beed0de6 --- /dev/null +++ b/db/migrate/20211207113102_remove_attribute_key_index_on_custom_attribute_definition.rb @@ -0,0 +1,7 @@ +class RemoveAttributeKeyIndexOnCustomAttributeDefinition < ActiveRecord::Migration[6.1] + def change + remove_index :custom_attribute_definitions, [:attribute_key, :account_id], name: 'attribute_key_model_index', + if_exists: true + add_index :custom_attribute_definitions, [:attribute_key, :account_id], unique: true, name: 'attribute_key_index', if_not_exists: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 14526629d..5232229ad 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_11_29_120040) do +ActiveRecord::Schema.define(version: 2021_12_07_113102) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -368,7 +368,7 @@ ActiveRecord::Schema.define(version: 2021_11_29_120040) do t.text "attribute_description" t.jsonb "attribute_values", default: [] t.index ["account_id"], name: "index_custom_attribute_definitions_on_account_id" - t.index ["attribute_key", "attribute_model"], name: "attribute_key_model_index", unique: true + t.index ["attribute_key", "account_id"], name: "attribute_key_index", unique: true end create_table "custom_filters", force: :cascade do |t|