Use SQL heredoc on long statement lines in migrations (#29112)

This commit is contained in:
Matt Jankowski 2024-02-06 11:42:25 -05:00 committed by GitHub
parent 64300e0fe3
commit 1e0b0a3486
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 7 deletions

View File

@ -2,7 +2,17 @@
class AddSearchIndexToAccounts < ActiveRecord::Migration[5.0] class AddSearchIndexToAccounts < ActiveRecord::Migration[5.0]
def up 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 end
def down def down

View File

@ -9,9 +9,32 @@ class AddCaseInsensitiveIndexToTags < ActiveRecord::Migration[5.2]
redundant_tag_ids = row['ids'].split(',')[1..] redundant_tag_ids = row['ids'].split(',')[1..]
safety_assured do 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 <<~SQL.squish
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)" UPDATE accounts_tags
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)" 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 end
Tag.where(id: redundant_tag_ids).in_batches.delete_all Tag.where(id: redundant_tag_ids).in_batches.delete_all

View File

@ -3,13 +3,26 @@
class FixCanonicalEmailBlocksForeignKey < ActiveRecord::Migration[6.1] class FixCanonicalEmailBlocksForeignKey < ActiveRecord::Migration[6.1]
def up def up
safety_assured do 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
end end
def down def down
safety_assured do 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 end
end end

View File

@ -4,7 +4,16 @@ class FixReblogDeletedAt < ActiveRecord::Migration[6.1]
disable_ddl_transaction! disable_ddl_transaction!
def up 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 end
def down; end def down; end