class DropLegacyPathColumnsFromReleases < ActiveRecord::Migration[8.1]
  COLUMN_KINDS = {
    "cartridge_path"   => "cartridge",
    "source_path"      => "source",
    "html_folder_path" => "html",
    "docs_folder_path" => "docs"
  }.freeze

  def up
    COLUMN_KINDS.each_key do |column|
      remove_column :releases, column
    end
  end

  def down
    COLUMN_KINDS.each_key do |column|
      add_column :releases, column, :string
    end

    # rollbacknél az adat a release_assets-ből áll vissza, nem veszik el
    COLUMN_KINDS.each do |column, kind|
      execute <<~SQL.squish
        UPDATE releases r
        JOIN release_assets ra ON ra.release_id = r.id AND ra.kind = '#{kind}'
        SET r.#{column} = ra.path
      SQL
    end
  end
end
