feat: update schema

This commit is contained in:
Shivam Mishra
2025-03-25 18:43:02 +05:30
parent 89deb74c24
commit 340e5d2acc
4 changed files with 31 additions and 30 deletions
+13 -14
View File
@@ -2,20 +2,19 @@
#
# Table name: accounts
#
# id :integer not null, primary key
# auto_resolve_duration :integer
# contactable_contacts_count :integer default(0)
# custom_attributes :jsonb
# domain :string(100)
# feature_flags :bigint default(0), not null
# internal_attributes :jsonb not null
# limits :jsonb
# locale :integer default("en")
# name :string not null
# status :integer default("active")
# support_email :string(100)
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# auto_resolve_duration :integer
# custom_attributes :jsonb
# domain :string(100)
# feature_flags :bigint default(0), not null
# internal_attributes :jsonb not null
# limits :jsonb
# locale :integer default("en")
# name :string not null
# status :integer default("active")
# support_email :string(100)
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
+2 -1
View File
@@ -25,13 +25,14 @@
# Indexes
#
# index_contacts_on_account_id (account_id)
# index_contacts_on_account_id_and_last_activity_at (account_id,last_activity_at DESC NULLS LAST)
# index_contacts_on_blocked (blocked)
# index_contacts_on_lower_email_account_id (lower((email)::text), account_id)
# index_contacts_on_name_email_phone_number_identifier (name,email,phone_number,identifier) USING gin
# index_contacts_on_nonempty_fields (account_id,email,phone_number,identifier) WHERE (((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))
# index_contacts_on_phone_number_and_account_id (phone_number,account_id)
# index_contacts_searchable_fields_gin (name gin_trgm_ops, email gin_trgm_ops, phone_number gin_trgm_ops, identifier gin_trgm_ops, ((additional_attributes ->> 'company_name'::text)) gin_trgm_ops) WHERE (((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text)) USING gin
# index_resolved_contact_account_id (account_id) WHERE (((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))
# index_resolved_contacts_on_account_and_last_activity (account_id,last_activity_at DESC NULLS LAST) WHERE (((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))
# uniq_email_per_account_contact (email,account_id) UNIQUE
# uniq_identifier_per_account_contact (identifier,account_id) UNIQUE
#
@@ -21,19 +21,19 @@ class UpdateIndexesForContacts < ActiveRecord::Migration[7.0]
private
def add_searchable_index
add_index :contacts,
[:name, :email, :phone_number, :identifier, "(additional_attributes->>'company_name')"],
name: 'index_contacts_searchable_fields_gin',
using: :gin,
opclass: {
name: :gin_trgm_ops,
email: :gin_trgm_ops,
phone_number: :gin_trgm_ops,
identifier: :gin_trgm_ops,
"(additional_attributes->>'company_name')": :gin_trgm_ops
},
where: "(email <> '' OR phone_number <> '' OR identifier <> '')",
algorithm: :concurrently
execute "SET statement_timeout = '3600000';"
execute 'DROP INDEX IF EXISTS index_contacts_searchable_fields_gin;'
execute <<-SQL.squish
CREATE INDEX CONCURRENTLY index_contacts_searchable_fields_gin
ON contacts USING gin (
name gin_trgm_ops,
email gin_trgm_ops,
phone_number gin_trgm_ops,
identifier gin_trgm_ops,
(additional_attributes->>'company_name') gin_trgm_ops
)
WHERE (email <> '' OR phone_number <> '' OR identifier <> '');
SQL
end
def remove_old_index
+3 -2
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2025_03_15_202035) do
ActiveRecord::Schema[7.0].define(version: 2025_03_25_125320) do
# These extensions should be enabled to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -497,8 +497,9 @@ ActiveRecord::Schema[7.0].define(version: 2025_03_15_202035) do
t.string "country_code", default: ""
t.boolean "blocked", default: false, null: false
t.index "lower((email)::text), account_id", name: "index_contacts_on_lower_email_account_id"
t.index "name gin_trgm_ops, email gin_trgm_ops, phone_number gin_trgm_ops, identifier gin_trgm_ops, ((additional_attributes ->> 'company_name'::text)) gin_trgm_ops", name: "index_contacts_searchable_fields_gin", where: "(((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))", using: :gin
t.index ["account_id", "email", "phone_number", "identifier"], name: "index_contacts_on_nonempty_fields", where: "(((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))"
t.index ["account_id", "last_activity_at"], name: "index_contacts_on_account_id_and_last_activity_at", order: { last_activity_at: "DESC NULLS LAST" }
t.index ["account_id", "last_activity_at"], name: "index_resolved_contacts_on_account_and_last_activity", order: { last_activity_at: "DESC NULLS LAST" }, where: "(((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))"
t.index ["account_id"], name: "index_contacts_on_account_id"
t.index ["account_id"], name: "index_resolved_contact_account_id", where: "(((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))"
t.index ["blocked"], name: "index_contacts_on_blocked"