Skip to content

Apply table/schema filters when fetching FK constraints#30

Open
teknogeek0 wants to merge 1 commit into
mainfrom
fix/fk-constraint-filter-exclusion
Open

Apply table/schema filters when fetching FK constraints#30
teknogeek0 wants to merge 1 commit into
mainfrom
fix/fk-constraint-filter-exclusion

Conversation

@teknogeek0
Copy link
Copy Markdown
Collaborator

Summary

  • schema_list_fk_constraints accepted a SourceFilters * parameter but never used it, causing all FK constraints to be fetched regardless of --exclude-table or --exclude-schema filters
  • When pgcopydb reached the post-restore FK constraint creation step, it would attempt constraints where the referencing or referenced table didn't exist on the target, causing a hard failure
  • No data is affected — only the final ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY step failed

Changes

  • Added listSourceFKConstraintsSQL[] with filter-aware SQL variants following the same pattern as listSourceIndexesSQL
  • The EXCL variant left-joins both the referencing table and the referenced table against filter_exclude_schema and filter_exclude_table, skipping constraints where either side is excluded
  • The INCL variant inner-joins both tables against filter_include_only_table
  • prepareFilters is already idempotent (filters->prepared guard), so calling it from both schema_list_all_indexes and schema_list_fk_constraints in the same session is safe

Test plan

  • Added foo.tbl_ref_target (excluded by table filter) and foo.tbl_with_fk_to_excluded_tbl (FK referencing the excluded table) to the filtering test fixture
  • New test 5-fk-constraints.sql verifies both the excluded-schema and excluded-table FK scenarios result in count 0 on the target
  • Full test suite (DOCKER=podman PGVERSION=16 make tests) passes

schema_list_fk_constraints ignored its SourceFilters parameter,
fetching all FK constraints regardless of --exclude-table or
--exclude-schema filters. This caused the post-restore constraint
step to fail when either the referencing or referenced table was
absent from the target.

Add listSourceFKConstraintsSQL[] with filter-aware variants matching
the pattern used by listSourceIndexesSQL. The EXCL variant excludes
any FK constraint where either the referencing or the referenced
table is in an excluded schema or table list.

Add a filtering test covering both the excluded-schema and
excluded-table cases.
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