fix: Convert cached_label_list to text (#9143)

This commit is contained in:
Shivam Mishra
2024-03-26 07:17:08 +05:30
committed by GitHub
parent 48452a42f4
commit 9917cb4273
3 changed files with 35 additions and 3 deletions

View File

@@ -6,7 +6,7 @@
# additional_attributes :jsonb
# agent_last_seen_at :datetime
# assignee_last_seen_at :datetime
# cached_label_list :string
# cached_label_list :text
# contact_last_seen_at :datetime
# custom_attributes :jsonb
# first_reply_created_at :datetime

View File

@@ -0,0 +1,32 @@
class ConvertCachedLabelListToText < ActiveRecord::Migration[7.0]
def up
change_column :conversations, :cached_label_list, :text
end
def down
# This might cause data loss if the text is longer than 255 characters
# lets start by truncating the data to 255 characters
Conversation.where('LENGTH(cached_label_list) > 255').find_in_batches do |conversation_batch|
Conversation.transaction do
conversation_batch.each do |conversation|
conversation.update!(cached_label_list: truncate_list(conversation.cached_label_list))
end
end
end
change_column :conversations, :cached_label_list, :string
end
private
# Truncate the list to 255 characters or less
# by removing the last element until the length is less than 255
def truncate_list(label_list)
labels = label_list.split(',')
# we add the `labels.length - 1` to account for the commas
labels.pop while (labels.join(',').length + labels.length - 1) > 255
labels.join(',')
end
end

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: 2024_03_19_062553) do
ActiveRecord::Schema[7.0].define(version: 2024_03_22_071629) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -472,7 +472,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_19_062553) do
t.integer "priority"
t.bigint "sla_policy_id"
t.datetime "waiting_since"
t.string "cached_label_list"
t.text "cached_label_list"
t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true
t.index ["account_id", "id"], name: "index_conversations_on_id_and_account_id"
t.index ["account_id", "inbox_id", "status", "assignee_id"], name: "conv_acid_inbid_stat_asgnid_idx"