Skip to content

fix(taxcode): dedupe tax_codes by app mapping#4448

Open
borbelyr-kong wants to merge 3 commits into
mainfrom
fix/dedupe-tax-codes-by-app-mapping
Open

fix(taxcode): dedupe tax_codes by app mapping#4448
borbelyr-kong wants to merge 3 commits into
mainfrom
fix/dedupe-tax-codes-by-app-mapping

Conversation

@borbelyr-kong
Copy link
Copy Markdown
Contributor

@borbelyr-kong borbelyr-kong commented May 27, 2026

Summary by CodeRabbit

  • Chores
    • One-way deduplication of tax codes: duplicate entries are soft-deleted and all billing/pricing/subscription and organization references are repointed to retained entries, including embedded JSON snapshots in invoice lines.
  • Tests
    • Adds an automated migration test that seeds duplicates (FKs and JSON snapshots), runs the migration, and verifies references are updated and losers are soft-deleted.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5319a4f2-7f01-4f29-bd92-b9bd4e1b2783

📥 Commits

Reviewing files that changed from the base of the PR and between 9da8058 and dcfaf7d.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (3)
  • tools/migrate/dedupe_tax_codes_by_app_mapping_test.go
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.down.sql
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.up.sql
🚧 Files skipped from review as they are similar to previous changes (3)
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.down.sql
  • tools/migrate/dedupe_tax_codes_by_app_mapping_test.go
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.up.sql

📝 Walkthrough

Walkthrough

Adds a DB migration (20260527120000) that deduplicates tax_codes sharing identical app_mappings by selecting deterministic winners, repointing FK/JSONB references and rewriting invoice-line snapshots, soft-deleting losers, plus an end-to-end test and a documented no-op down migration.

Changes

Tax Code Deduplication Migration

Layer / File(s) Summary
Up migration: deduplication, repointing, snapshot rewrite
tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.up.sql
Expands tax_codes.app_mappings, ranks duplicates (system-managed first, then oldest created_at, then smallest id), materializes loser→winner mappings, sanity-checks them, indexes the mapping, updates FK tax_code_id and JSONB tax_config.tax_code_id across billing/charge/subscription/plan/addon/org tables, rewrites billing_invoice_lines.tax_config.tax_code snapshots matching losers, soft-deletes loser rows, drops the temp table, and commits.
Down migration: irreversibility documentation
tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.down.sql
Down migration is an explicit no-op with comments explaining rollback is infeasible because original loser IDs are not retained after repointing/soft-deletes.
Comprehensive test: fixtures and validation
tools/migrate/dedupe_tax_codes_by_app_mapping_test.go
End-to-end test seeds controlled duplicate and singleton tax_codes (including a system-managed winner), inserts dependent rows referencing a loser via FKs and JSONB (and invoice lines with embedded tax_code snapshots), runs the fixture and dedupe migrations, and asserts losers are soft-deleted and all references and embedded snapshots point to the expected winners.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

area/product-catalog

Suggested reviewers

  • turip
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: fixing duplicate tax_codes by deduplicating them based on app mapping, which aligns with the three files implementing the migration logic and comprehensive tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dedupe-tax-codes-by-app-mapping

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tools/migrate/dedupe_tax_codes_by_app_mapping_test.go (1)

296-334: 🏗️ Heavy lift

Great start—consider asserting all FK targets touched by the migration

The test currently verifies a subset of repointed columns, while the migration updates many FK-bearing tables. Expanding this to all updated targets would prevent silent regressions if one UPDATE gets dropped or edited later.

As per coding guidelines: "**/*_test.go: Make sure the tests are comprehensive and cover the changes."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tools/migrate/dedupe_tax_codes_by_app_mapping_test.go` around lines 296 -
334, The test only asserts a subset of foreign-key repoints; update the test in
dedupe_tax_codes_by_app_mapping_test.go to assert every FK target the migration
modifies using the same pattern as the existing checks (use
db.QueryRow(...).Scan(...) and require.Equal(t, group1Winner, ...)). Locate the
assertion block using the variables wfcRowID, prcRowID, subItemID, flatFeeID,
orgDTCID and add equivalent queries/assertions for each additional table/column
the migration updates so all FK-bearing tables are covered by the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.up.sql`:
- Around line 41-46: The new `_tax_code_dedup_map` can contain the same loser_id
mapped to multiple distinct winner_id values due to multiple TaxCodeAppMappings;
add a post-population check that detects any loser_id with more than one
distinct winner_id (e.g., GROUP BY loser_id HAVING COUNT(DISTINCT winner_id) >
1) and immediately abort the migration with a clear RAISE EXCEPTION message if
any rows are found; perform this check before any UPDATE ... FROM
_tax_code_dedup_map and before creating any FK/indexes so the migration fails
fast and deterministically when the mapping isn’t one-to-one.

---

Nitpick comments:
In `@tools/migrate/dedupe_tax_codes_by_app_mapping_test.go`:
- Around line 296-334: The test only asserts a subset of foreign-key repoints;
update the test in dedupe_tax_codes_by_app_mapping_test.go to assert every FK
target the migration modifies using the same pattern as the existing checks (use
db.QueryRow(...).Scan(...) and require.Equal(t, group1Winner, ...)). Locate the
assertion block using the variables wfcRowID, prcRowID, subItemID, flatFeeID,
orgDTCID and add equivalent queries/assertions for each additional table/column
the migration updates so all FK-bearing tables are covered by the test.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b083803b-9651-4dd3-8da1-bdf425ee6afb

📥 Commits

Reviewing files that changed from the base of the PR and between 5b6909a and 034ed70.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (3)
  • tools/migrate/dedupe_tax_codes_by_app_mapping_test.go
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.down.sql
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.up.sql

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 28, 2026

Actionable comments posted: 0

@borbelyr-kong borbelyr-kong force-pushed the fix/dedupe-tax-codes-by-app-mapping branch from 9da8058 to dcfaf7d Compare May 28, 2026 12:23
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 28, 2026

Actionable comments posted: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant