test(datastore): require repository-owned rows to carry the repository

Summary

  • Problem: our remote and hosted child tables (versions, files, tags, manifests, blobs) store only their parent's id, not the repository's. Any repository-wide read of them, like an idle list, cache cleanup or eviction by size, has to walk every package or image first, and adding the column later means a backfill on a large partitioned table. We hit this designing remote cache cleanup (S35, !3831 (merged)), and nothing stops a new format from repeating it.
  • Impact: the team: whoever adds the next format's tables.
  • Change: a rule in docs/dev/database.md: a table whose rows belong to a repository stores the repository id, NOT NULL, at every level, and each of its foreign keys to a parent (itself included) carries it, mapped to the parent's repository column, like maven_files already does for its package. TestRepositoryScope, a catalog-driven schema test, enforces it per foreign key: it finds a repository's tables through any foreign-key chain, starting at the column named after a format repository table (maven_repository_id for maven_repositories), so an upstream link is not ownership. The 31 foreign keys that predate the rule are exemptions, and a second subtest fails when one is no longer needed, so the list only shrinks. TestRepositoryScope_ProbesEachCase builds probe tables for each case the rule decides, in a transaction it rolls back, so a later edit cannot weaken the query unnoticed. S37 (Go hosted) and S38 (RPM hosted) are amended so their child tables, not built yet, follow the rule.
  • Downsides: new tables carry one more column and wider foreign keys. The exemption list is long until the existing tables are fixed; #1752 fixes the ones the cache cleanup needs.

The process changes (a "simplify now, defer safely" principle, a Deferred section in the spec template, a Plan MR question) were here at first. Danger keeps guardrail files out of an MR with code, so they moved to !3866 (merged).

Checklist

  • Ran it: ran both tests against a migrated database: they pass on today's schema. The probe test covers each case the rule decides:

    • Reported: a nullable direct owner, an owner whose not-null CHECK is NOT VALID, an owner whose CHECK is on another column, an unwidened child, a widened but nullable child, a self-reference, a second unwidened reference beside a widened one, a key mapping the repository to the wrong parent column, a key mapping another child column to the parent's repository, a child of a compliant table, and a child of an exempt table.
    • Not reported: a correct direct owner, an owner with a validated not-null CHECK, a correct widened child, and an upstream link with its rule.

    I checked it bites by weakening the query in five ways (dropping the NOT NULL clause, accepting a NOT VALID CHECK, checking only the child or only the parent side of the mapping, and matching a CHECK on any column): each failed the probe test.

  • Interfaces and data: none. No migration, no schema change, no API change. The main test reads the catalog; the probe test also creates tables, in a transaction it rolls back. S37 and S38 change only the specs of tables not built yet.

  • Failure: the test fails CI with the offending foreign keys and a pointer to the rule. If the query broke and found nothing, the second subtest fails, because every exemption would look fixed.

  • Rollout: n/a, a test and docs.

  • Hot path: n/a.

  • Database: no migration or application query. The catalog query runs only in the integration suite.

  • Security: n/a. The Duo security review found nothing.

  • ADRs: ADR-007 (every row belongs to a namespace, composite keys on partitioned tables). The widened key includes namespace_id, so it holds.

  • E2E: none: no client-visible behavior.

  • Simplicity: I rejected a lint rule over migration SQL or structure.sql: it can't see the final schema's foreign keys reliably, and would re-implement a parser. The catalog test sees the real schema and follows the existing TestIDUUIDVersionCheck_CoversEveryApplicationGeneratedIDTable pattern. The rule stays general, so a new format is covered with no edit.

Related to #1752

Edited by João Pereira

Merge request reports

Loading
Loading