Skip to content

A platform for community discussion. Free, open, simple.

DEV: update thread title prompt migration (#27052)

David Battersby34c4acd32f53
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