Skip to content

A platform for community discussion. Free, open, simple.

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

Guo Xiang Tan2161abfabd43
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 1d19c5eb90b..91a95b64099 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 00000000000..ba984a88acd
--- /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