Skip to content

Handle self-referencing FK filter propagation during subsetting#19

Merged
nafg merged 4 commits intomasterfrom
self-ref-fk-filter-propagation
Feb 12, 2026
Merged

Handle self-referencing FK filter propagation during subsetting#19
nafg merged 4 commits intomasterfrom
self-ref-fk-filter-propagation

Conversation

@nafg
Copy link
Collaborator

@nafg nafg commented Feb 11, 2026

Summary

  • When subsetting, FilterPropagation now correctly handles self-referencing FKs by generating recursive CTEs that compute the transitive closure of reachable rows
  • Cross-table FKs are processed first so the in-progress accumulator is available for self-ref FK processing
  • Rows with dangling self-ref FK pointers (e.g., a project in dept 1 whose parent is in dept 2) are now excluded

Test plan

  • Unit tests: self-ref FK with explicit filter, cross-table propagated filter, and no filter
  • Integration test: 3-table chain (departments → projects → assignments) with self-ref FK on projects
  • All existing tests pass (no regressions)

🤖 Generated with Claude Code

@nafg nafg requested a review from Copilot February 11, 2026 14:59
@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from a5595e0 to 868e3de Compare February 11, 2026 14:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates subsetting filter propagation to correctly handle self-referencing foreign keys by generating recursive CTE-based constraints, and adds unit/integration coverage using new departments/projects/assignments fixtures.

Changes:

  • Add recursive-CTE generation for self-referencing FK propagation in FilterPropagation.
  • Adjust FK processing order so non-self (cross-table) FK filters are applied before self-ref handling.
  • Extend unit + integration tests and add new schema/data fixtures for a 3-table chain with a self-ref FK.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
simple-anonymizer/src/scala/simpleanonymizer/FilterPropagation.scala Adds recursive CTE generation for self-referencing FK propagation and processes non-self FKs first.
tests/src/scala/simpleanonymizer/FilterPropagationTest.scala Adds unit tests asserting self-ref recursive CTE behavior (explicit + propagated filter, and no-filter case).
integration-tests/src/scala/simpleanonymizer/DbCopierIntegrationTest.scala Adds an integration test verifying cross-table propagation + self-ref exclusion behavior; updates existing tests to include/skip new tables.
integration-tests/src/resources/01-schema.sql Adds departments, projects (self-ref), and assignments tables for integration coverage.
integration-tests/src/resources/02-data.sql Seeds departments/projects/assignments data including a cross-dept parent edge case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from 868e3de to ea9e8ac Compare February 12, 2026 10:13
@nafg nafg requested a review from Copilot February 12, 2026 10:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from ea9e8ac to b34a7b0 Compare February 12, 2026 11:03
@nafg nafg requested a review from Copilot February 12, 2026 11:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

When subsetting with FilterPropagation, self-referencing FKs were
ignored because the table's own propagated filter hadn't been computed
yet (accumulated.get(table) was always None). This caused rows with
dangling self-ref FK pointers to be copied, violating FK integrity.

Fix: process cross-table FKs first, then for self-ref FKs use the
in-progress accumulator (which already has cross-table clauses) and
generate a recursive CTE to compute the transitive closure of
reachable rows through the self-ref chain.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from b34a7b0 to 6f1c3cc Compare February 12, 2026 11:26
@nafg nafg requested a review from Copilot February 12, 2026 11:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Introduce LogicalFK to group multi-column FK constraints and generate
correct tuple-based SQL (e.g., (fk1, fk2) IN (SELECT pk1, pk2 ...)).
Extract quotedCols/sqlTuple helpers to eliminate double-quoting and
repetition. Fix sqlTuple misuse in SELECT column lists where plain
comma-separated lists are needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 12, 2026 17:57
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Move qualified-name quoting into SlickProfile.quoteQualified (two
overloads) and LogicalFK/groupFKs into DbContext, adding a memoized
logicalForeignKeys lazy val. All consumers now share these abstractions
instead of duplicating the logic.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from 1143eb0 to c85c181 Compare February 12, 2026 19:10
When a table has multiple self-referencing FKs (e.g., manager_id and
mentor_id), compute each recursive CTE from the same base filter
(explicit + cross-ref) rather than chaining them sequentially, which
would nest one CTE inside another's filter.

Use unique CTE names (_reachable_<tablename>) and prefix CTE columns
with _r_ to avoid ambiguity with base table columns in the WHERE clause.

Also refactor tests to construct LogicalFK directly instead of routing
through MForeignKey + groupFKs, matching the actual API surface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 12, 2026 19:23
@nafg nafg force-pushed the self-ref-fk-filter-propagation branch from c85c181 to dcd1209 Compare February 12, 2026 19:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nafg nafg merged commit 5ad0ccc into master Feb 12, 2026
8 checks passed
@nafg nafg deleted the self-ref-fk-filter-propagation branch February 12, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments