FIX: Migrate values before adding a not null constraint. (#18940)

Since the migration was added as a post migration, we'll try to add the constraint first, causing a NotNullViolation exception.

This only affects sites that were last deployed more than two days ago.
This commit is contained in:
Roman Rizzi 2022-11-08 16:11:03 -03:00 committed by GitHub
parent a5099c72a7
commit 74a9859a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 17 deletions

View File

@ -2,6 +2,14 @@
class MakeChatEditorIdsNotNull < ActiveRecord::Migration[7.0]
def change
DB.exec("UPDATE chat_messages SET last_editor_id = user_id")
DB.exec(<<~SQL)
UPDATE chat_message_revisions cmr
SET user_id = cm.user_id
FROM chat_messages AS cm
WHERE cmr.chat_message_id = cm.id
SQL
change_column_null :chat_messages, :last_editor_id, false
change_column_null :chat_message_revisions, :user_id, false
end

View File

@ -1,17 +0,0 @@
# frozen_string_literal: true
class BackfillEditingUserIdsForChatMessagesAndRevisions < ActiveRecord::Migration[7.0]
def up
DB.exec("UPDATE chat_messages SET last_editor_id = user_id")
DB.exec(<<~SQL)
UPDATE chat_message_revisions cmr
SET user_id = cm.user_id
FROM chat_messages AS cm
WHERE cmr.chat_message_id = cm.id
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end