From 9917cb42730f3f61d51f2c0465bf0005d314f8cf Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 26 Mar 2024 07:17:08 +0530 Subject: [PATCH] fix: Convert `cached_label_list` to text (#9143) --- app/models/conversation.rb | 2 +- ...71629_convert_cached_label_list_to_text.rb | 32 +++++++++++++++++++ db/schema.rb | 4 +-- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20240322071629_convert_cached_label_list_to_text.rb diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 72518f250..513c993da 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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 diff --git a/db/migrate/20240322071629_convert_cached_label_list_to_text.rb b/db/migrate/20240322071629_convert_cached_label_list_to_text.rb new file mode 100644 index 000000000..91dfb428a --- /dev/null +++ b/db/migrate/20240322071629_convert_cached_label_list_to_text.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 498ad13d3..a877bf05d 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[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"