From 0de3b279ce99157385146eb9f6c7d2a5878c825c Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 21 Jun 2023 23:57:16 +1000 Subject: [PATCH] FEATURE: add db:resize:notification_id task for growing table (#20505) Under exceptional cases people may need to resize the notification table. This only happens on forums with a total of more than 2.5 billion notifications. This rake task can be used to convert all the notification columns to bigint to make more room. --- lib/tasks/db.rake | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 68fb2d50f7d..3170506fd1a 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -575,3 +575,18 @@ task "db:status:json" do puts({ status: "ok" }.to_json) end end + +desc "Grow notification id column to a big int in case of overflow" +task "db:resize:notification_id" => :environment do + sql = <<~SQL + SELECT table_name, column_name FROM INFORMATION_SCHEMA.columns + WHERE (column_name like '%notification_id' OR column_name = 'id' and table_name = 'notifications') AND data_type = 'integer' + SQL + + DB + .query(sql) + .each do |row| + puts "Changing #{row.table_name}(#{row.column_name}) to a bigint" + DB.exec("ALTER table #{row.table_name} ALTER COLUMN #{row.column_name} TYPE BIGINT") + end +end