mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Make Bookmark#post_id column nullable (#16287)
As we are gradually moving to having a polymorphic bookmarkable relationship on the Bookmark table, we need to make the post_id column nullable to be able to develop and test the new columns, and for cutover/migration purposes later as well.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MakeSomeBookmarkColumnsNullable < ActiveRecord::Migration[6.1]
|
||||
def up
|
||||
change_column_null :bookmarks, :post_id, true
|
||||
execute "ALTER TABLE bookmarks ADD CONSTRAINT enforce_post_id_or_bookmarkable CHECK (
|
||||
(post_id IS NOT NULL) OR (bookmarkable_id IS NOT NULL AND bookmarkable_type IS NOT NULL)
|
||||
)"
|
||||
end
|
||||
|
||||
def down
|
||||
DB.exec("UPDATE bookmarks SET post_id = bookmarkable_id WHERE bookmarkable_type = 'Post'")
|
||||
DB.exec("UPDATE bookmarks SET post_id = (SELECT id FROM posts WHERE topic_id = bookmarkable_id AND post_number = 1), for_topic = TRUE WHERE bookmarkable_type = 'Topic'")
|
||||
change_column_null :bookmarks, :post_id, false
|
||||
execute "ALTER TABLE bookmarks DROP CONSTRAINT enforce_post_id_or_bookmarkable"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user