From 34c4acd32f539038d42309f5500a5b04a8cb8cf1 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Fri, 17 May 2024 00:53:19 +0400 Subject: [PATCH] DEV: update thread title prompt migration (#27052) Add migration to handle batch processing of user options Co-authored-by: Osama Sayegh --- ...ow_thread_title_prompts_to_user_options.rb | 2 +- ...e_user_options_for_thread_title_prompts.rb | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 plugins/chat/db/post_migrate/20240516145911_update_user_options_for_thread_title_prompts.rb diff --git a/plugins/chat/db/migrate/20240409093348_add_show_thread_title_prompts_to_user_options.rb b/plugins/chat/db/migrate/20240409093348_add_show_thread_title_prompts_to_user_options.rb index b70c08e3095..2c2236721cb 100644 --- a/plugins/chat/db/migrate/20240409093348_add_show_thread_title_prompts_to_user_options.rb +++ b/plugins/chat/db/migrate/20240409093348_add_show_thread_title_prompts_to_user_options.rb @@ -2,6 +2,6 @@ class AddShowThreadTitlePromptsToUserOptions < ActiveRecord::Migration[7.0] def change - add_column :user_options, :show_thread_title_prompts, :boolean, default: true, null: false + add_column :user_options, :show_thread_title_prompts, :boolean end end diff --git a/plugins/chat/db/post_migrate/20240516145911_update_user_options_for_thread_title_prompts.rb b/plugins/chat/db/post_migrate/20240516145911_update_user_options_for_thread_title_prompts.rb new file mode 100644 index 00000000000..61c1004177a --- /dev/null +++ b/plugins/chat/db/post_migrate/20240516145911_update_user_options_for_thread_title_prompts.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class UpdateUserOptionsForThreadTitlePrompts < ActiveRecord::Migration[7.0] + def up + change_column_default :user_options, :show_thread_title_prompts, true + + if DB.query_single( + "SELECT 1 FROM user_options WHERE show_thread_title_prompts IS NULL LIMIT 1", + ).first + batch_size = 100_000 + min_id = DB.query_single("SELECT MIN(user_id) FROM user_options").first.to_i + max_id = DB.query_single("SELECT MAX(user_id) FROM user_options").first.to_i + while max_id >= min_id + DB.exec( + "UPDATE user_options SET show_thread_title_prompts = true WHERE user_id > #{max_id - batch_size} AND user_id <= #{max_id}", + ) + max_id -= batch_size + end + end + + change_column_null :user_options, :show_thread_title_prompts, false + end + + def down + end +end