Packages build info, properly implement the uniqueness constraint

🔥 Problem

Follow-up of !68649 (comment 656594885).

We have an issue with build infos problems.

  1. A lot of rows have nil pipeline_id. This is not useful, the code will not do anything with that that.
  2. Several rows are duplicated (excluding with a NULL pipeline_id). This doesn't make sense. The build infos are meant to link a package model (see below) with a pipeline object. We don't need the same package and same pipeline to be linked multiple times.

We have two tables for builds infos:

  • At the packages_packages level. This level has problem (1.) and (2.).
  • At the packages_packages_files level. This level has only problem (1.).

🚒 Solution

There are two things to do here:

  • Existing rows cleanup:
    • Remove all rows with a nil pipeline_id. They are not used or better said, the code can't do anything with that, it's not valuable.
    • Deduplicate duplicated rows. We have cases with multiple rows on the same package_id and pipeline_id. This is not useful. We only need one row.
  • Enforce a unique constraint:
    • Implement the unique constraint at the database level
    • Implement the unique constraint at the rails level
    • The table currently doesn't have any constraint. By adding one, we can't use find_or_create_by anymore. The solution is to switch from find_to_create_by to upsert to uniquely insert the build info rows.
  • Enforce a non null constraint:
    • Implement the unique constraint at the database level
    • Implement the unique constraint at the rails level
Edited by David Fernandez