From 1e0b0a3486cf04666b7cbd67a4a72cf7f8068c97 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 6 Feb 2024 11:42:25 -0500 Subject: [PATCH] Use SQL heredoc on long statement lines in migrations (#29112) --- ...0317193015_add_search_index_to_accounts.rb | 12 +++++++- ...5042_add_case_insensitive_index_to_tags.rb | 29 +++++++++++++++++-- ..._fix_canonical_email_blocks_foreign_key.rb | 17 +++++++++-- .../20220309213005_fix_reblog_deleted_at.rb | 11 ++++++- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/db/migrate/20170317193015_add_search_index_to_accounts.rb b/db/migrate/20170317193015_add_search_index_to_accounts.rb index e29da61b15..0edc381549 100644 --- a/db/migrate/20170317193015_add_search_index_to_accounts.rb +++ b/db/migrate/20170317193015_add_search_index_to_accounts.rb @@ -2,7 +2,17 @@ class AddSearchIndexToAccounts < ActiveRecord::Migration[5.0] def up - execute 'CREATE INDEX search_index ON accounts USING gin((setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\')));' + execute <<~SQL.squish + CREATE INDEX search_index + ON accounts + USING gin( + ( + setweight(to_tsvector('simple', accounts.display_name), 'A') || + setweight(to_tsvector('simple', accounts.username), 'B') || + setweight(to_tsvector('simple', coalesce(accounts.domain, '')), 'C') + ) + ) + SQL end def down diff --git a/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb b/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb index f2d3aa8ee8..02f073e14f 100644 --- a/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb +++ b/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb @@ -9,9 +9,32 @@ class AddCaseInsensitiveIndexToTags < ActiveRecord::Migration[5.2] redundant_tag_ids = row['ids'].split(',')[1..] safety_assured do - execute "UPDATE accounts_tags AS t0 SET tag_id = #{canonical_tag_id} WHERE tag_id IN (#{redundant_tag_ids.join(', ')}) AND NOT EXISTS (SELECT t1.tag_id FROM accounts_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.account_id = t0.account_id)" - execute "UPDATE statuses_tags AS t0 SET tag_id = #{canonical_tag_id} WHERE tag_id IN (#{redundant_tag_ids.join(', ')}) AND NOT EXISTS (SELECT t1.tag_id FROM statuses_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.status_id = t0.status_id)" - execute "UPDATE featured_tags AS t0 SET tag_id = #{canonical_tag_id} WHERE tag_id IN (#{redundant_tag_ids.join(', ')}) AND NOT EXISTS (SELECT t1.tag_id FROM featured_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.account_id = t0.account_id)" + execute <<~SQL.squish + UPDATE accounts_tags + AS t0 + SET tag_id = #{canonical_tag_id} + WHERE + tag_id IN (#{redundant_tag_ids.join(', ')}) + AND NOT EXISTS (SELECT t1.tag_id FROM accounts_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.account_id = t0.account_id) + SQL + + execute <<~SQL.squish + UPDATE statuses_tags + AS t0 + SET tag_id = #{canonical_tag_id} + WHERE + tag_id IN (#{redundant_tag_ids.join(', ')}) + AND NOT EXISTS (SELECT t1.tag_id FROM statuses_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.status_id = t0.status_id) + SQL + + execute <<~SQL.squish + UPDATE featured_tags + AS t0 + SET tag_id = #{canonical_tag_id} + WHERE + tag_id IN (#{redundant_tag_ids.join(', ')}) + AND NOT EXISTS (SELECT t1.tag_id FROM featured_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.account_id = t0.account_id) + SQL end Tag.where(id: redundant_tag_ids).in_batches.delete_all diff --git a/db/migrate/20210630000137_fix_canonical_email_blocks_foreign_key.rb b/db/migrate/20210630000137_fix_canonical_email_blocks_foreign_key.rb index 9b475966bb..90a1b1a5cf 100644 --- a/db/migrate/20210630000137_fix_canonical_email_blocks_foreign_key.rb +++ b/db/migrate/20210630000137_fix_canonical_email_blocks_foreign_key.rb @@ -3,13 +3,26 @@ class FixCanonicalEmailBlocksForeignKey < ActiveRecord::Migration[6.1] def up safety_assured do - execute 'ALTER TABLE canonical_email_blocks DROP CONSTRAINT fk_rails_1ecb262096, ADD CONSTRAINT fk_rails_1ecb262096 FOREIGN KEY (reference_account_id) REFERENCES accounts(id) ON DELETE CASCADE;' + execute <<~SQL.squish + ALTER TABLE canonical_email_blocks + DROP CONSTRAINT fk_rails_1ecb262096, + ADD CONSTRAINT fk_rails_1ecb262096 + FOREIGN KEY (reference_account_id) + REFERENCES accounts(id) + ON DELETE CASCADE + SQL end end def down safety_assured do - execute 'ALTER TABLE canonical_email_blocks DROP CONSTRAINT fk_rails_1ecb262096, ADD CONSTRAINT fk_rails_1ecb262096 FOREIGN KEY (reference_account_id) REFERENCES accounts(id);' + execute <<~SQL.squish + ALTER TABLE canonical_email_blocks + DROP CONSTRAINT fk_rails_1ecb262096, + ADD CONSTRAINT fk_rails_1ecb262096 + FOREIGN KEY (reference_account_id) + REFERENCES accounts(id) + SQL end end end diff --git a/db/migrate/20220309213005_fix_reblog_deleted_at.rb b/db/migrate/20220309213005_fix_reblog_deleted_at.rb index e3474beeb1..7fb9bff26c 100644 --- a/db/migrate/20220309213005_fix_reblog_deleted_at.rb +++ b/db/migrate/20220309213005_fix_reblog_deleted_at.rb @@ -4,7 +4,16 @@ class FixReblogDeletedAt < ActiveRecord::Migration[6.1] disable_ddl_transaction! def up - safety_assured { execute 'UPDATE statuses s SET deleted_at = r.deleted_at FROM statuses r WHERE s.reblog_of_id = r.id AND r.deleted_at IS NOT NULL' } + safety_assured do + execute <<~SQL.squish + UPDATE statuses s + SET deleted_at = r.deleted_at + FROM statuses r + WHERE + s.reblog_of_id = r.id + AND r.deleted_at IS NOT NULL + SQL + end end def down; end