Skip to content

refactor(credit): migrate invoice purchase statuses - #4860

Merged
turip merged 1 commit into
mainfrom
feat/migrate-invoice-credit-purchase-status
Aug 6, 2026
Merged

turip merged 1 commit into
mainfrom
feat/migrate-invoice-credit-purchase-status

Conversation

@turip

@turip turip commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Summary

  • migrate legacy invoice-settled credit purchases from the generic active status using their durable credit-grant and invoiced-payment realizations
  • keep both the detailed lifecycle status and short meta status consistent during the backfill
  • fail the migration when an active invoice purchase has no valid credit grant or has an unsupported payment status
  • remove the runtime legacy-status mapper and replace its compatibility tests with focused migration coverage

Why

This is the migration follow-up to #4859. Moving compatibility into the database lets the invoice credit-purchase state machine operate exclusively on explicit detailed states before cost-basis pinning is implemented.

Ticket: OM-436

Impact

The migration maps invoice purchases as follows:

  • no invoiced payment: active.payment.pending
  • authorized invoiced payment: active.payment.authorized
  • settled invoiced payment: final

The data migration is intentionally not reversed by its down migration because collapsing detailed states would lose lifecycle information.

Validation

  • make migrate-check-lint
  • make migrate-check-validate
  • POSTGRES_HOST=127.0.0.1 go test ./tools/migrate -run '^(TestMigrateInvoiceCreditPurchaseStatuses|TestUpDownUp)$' -count=1
  • go test -tags=dynamic ./openmeter/billing/charges/creditpurchase/service/... -count=1
  • focused lint-go-fast and lint-go-style checks in the Nix CI shell

Summary by CodeRabbit

  • Data Updates
    • Existing invoice credit purchases are now assigned more accurate statuses based on their payment progress.
    • Pending, authorized, settled, pre-detailed, and externally processed purchases are handled consistently.
    • Records with missing credit grants or unsupported payment states are identified during migration rather than updated incorrectly.
    • Detailed statuses are preserved during future rollback operations.

Greptile Summary

The PR migrates legacy invoice-settled credit purchases from the generic active status to detailed lifecycle states, then removes the corresponding runtime compatibility mapper.

  • Derives pending, authorized, and final states from durable invoiced-payment realizations.
  • Validates credit grants and payment statuses before updating data.
  • Keeps detailed and meta statuses synchronized and adds focused migration coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
tools/migrate/migrations/20260806065936_migrate_invoice_credit_purchase_statuses.up.sql Validates legacy invoice purchases and atomically maps their detailed and meta statuses from durable realizations.
tools/migrate/invoice_credit_purchase_status_test.go Covers pending, authorized, settled, already-detailed, external, and invalid legacy migration cases.
openmeter/billing/charges/creditpurchase/service/invoice.go Removes the runtime legacy-status mapper after compatibility is moved into the database migration.
tools/migrate/migrations/20260806065936_migrate_invoice_credit_purchase_statuses.down.sql Intentionally preserves migrated detailed lifecycle information during rollback.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Legacy invoice purchase: active] --> B{Valid active credit grant?}
    B -- No --> X[Abort migration]
    B -- Yes --> C{Invoiced payment realization}
    C -- None --> D[active.payment.pending / active]
    C -- authorized --> E[active.payment.authorized / active]
    C -- settled --> F[final / final]
    C -- unsupported --> X
Loading

Reviews (2): Last reviewed commit: "refactor(credit): migrate invoice purcha..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds a SQL migration for invoice credit purchase statuses, validates related grants and payment states, and removes the equivalent runtime mapping from state-machine construction. PostgreSQL tests cover successful mappings and validation failures.

Changes

Invoice credit purchase status migration

Layer / File(s) Summary
SQL status migration and PostgreSQL coverage
tools/migrate/migrations/20260806065936_migrate_invoice_credit_purchase_statuses.up.sql, tools/migrate/migrations/20260806065936_migrate_invoice_credit_purchase_statuses.down.sql, tools/migrate/invoice_credit_purchase_status_test.go
The migration validates active credit grants and supported payment states, then writes detailed and generic statuses. Tests cover pending, authorized, settled, pre-detailed, external, missing-grant, and unsupported-status cases.
Runtime legacy mapping removal
openmeter/billing/charges/creditpurchase/service/invoice.go, openmeter/billing/charges/creditpurchase/service/invoice_test.go
The state-machine constructor no longer maps legacy active invoice credit purchases. The related helper, dependency, and tests are removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MigrationTest
  participant PostgreSQL
  participant StatusMigration
  MigrationTest->>PostgreSQL: Create temporary tables and test data
  MigrationTest->>StatusMigration: Execute migration
  StatusMigration->>PostgreSQL: Validate grants and payment states
  StatusMigration->>PostgreSQL: Update purchase statuses
  PostgreSQL-->>MigrationTest: Return migrated rows or validation errors
Loading

Possibly related PRs

Suggested labels: kind/refactor

Suggested reviewers: tothandras, mark-vass-konghq

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: migrating invoice credit-purchase statuses.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/migrate-invoice-credit-purchase-status

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.

Base automatically changed from feat/invoice-credit-purchase-state-machine to main August 6, 2026 08:18
@turip
turip force-pushed the feat/migrate-invoice-credit-purchase-status branch from 3baa10b to 31615aa Compare August 6, 2026 08:21
@turip
turip marked this pull request as ready for review August 6, 2026 08:21
@turip
turip requested a review from a team as a code owner August 6, 2026 08:21
@turip turip added release-note/misc Miscellaneous changes area/billing labels Aug 6, 2026
@turip
turip merged commit d8a032a into main Aug 6, 2026
29 of 32 checks passed
@turip
turip deleted the feat/migrate-invoice-credit-purchase-status branch August 6, 2026 09:28
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