From 7bd832c9bd77ec5c9427b8162ba005d75c113690 Mon Sep 17 00:00:00 2001 From: 2458773093 <2458773093@protonmail.com> Date: Mon, 22 Jul 2019 02:50:01 +0300 Subject: [PATCH] better migrations for unread count field on group accounts table --- ...0190721214831_add_unread_count_to_group_accounts.rb | 9 +++++++-- ...4917_backfill_add_unread_count_to_group_accounts.rb | 10 ++++++++++ db/schema.rb | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20190721234917_backfill_add_unread_count_to_group_accounts.rb diff --git a/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb b/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb index 8a9f19df..efe77fd7 100644 --- a/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb +++ b/db/migrate/20190721214831_add_unread_count_to_group_accounts.rb @@ -1,5 +1,10 @@ class AddUnreadCountToGroupAccounts < ActiveRecord::Migration[5.2] - def change - add_column :group_accounts, :unread_count, :integer, default: 0 + def up + add_column :group_accounts, :unread_count, :integer + change_column_default :group_accounts, :unread_count, 0 + end + + def down + remove_column :group_accounts, :unread_count end end diff --git a/db/migrate/20190721234917_backfill_add_unread_count_to_group_accounts.rb b/db/migrate/20190721234917_backfill_add_unread_count_to_group_accounts.rb new file mode 100644 index 00000000..e71fb1b0 --- /dev/null +++ b/db/migrate/20190721234917_backfill_add_unread_count_to_group_accounts.rb @@ -0,0 +1,10 @@ +class BackfillAddUnreadCountToGroupAccounts < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + GroupAccount.in_batches do |relation| + relation.update_all unread_count: 0 + sleep(0.1) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b3aaf19c..d9bf6f1e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_07_21_214831) do +ActiveRecord::Schema.define(version: 2019_07_21_234917) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"