discourse/db/migrate/20240827063908_copy_users_seen_notification_id_values.rb
Alan Guo Xiang Tan 4a6fc45429
DEV: Migrate User#seen_notification_id to bigint (#28572)
`Notification#id` was migrated to `bigint` in 799a45a291e9f2bd94278f565e58874458768079
2024-08-27 14:32:55 +03:00

20 lines
619 B
Ruby

# frozen_string_literal: true
class CopyUsersSeenNotificationIdValues < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def up
min_id, max_id = execute("SELECT MIN(id), MAX(id) FROM users")[0].values
batch_size = 10_000
(min_id..max_id).step(batch_size) { |start_id| execute <<~SQL.squish } if min_id && max_id
UPDATE users
SET new_seen_notification_id = seen_notification_id
WHERE id >= #{start_id} AND id < #{start_id + batch_size} AND new_seen_notification_id != seen_notification_id
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end