fix: Index custom attribute uniqueness on account (#3516)

- Move custom attribute uniqueness validation to account
This commit is contained in:
Muhsin Keloth 2021-12-07 18:20:47 +05:30 committed by GitHub
parent 6b025816f5
commit 46afcd9348
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View file

@ -16,7 +16,7 @@
# #
# Indexes # 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) # index_custom_attribute_definitions_on_account_id (account_id)
# #
class CustomAttributeDefinition < ApplicationRecord class CustomAttributeDefinition < ApplicationRecord
@ -25,7 +25,7 @@ class CustomAttributeDefinition < ApplicationRecord
validates :attribute_key, validates :attribute_key,
presence: true, presence: true,
uniqueness: { scope: :attribute_model } uniqueness: { scope: :account_id }
validates :attribute_display_type, presence: true validates :attribute_display_type, presence: true
validates :attribute_model, presence: true validates :attribute_model, presence: true

View file

@ -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

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -368,7 +368,7 @@ ActiveRecord::Schema.define(version: 2021_11_29_120040) do
t.text "attribute_description" t.text "attribute_description"
t.jsonb "attribute_values", default: [] t.jsonb "attribute_values", default: []
t.index ["account_id"], name: "index_custom_attribute_definitions_on_account_id" 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 end
create_table "custom_filters", force: :cascade do |t| create_table "custom_filters", force: :cascade do |t|