Repository navigation
chore: add flat fee run db tables - #4349
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (33)
📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR moves flat-fee realization persistence from charge-scoped tables to run-scoped tables, adds RealizationRunType and adapter methods for creating/fetching/updating a charge's current realization run, updates all adapters (credits, payments, invoiced usage, detailed lines) to persist to run-scoped entities, and maps realizations from the current run. ChangesFlat-Fee Run-Scoped Realization Refactor
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
openmeter/billing/charges/flatfee/adapter/charge.go (1)
140-144: 🏗️ Heavy liftBatch current-run initialization to reduce bulk-create DB overhead.
Nice migration step overall. The new loop does per-charge run insert + charge update, which adds a lot of round-trips for larger batches. Consider a set-based/bulk approach for run creation + current-run linkage so this path stays fast under load.
As per coding guidelines, "Performance should be a priority... database operations... should be vetted for potential performance bottlenecks."
🤖 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 `@openmeter/billing/charges/flatfee/adapter/charge.go` around lines 140 - 144, The loop doing per-entity inserts via tx.createCurrentRun(ctx, entity, totals.Totals{}) causes many DB round-trips; instead build all run records in-memory for the incoming entities, perform a single bulk insert (e.g. implement tx.createCurrentRunsBulk(ctx, runs) or use the DB driver's bulk insert mechanism) to get back all created run IDs, then perform a single bulk update to set each charge's current_run_id (e.g. one UPDATE with WHERE id IN (...) or a CASE mapping) within the same transaction; replace the per-entity calls in the current function with the bulk insert + bulk update sequence and ensure transactional integrity and returned errors are handled the same way.openmeter/billing/charges/flatfee/adapter/detailedline.go (1)
145-169: 💤 Low valueTiny cleanup opportunity:
chargeIDis only used for itsNamespace.Now that
RunIDcarries the run linkage,buildDetailedLineCreateonly pullsNamespaceout ofchargeID. Passing the fullmeta.ChargeIDis a bit misleading — anamespace string, runID stringsignature would more accurately reflect what the helper actually needs. Totally optional, just a readability nudge.♻️ Suggested signature tweak
-func buildDetailedLineCreate(db *entdb.Client, chargeID meta.ChargeID, runID string, line flatfee.DetailedLine) (*entdb.ChargeFlatFeeRunDetailedLineCreate, error) { +func buildDetailedLineCreate(db *entdb.Client, namespace string, runID string, line flatfee.DetailedLine) (*entdb.ChargeFlatFeeRunDetailedLineCreate, error) { if line.ID == "" { line.ID = ulid.Make().String() } create := db.ChargeFlatFeeRunDetailedLine.Create(). SetID(line.ID). - SetNamespace(chargeID.Namespace). + SetNamespace(namespace). SetRunID(runID). SetPricerReferenceID(line.ChildUniqueReferenceID)🤖 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 `@openmeter/billing/charges/flatfee/adapter/detailedline.go` around lines 145 - 169, buildDetailedLineCreate currently accepts a meta.ChargeID but only uses its Namespace; change the signature of buildDetailedLineCreate to accept namespace string (and runID string) instead of chargeID to make intent clearer, update all call sites that invoke buildDetailedLineCreate to pass charge.Namespace where they previously passed a meta.ChargeID, and ensure the body replaces uses of chargeID.Namespace with the new namespace parameter (preserving behavior for SetNamespace). Keep the function name, internals like stddetailedline.Create and the handling of CreditsApplied and TaxConfig unchanged.
🤖 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.
Nitpick comments:
In `@openmeter/billing/charges/flatfee/adapter/charge.go`:
- Around line 140-144: The loop doing per-entity inserts via
tx.createCurrentRun(ctx, entity, totals.Totals{}) causes many DB round-trips;
instead build all run records in-memory for the incoming entities, perform a
single bulk insert (e.g. implement tx.createCurrentRunsBulk(ctx, runs) or use
the DB driver's bulk insert mechanism) to get back all created run IDs, then
perform a single bulk update to set each charge's current_run_id (e.g. one
UPDATE with WHERE id IN (...) or a CASE mapping) within the same transaction;
replace the per-entity calls in the current function with the bulk insert + bulk
update sequence and ensure transactional integrity and returned errors are
handled the same way.
In `@openmeter/billing/charges/flatfee/adapter/detailedline.go`:
- Around line 145-169: buildDetailedLineCreate currently accepts a meta.ChargeID
but only uses its Namespace; change the signature of buildDetailedLineCreate to
accept namespace string (and runID string) instead of chargeID to make intent
clearer, update all call sites that invoke buildDetailedLineCreate to pass
charge.Namespace where they previously passed a meta.ChargeID, and ensure the
body replaces uses of chargeID.Namespace with the new namespace parameter
(preserving behavior for SetNamespace). Keep the function name, internals like
stddetailedline.Create and the handling of CreditsApplied and TaxConfig
unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f5ba59fc-e76a-4b05-819c-c3dad8b635c4
⛔ Files ignored due to path filters (80)
openmeter/ent/db/billinginvoiceline.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline/billinginvoiceline.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline/where.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_create.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_query.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeecreditallocations/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeecreditallocations_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeecreditallocations_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeecreditallocations_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeedetailedline/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeedetailedline_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeedetailedline_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeedetailedline_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeinvoicedusage/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeinvoicedusage_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeinvoicedusage_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeinvoicedusage_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeepayment/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeepayment_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun/chargeflatfeerun.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerun_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations/chargeflatfeeruncreditallocations.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruncreditallocations_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline/chargeflatfeerundetailedline.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerundetailedline_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage/chargeflatfeeruninvoicedusage.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeruninvoicedusage_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment/chargeflatfeerunpayment.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeerunpayment_update.gois excluded by!**/ent/db/**openmeter/ent/db/client.gois excluded by!**/ent/db/**openmeter/ent/db/cursor.gois excluded by!**/ent/db/**openmeter/ent/db/ent.gois excluded by!**/ent/db/**openmeter/ent/db/entmixinaccessor.gois excluded by!**/ent/db/**openmeter/ent/db/expose.gois excluded by!**/ent/db/**openmeter/ent/db/hook/hook.gois excluded by!**/ent/db/**openmeter/ent/db/migrate/schema.gois excluded by!**/ent/db/**openmeter/ent/db/mutation.gois excluded by!**/ent/db/**openmeter/ent/db/paginate.gois excluded by!**/ent/db/**openmeter/ent/db/predicate/predicate.gois excluded by!**/ent/db/**openmeter/ent/db/runtime.gois excluded by!**/ent/db/**openmeter/ent/db/setorclear.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode/taxcode.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode/where.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode_create.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode_query.gois excluded by!**/ent/db/**openmeter/ent/db/taxcode_update.gois excluded by!**/ent/db/**openmeter/ent/db/tx.gois excluded by!**/ent/db/**
📒 Files selected for processing (12)
openmeter/billing/charges/flatfee/adapter/charge.goopenmeter/billing/charges/flatfee/adapter/credits.goopenmeter/billing/charges/flatfee/adapter/detailedline.goopenmeter/billing/charges/flatfee/adapter/detailedline_test.goopenmeter/billing/charges/flatfee/adapter/mapper.goopenmeter/billing/charges/flatfee/adapter/payment.goopenmeter/billing/charges/flatfee/adapter/realizationrun.goopenmeter/billing/charges/flatfee/adapter/usage.goopenmeter/billing/charges/flatfee/realizationrun.goopenmeter/ent/schema/billing.goopenmeter/ent/schema/chargesflatfee.goopenmeter/ent/schema/taxcode.go
Overview
This patch introduces flat-fee runs. This is needed due to credit notes/pro-rating support, which requires to store different versions of the flat fee (one current, and all the old pending pro-rating/creditnote ones).
This patch only migrates credit_only data, as credit_then_invoice is not yet operational for flat fee.
Notes for reviewer
Summary by CodeRabbit
New Features
Bug Fixes / Data Integrity