Data quality

Orphan integrity audit for workspace records

Check every workspace record for a missing parent, keep one college workspace at a time, quarantine orphans for review, and stop new orphans with composite tenant foreign keys and soft-delete rules.

Scope and starting rule

Every app table holds tenant_id, and every check below runs inside one college workspace only. Confirm the exact table and column names in migrations/0002_app.sql in the shipped build before you apply a statement, and use those confirmed names in place of the logical names on this page. No check joins records across colleges. No check deletes a retired record, because past citations and audit entries must stay readable.

  • Parent types in scope are college workspace, source, assistant, question, and answer record.
  • Child types in scope are assistant source links, library entries, questions, answers, ratings, handoff threads, theme links, and saved collection links that store a workspace record id.
  • Course and room identifiers are not documented as separate tables in this pack. Where a course space is built, apply the same parent check with the course record as parent and tenant_id in the key.
  • Saved answers on this device are not workspace records and are not in this audit.

Audit table by parent and child

Run one anti-join per row, per college workspace, before you merge or retire any record. A row with a count above zero is an orphan group to quarantine, not a group to delete.

Child record, parent record, and orphan condition for one tenant_id
Child recordParent recordOrphan conditionFix on this page
Assistant source linkSource record in the same tenant_idLinked source is missing, soft-deleted, suspended, expired, or retired, or its tenant_id differsComposite tenant foreign key with RESTRICT, plus quarantine of the link
Library entrySource record and assistant record in the same tenant_idSource or assistant is missing, soft-deleted, or in another collegeComposite tenant foreign keys for both parents, plus quarantine
Question recordAssistant record in the same tenant_id, and the college workspace itselfAssistant is missing or soft-deleted, or tenant_id has no workspace recordComposite tenant foreign key to assistant, plus quarantine
Answer recordQuestion record in the same tenant_idQuestion is missing or soft-deleted, or tenant_id differs from the question tenant_idComposite tenant foreign key with RESTRICT, plus quarantine
Rating and comment recordAnswer record in the same tenant_idAnswer is missing or soft-deleted, or tenant_id differsComposite tenant foreign key with RESTRICT, plus quarantine
Handoff thread and theme linkQuestion record in the same tenant_idQuestion is missing or soft-deleted, or tenant_id differsComposite tenant foreign key with RESTRICT, plus quarantine

Exact integrity fix

Apply these rules in one migration change, with the confirmed table names from the shipped build. A single-column foreign key alone is not enough, because it does not stop a child in one college from pointing at a parent in another college.

  • Add a composite unique key on each parent record for tenant_id and id, if that pair is not already unique.
  • Add a composite foreign key on each child record for tenant_id and parent id, referencing the parent pair tenant_id and id, with ON DELETE RESTRICT and ON UPDATE RESTRICT. A delete or an id change for a parent with active children then fails instead of leaving a child with no parent.
  • For sources and assistants, add a soft-delete field and a status field where the shipped schema does not already hold them. Soft-delete sets the soft-delete field and a status of retired or superseded at once. The record stays readable for past citations and audit entries, stops being used in new answers, and an active-only unique index keeps one active record per identity per workspace.
  • For questions, answers, ratings, handoff threads, and theme links, use RESTRICT for a hard delete. Retire the record by status where the screen already supports a retired state, and keep the parent check on the retired record as well.
  • Keep every statement scoped by tenant_id and bound to the signed-in tenant. Do not add a cross-college join, a system view join, or a new login or tenancy path in this change.

Pattern to confirm in the shipped schema

CREATE UNIQUE INDEX IF NOT EXISTS idx_parent_tenant_id ON parent_table (tenant_id, id);
-- One composite foreign key per child table, using confirmed names:
-- FOREIGN KEY (tenant_id, parent_id) REFERENCES parent_table (tenant_id, id)
-- ON DELETE RESTRICT ON UPDATE RESTRICT

Replace parent_table and parent_id with the confirmed names for each row in the audit table above. Ship one foreign key per row in one reviewed change, and keep the existing tenant scope on every statement.

Backfill for existing orphans

Run this backfill per college workspace, in a transaction, before the new foreign keys are enforced. The backfill quarantines orphans for owner review and changes no headline count for records that have a valid parent.

  1. Count orphans per child type for one tenant_id

    Run the anti-join for each row in the audit table with tenant_id bound to that workspace. Record the count per child type before any change.

  2. Quarantine orphans, do not delete them

    Set the child record status to orphan_quarantine, set a review flag, and keep every field needed to re-link it to the canonical parent found in the data quality audit. A quarantined record is hidden from new answers and from headline counts, and it is shown in Reports so an owner can re-link or retire it.

  3. Re-link or retire each quarantine group

    For each group, either link the child to the canonical parent in the same tenant_id or retire the child by status. Do not link a child to a parent in another college.

  4. Enforce the foreign keys, then re-run the count

    Add the composite foreign keys after quarantine counts reach zero for active records. Re-run every anti-join and confirm zero active orphans per child type.

  5. Make the backfill idempotent

    A second run with the same tenant_id changes zero rows, because quarantined records are already flagged and re-linked records already have a valid parent. Record the outcome of each run with tenant_id, child type, count before, count after, and the date checked.

Anti-join pattern for one tenant_id

SELECT COUNT(*) AS orphan_count
FROM child_table AS child
WHERE child.tenant_id = ?1
  AND NOT EXISTS (
    SELECT 1 FROM parent_table AS parent
    WHERE parent.tenant_id = child.tenant_id
      AND parent.id = child.parent_id
      AND parent.deleted_at IS NULL
  );

Bind tenant_id as the first parameter for that workspace. Use this pattern for each row in the audit table, with the confirmed child and parent names, and exclude records already in orphan_quarantine or retired status from the active count while still listing them for owner review.

Checks before you call the work done

Do each check in a test workspace before you publish.

  • Insert a child with a missing parent id in that tenant and confirm the insert fails.
  • Insert a child that points at a parent id in another college and confirm the insert fails.
  • Delete a parent with an active child and confirm the delete is restricted, while a soft-delete of a source keeps past citations readable and stops new answers.
  • Run the backfill twice for one tenant and confirm the second run changes zero rows and the orphan count stays at zero for active records.
  • Confirm test and staff preview records are labeled as test, excluded from headline counts, and still pass the same parent check.
  • Confirm the college name shown on screen matches the tenant_id used in every check, and that no check returns a record from another college.

Keep one valid parent per workspace record

Use the composite tenant keys, the soft-delete rules, and the quarantine backfill on this page, then check the orphan count in the next period before you call the work done.