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:
parent
a5099c72a7
commit
74a9859a12
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue