class AddCascadeToSoftwareForeignKeys < ActiveRecord::Migration[8.1]
  def up
    remove_foreign_key :external_links, name: "fk_softwares_external_links"
    add_foreign_key :external_links, :softwares, name: "fk_softwares_external_links", on_delete: :cascade

    remove_foreign_key :releases, name: "fk_softwares_releases"
    add_foreign_key :releases, :softwares, name: "fk_softwares_releases", on_delete: :cascade

    remove_foreign_key :software_images, :softwares
    add_foreign_key :software_images, :softwares, on_delete: :cascade

    # Keep download stats when a release is removed.
    remove_foreign_key :downloads, name: "fk_downloads_release"
    add_foreign_key :downloads, :releases, name: "fk_downloads_release", on_delete: :nullify
  end

  def down
    remove_foreign_key :external_links, name: "fk_softwares_external_links"
    add_foreign_key :external_links, :softwares, name: "fk_softwares_external_links"

    remove_foreign_key :releases, name: "fk_softwares_releases"
    add_foreign_key :releases, :softwares, name: "fk_softwares_releases"

    remove_foreign_key :software_images, :softwares
    add_foreign_key :software_images, :softwares

    remove_foreign_key :downloads, name: "fk_downloads_release"
    add_foreign_key :downloads, :releases, name: "fk_downloads_release"
  end
end
