Repository navigation
chore: clean up gathering invoice detailed lines - #3799
Conversation
📝 WalkthroughWalkthroughThe PR adds a transactional migration that backs up and removes gathering invoice details with dependency validation. It adds supporting indexes, excludes migration backup tables from Atlas checks, and documents the non-restorable rollback. ChangesGathering invoice cleanup
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Migration
participant BillingTables
participant BackupTable
Migration->>BillingTables: select gathering invoice detail rows
BillingTables-->>Migration: matching rows
Migration->>BackupTable: store source rows as JSONB
Migration->>BillingTables: validate delete dependencies
BillingTables-->>Migration: dependency results
Migration->>BillingTables: delete selected details, lines, and configs
Migration->>Migration: commit transaction
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
342754d to
e42f796
Compare
e42f796 to
69533c6
Compare
6869774 to
73ed672
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/20260716150455_cleanup_gathering_invoice_details.up.sql`:
- Around line 1-16: The cleanup migration currently creates temporary helper
indexes inside its transaction, unnecessarily holding write-blocking locks.
Remove the helper index creation and corresponding drops from this migration,
create and drop them in separate migrations using CREATE/DROP INDEX
CONCURRENTLY, and leave this file containing only the transactional cleanup.
- Around line 26-305: Materialize and lock the gathering-invoice deletion cohort
before the backup statements, using a temporary or equivalent cohort keyed by
invoice-line identity and namespace. Update every backup INSERT, cascade
validation DO block, and DELETE in this migration to join that single cohort
rather than independently re-scanning live invoice and line tables, preserving
the existing status/type predicates and ensuring the cohort is locked for the
migration’s full operation.
🪄 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: e065455e-b887-4d32-99ca-03675459dd36
⛔ Files ignored due to path filters (1)
tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (3)
atlas.hcltools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.down.sqltools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.up.sql
73ed672 to
c886c35
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/20260716150454_create_gathering_detail_cleanup_indexes.up.sql`:
- Around line 1-14: Update all four migration files to use concurrent index
operations: create indexes concurrently in
tools/migrate/migrations/20260716150454_create_gathering_detail_cleanup_indexes.up.sql
and
tools/migrate/migrations/20260716150456_drop_gathering_detail_cleanup_indexes.down.sql,
and use separate DROP INDEX CONCURRENTLY statements in
tools/migrate/migrations/20260716150454_create_gathering_detail_cleanup_indexes.down.sql
and
tools/migrate/migrations/20260716150456_drop_gathering_detail_cleanup_indexes.up.sql.
Mark these migrations for execution outside a transaction via the migration
runner’s transaction-mode directive, then regenerate them through Atlas and
verify the generated statements.
In
`@tools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.up.sql`:
- Around line 69-87: The config backup inserts must emit each (source_table,
namespace, id) only once. Update both config backup queries, including the
INSERT selecting from billing_invoice_flat_fee_line_configs, to deduplicate rows
using DISTINCT, DISTINCT ON, or an equivalent grouping while preserving the
existing filtering and selected backup data.
🪄 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: 964c5c6b-d5cf-4110-8930-58683904b88a
⛔ Files ignored due to path filters (1)
tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (7)
atlas.hcltools/migrate/migrations/20260716150454_create_gathering_detail_cleanup_indexes.down.sqltools/migrate/migrations/20260716150454_create_gathering_detail_cleanup_indexes.up.sqltools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.down.sqltools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.up.sqltools/migrate/migrations/20260716150456_drop_gathering_detail_cleanup_indexes.down.sqltools/migrate/migrations/20260716150456_drop_gathering_detail_cleanup_indexes.up.sql
🚧 Files skipped from review as they are similar to previous changes (2)
- tools/migrate/migrations/20260716150455_cleanup_gathering_invoice_details.down.sql
- atlas.hcl
Overview
Remove invalid detailed rows attached to gathering invoice lines before the remaining standard invoices are migrated to billing schema level 2.
The code before the billing refactor in Jan 2026 have sometimes persisted detailed lines, for gathering invoices which is an invalid state.
This patch cleans it up as preparation for invoice schema 2 migration.
Changes
om_migration_backup_20260716150455_gathering_details, including its source table, namespace, ID, and full JSON representation.The cleanup does not acquire billing customer locks because these invalid gathering details are not loaded or mutated by billing.
The migration is intentionally irreversible because the removed legacy data cannot be safely restored automatically. The persistent backup table remains available for manual recovery.
Validation
atlas migrate --env local lint --latest 2atlas migrate --env local validateatlas migrate --env local diff migrate-checkgit diff --checkSummary by CodeRabbit
Data Maintenance
Performance
Migration Safety
Greptile Summary
This PR removes invalid detailed rows from gathering invoices before the remaining schema-level-2 migration. The main changes are:
Confidence Score: 5/5
No additional blocking issue qualifies for this follow-up review.
Important Files Changed
Reviews (5): Last reviewed commit: "fix: make gathering detail cleanup rerun..." | Re-trigger Greptile
Context used: