class CreatePostImplications < ActiveRecord::Migration[8.0]
  def up
    create_table :post_implications, primary_key: [:post_id, :parent_post_id] do |t|
      t.references :post, null: false, foreign_key: true, index: false
      t.references :parent_post, null: false, foreign_key: { to_table: :posts }
      t.timestamps

      t.check_constraint 'post_id <> parent_post_id',
                         name: 'chk_post_implications_no_self'
    end

    add_column :post_versions, :parent_post_ids, :text, null: false, after: :parent_id
    remove_column :post_versions, :parent_id, :bigint
    remove_reference :posts, :parent, foreign_key: { to_table: :posts }
  end

  def down
    add_reference :posts, :parent, foreign_key: { to_table: :posts }, after: :thumbnail_base
    add_column :post_versions, :parent_id, :bigint, after: :post_id
    remove_column :post_versions, :parent_post_ids, :text

    drop_table :post_implications
  end
end
