Skip to content

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

Merged
borbelyr-kong merged 3 commits into
mainfrom
fix/dedupe-tax-codes-by-app-mapping
Jun 3, 2026
Merged

borbelyr-kong merged 3 commits into
mainfrom
fix/dedupe-tax-codes-by-app-mapping

Conversation

@borbelyr-kong

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

Copy link
Copy Markdown
Contributor

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. Invoice snapshots that embed tax code data are updated to reference the retained tax code.
  • Tests
    • Adds an automated migration test that seeds duplicate tax codes (including FK and embedded JSON references), runs the migration, and verifies references are updated and losers are soft-deleted.

@coderabbitai

coderabbitai Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

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: 179f15f5-f387-497c-a655-3aba3d4f0f78

📥 Commits

Reviewing files that changed from the base of the PR and between dcfaf7d and 8dd3dc1.

⛔ 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 due to trivial changes (1)
  • tools/migrate/migrations/20260527120000_dedupe_tax_codes_by_app_mapping.down.sql
🚧 Files skipped from review as they are similar to previous changes (2)
  • 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 and specifically summarizes the main change: deduplicating tax codes by app mapping, which is exactly what the migration and test implement.
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

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

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Actionable comments posted: 0

@borbelyr-kong
borbelyr-kong force-pushed the fix/dedupe-tax-codes-by-app-mapping branch from dcfaf7d to 8dd3dc1 Compare June 3, 2026 13:32
@borbelyr-kong
borbelyr-kong merged commit ba41efc into main Jun 3, 2026
27 checks passed
@borbelyr-kong
borbelyr-kong deleted the fix/dedupe-tax-codes-by-app-mapping branch June 3, 2026 14:14

This branch was previously deployed

1 inactive deployment
dev — 8dd3dc16 Deployed Jun 3, 2026 by borbelyr-kong via Migration Checks #14805
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.

2 participants