DEV: update thread title prompt migration (#27052)

Add migration to handle batch processing of user options 

Co-authored-by: Osama Sayegh <asooomaasoooma90@gmail.com>
This commit is contained in:
David Battersby 2024-05-17 00:53:19 +04:00 committed by GitHub
parent 63b7a36fac
commit 34c4acd32f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -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