DEV: Move data migration of `PostSearchData#private_message` into post_migration.

DEV: Move data migration of PostSearchData#private_message into post_migration.

Follow-up to 92b7fe4c62094275c5dd09b1f3dac5c5cbcad1c0

diff --git a/db/migrate/20200813051337_add_private_message_to_post_search_data.rb b/db/migrate/20200813051337_add_private_message_to_post_search_data.rb
index 1d19c5e..91a95b6 100644
--- a/db/migrate/20200813051337_add_private_message_to_post_search_data.rb
+++ b/db/migrate/20200813051337_add_private_message_to_post_search_data.rb
@@ -3,49 +3,9 @@
 class AddPrivateMessageToPostSearchData < ActiveRecord::Migration[6.0]
   def up
     add_column :post_search_data, :private_message, :boolean
-
-    # Delete post_search_data of orphaned posts
-    execute <<~SQL
-    DELETE FROM post_search_data
-    WHERE post_id IN (
-      SELECT posts.id
-      FROM posts
-      LEFT JOIN topics ON topics.id = posts.topic_id
-      WHERE topics.id IS NULL
-    )
-    SQL
-
-    # Delete orphaned post_search_data
-    execute <<~SQL
-    DELETE FROM post_search_data
-    WHERE post_id IN (
-      SELECT post_search_data.post_id
-      FROM post_search_data
-      LEFT JOIN posts ON posts.id = post_search_data.post_id
-      WHERE posts.id IS NULL
-    )
-    SQL
-
-    execute <<~SQL
-    UPDATE post_search_data
-    SET private_message = true
-    FROM posts
-    INNER JOIN topics ON topics.id = posts.topic_id AND topics.archetype = 'private_message'
-    WHERE posts.id = post_search_data.post_id
-    SQL
-
-    execute <<~SQL
-    UPDATE post_search_data
-    SET private_message = false
-    FROM posts
-    INNER JOIN topics ON topics.id = posts.topic_id AND topics.archetype <> 'private_message'
-    WHERE posts.id = post_search_data.post_id
-    SQL
-
-    change_column_null(:post_search_data, :private_message, false)
   end
 
   def down
-    raise ActiveRecord::IrreversibleMigration
+    remove_column :post_search_data, :private_message
   end
 end
diff --git a/db/post_migrate/20200818084329_update_private_message_on_post_search_data.rb b/db/post_migrate/20200818084329_update_private_message_on_post_search_data.rb
new file mode 100644
index 0000000..ba984a8
--- /dev/null
+++ b/db/post_migrate/20200818084329_update_private_message_on_post_search_data.rb
@@ -0,0 +1,47 @@
+class UpdatePrivateMessageOnPostSearchData < ActiveRecord::Migration[6.0]
+  def up
+    # Delete post_search_data of orphaned posts
+    execute <<~SQL
+    DELETE FROM post_search_data
+    WHERE post_id IN (
+      SELECT posts.id
+      FROM posts
+      LEFT JOIN topics ON topics.id = posts.topic_id
+      WHERE topics.id IS NULL
+    )
+    SQL
+
+    # Delete orphaned post_search_data
+    execute <<~SQL
+    DELETE FROM post_search_data
+    WHERE post_id IN (
+      SELECT post_search_data.post_id
+      FROM post_search_data
+      LEFT JOIN posts ON posts.id = post_search_data.post_id
+      WHERE posts.id IS NULL
+    )
+    SQL
+
+    execute <<~SQL
+    UPDATE post_search_data
+    SET private_message = true
+    FROM posts
+    INNER JOIN topics ON topics.id = posts.topic_id AND topics.archetype = 'private_message'
+    WHERE posts.id = post_search_data.post_id
+    SQL
+
+    execute <<~SQL
+    UPDATE post_search_data
+    SET private_message = false
+    FROM posts
+    INNER JOIN topics ON topics.id = posts.topic_id AND topics.archetype <> 'private_message'
+    WHERE posts.id = post_search_data.post_id
+    SQL
+
+    change_column_null(:post_search_data, :private_message, false)
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end

GitHub sha: 2161abfa

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/migration-error-activerecord-pg-error-column-private-message-contains-null-values/161304/1