DEV: Promote historic post_deploy migrations (#28128)

This commit promotes all post_deploy migrations which existed in Discourse v3.2.0 (timestamp <= 20240112043325)
This commit is contained in:
Natalie Tay
2024-07-30 01:14:03 +08:00
committed by GitHub
parent 7a1e3accff
commit 5b51ed3856
6 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class UpdateThreadReplyCounts < ActiveRecord::Migration[7.0]
def up
DB.exec <<~SQL
UPDATE chat_threads threads
SET replies_count = subquery.replies_count
FROM (
SELECT COUNT(*) - 1 AS replies_count, thread_id
FROM chat_messages
WHERE chat_messages.deleted_at IS NULL AND thread_id IS NOT NULL
GROUP BY thread_id
) subquery
WHERE threads.id = subquery.thread_id
AND subquery.replies_count != threads.replies_count
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
class BackfillChatChannelAndThreadLastMessageIdsPostMigrate < ActiveRecord::Migration[7.0]
execute <<-SQL
UPDATE chat_channels
SET last_message_id = (
SELECT cm.id
FROM chat_messages cm
LEFT JOIN chat_threads ON chat_threads.original_message_id = cm.id
WHERE cm.chat_channel_id = chat_channels.id
AND cm.deleted_at IS NULL
AND (cm.thread_id IS NULL OR chat_threads.id IS NOT NULL)
ORDER BY cm.created_at DESC, cm.id DESC
LIMIT 1
);
SQL
execute <<-SQL
UPDATE chat_threads
SET last_message_id = (
SELECT cm.id
FROM chat_messages cm
WHERE cm.thread_id = chat_threads.id
AND cm.deleted_at IS NULL
ORDER BY cm.created_at DESC, cm.id DESC
LIMIT 1
);
SQL
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class UpdateRelationshipBetweenChatMentionsAndNotificationsPostMigrate < ActiveRecord::Migration[
7.0
]
disable_ddl_transaction!
BATCH_SIZE = 5000
def up
begin
inserted_count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)
INSERT INTO chat_mention_notifications(chat_mention_id, notification_id)
SELECT cm.id, cm.notification_id
FROM chat_mentions cm
LEFT JOIN chat_mention_notifications cmn ON cmn.chat_mention_id = cm.id
WHERE cm.notification_id IS NOT NULL and cmn.chat_mention_id IS NULL
LIMIT :batch_size;
SQL
end while inserted_count > 0
end
def down
end
end

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
class SetTypeAndTargetIdOnChatMentionsPostMigrate < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
BATCH_SIZE = 5000
def up
# we're setting it again in post-migration
# in case some mentions have been created after we run
# this query the first time in the regular migration
begin
updated_count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)
WITH cte AS (SELECT id, user_id
FROM chat_mentions
WHERE type IS NULL AND target_id IS NULL
LIMIT :batch_size)
UPDATE chat_mentions
SET type = 'Chat::UserMention', target_id = cte.user_id
FROM cte
WHERE chat_mentions.id = cte.id;
SQL
end while updated_count > 0
end
def down
end
end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
class MakeTypeOnChatMentionsNonNullable < ActiveRecord::Migration[7.0]
def up
change_column_null :chat_mentions, :type, false
end
def down
change_column_null :chat_mentions, :type, true
end
end