# frozen_string_literal: true # Packages must be unique by name - the same package is listed once per # architecture/component in the Debian archive and the importer looks it up # by name before inserting, so anything that lets two rows for one name appear # (e.g. concurrent import runs) breaks the site. Production had accidentally # relied on a constraint that was only ever added ad-hoc on some databases # and never tracked as a migration; this makes db:migrate the single source # of truth. class AddUniqueNameConstraintToPackages < ActiveRecord::Migration[8.1] def up add_unique_constraint :packages, :name, name: :packages_name_key unless constraint_exists? end def down remove_unique_constraint :packages, :name, name: :packages_name_key if constraint_exists? end private def constraint_exists? select_value(<<~SQL.squish) SELECT 1 FROM pg_constraint WHERE conrelid = 'packages'::regclass AND conname = 'packages_name_key' AND contype = 'u' SQL end end