]> source.dussan.org Git - redmine.git/commitdiff
Add a migration to update existing journals where `updated_by_id` points to a deleted...
authorGo MAEDA <maeda@farend.jp>
Tue, 29 Oct 2024 01:11:29 +0000 (01:11 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 29 Oct 2024 01:11:29 +0000 (01:11 +0000)
git-svn-id: https://svn.redmine.org/redmine/trunk@23170 e93f8b46-1217-0410-a6f0-8f06a7374b81

db/migrate/20241026031710_update_orphaned_journal_updated_by_id_to_anonymous.rb [new file with mode: 0644]

diff --git a/db/migrate/20241026031710_update_orphaned_journal_updated_by_id_to_anonymous.rb b/db/migrate/20241026031710_update_orphaned_journal_updated_by_id_to_anonymous.rb
new file mode 100644 (file)
index 0000000..2afb774
--- /dev/null
@@ -0,0 +1,18 @@
+class UpdateOrphanedJournalUpdatedByIdToAnonymous < ActiveRecord::Migration[7.2]
+  def up
+    # Don't use `User.anonymous` here because it creates a new anonymous
+    # user if one doesn't exist yet.
+    anonymous_user_id = AnonymousUser.unscoped.pick(:id)
+    # The absence of an anonymous user implies a fresh installation.
+    return if anonymous_user_id.nil?
+
+    Journal.joins('LEFT JOIN users ON users.id = journals.updated_by_id')
+           .where.not(updated_by_id: nil)
+           .where(users: { id: nil })
+           .update_all(updated_by_id: anonymous_user_id)
+  end
+
+  def down
+    # no-op
+  end
+end