Skip to content

Feat/tax codes v3 add fk keys - #3941

Merged
turip merged 8 commits into
mainfrom
feat/tax-codes-v3-add-fk-keys
Mar 19, 2026
Merged

turip merged 8 commits into
mainfrom
feat/tax-codes-v3-add-fk-keys

Conversation

@borbelyr-kong

@borbelyr-kong borbelyr-kong commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Added tax code support across billing workflows, invoices, subscription items, plan/addon rate cards, and related entities to enable per-item tax classification and behavior.
  • Chores
    • Database migrations to add tax fields, indices, and foreign-key relationships for the new tax code support across affected tables.

@borbelyr-kong
borbelyr-kong requested a review from a team as a code owner March 13, 2026 12:54
@coderabbitai

coderabbitai Bot commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

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: b223399c-e25a-4c6b-a851-e5d129e3f6da

📥 Commits

Reviewing files that changed from the base of the PR and between fde3cb6 and 360fb25.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (1)
  • tools/migrate/migrations/20260313135547_tax-code-fk.up.sql

📝 Walkthrough

Walkthrough

This PR adds a TaxMixin (tax_code_id, tax_behavior) and connects many schemas to a new TaxCode entity via unique tax_code edges, and includes DB migrations to add/drop the corresponding columns, FKs and indexes across rate cards, billing entities, and subscription items.

Changes

Cohort / File(s) Summary
Tax Schema Core
openmeter/ent/schema/taxcode.go
Added TaxCode edges to many entities, introduced TaxMixin with Fields() (tax_code_id, tax_behavior) and Indexes().
Billing schemas
openmeter/ent/schema/billing.go
Applied TaxMixin{} and added edge.From("tax_code", TaxCode.Type).Ref(...).Field("tax_code_id").Unique() to: BillingWorkflowConfig, BillingCustomerOverride, BillingInvoiceLine, BillingInvoiceSplitLineGroup, BillingStandardInvoiceDetailedLine.
Rate cards & addons
openmeter/ent/schema/productcatalog.go, openmeter/ent/schema/addon.go
Added TaxMixin{} and tax_code edge to PlanRateCard and AddonRateCard.
Subscription items
openmeter/ent/schema/subscription.go
Added TaxMixin{} and unique tax_code edge to SubscriptionItem.
Migrations (DB)
tools/migrate/migrations/20260313135547_tax-code-fk.up.sql, .../20260313135547_tax-code-fk.down.sql
Up: add tax_behavior, tax_code_id, per-table FK (ON DELETE SET NULL NOT VALID), then VALIDATE; create per-table indexes. Down: drop indexes, constraints, and columns for the same tables.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

release-note/feature, area/billing

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 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: adding foreign key relationships for tax codes across multiple schema entities.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/tax-codes-v3-add-fk-keys
📝 Coding Plan
  • Generate coding plan for human review comments

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.

@borbelyr-kong borbelyr-kong added the release-note/ignore Ignore this change when generating release notes label Mar 13, 2026

@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: 4

🧹 Nitpick comments (1)
openmeter/ent/schema/taxcode.go (1)

14-14: Minor: import ordering.

The mixin import from entgo.io/ent/schema/mixin is placed after the local imports. Consider grouping it with the other entgo.io imports at lines 4-9 for consistency.

Suggested import organization
 import (
 	"entgo.io/ent"
 	"entgo.io/ent/dialect"
 	"entgo.io/ent/dialect/entsql"
 	"entgo.io/ent/schema/edge"
 	"entgo.io/ent/schema/field"
 	"entgo.io/ent/schema/index"
+	"entgo.io/ent/schema/mixin"

 	"github.com/openmeterio/openmeter/openmeter/productcatalog"
 	"github.com/openmeterio/openmeter/openmeter/taxcode"
 	"github.com/openmeterio/openmeter/pkg/framework/entutils"
-	"entgo.io/ent/schema/mixin"
 )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/taxcode.go` at line 14, The import ordering in
taxcode.go is inconsistent: move the "entgo.io/ent/schema/mixin" import so it is
grouped with the other entgo.io imports (the existing entgo.io/ent and
entgo.io/ent/schema/* imports) to match the project's import grouping
conventions; update the import block in taxcode.go so mixin appears alongside
the other entgo.io entries to keep imports organized and consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@openmeter/ent/schema/billing.go`:
- Around line 186-187: The create builders for CustomerOverride, InvoiceLine and
DetailedLine need to persist the new TaxMixin fields: update the builder chains
in the functions that construct customer overrides and invoice/detailed lines to
call the generated nillable setters (e.g. SetNillableTaxCodeID(...) and
SetNillableTaxBehavior(...)) using the source object's tax fields (e.g.
src.TaxCodeID, src.TaxBehavior or equivalent), so NULL is only stored when the
source is nil; apply this change to the create chains currently in the customer
override creator and the invoice line / detailed line creators so the new
relationship is actually set on insert.
- Around line 170-173: Add explicit indexes for the new tax_code_id foreign-key
fields so Postgres won't rely on table scans: for each schema that defines
edge.From("tax_code", TaxCode.Type).Ref(...).Field("tax_code_id") (e.g.,
billing_workflow_configs, billing_invoice_lines and the other schemas at the
referenced locations), add an index via index.Fields("tax_code_id") in the ent
schema definition (or ensure a separate migration creates those indexes). Update
the schema methods that declare tax_code_id to include index declaration so FK
checks/queries on TaxCode use the index.

In `@openmeter/ent/schema/productcatalog.go`:
- Around line 176-179: rateCardBulkCreate currently omits the TaxMixin fields so
PlanRateCard rows are created without tax_code_id and tax_behavior; update
rateCardBulkCreate in the plan adapter to read tax fields from the input
rate-card DTO and populate the PlanRateCard creation calls with tax_code_id (or
set the tax_code edge) and tax_behavior so the TaxMixin and tax_code
relationship are persisted for each created PlanRateCard. Ensure you use the
same field/edge names used in the schema (TaxMixin, tax_code, tax_code_id,
tax_behavior, PlanRateCard) when mapping incoming data to the entity creation.

In `@openmeter/ent/schema/subscription.go`:
- Around line 228-231: The Create() method in subscriptionitemrepo.go currently
omits setting the subscription item's tax fields, so tax_code_id and
tax_behavior (defined in the SubscriptionItem schema edge/fields) remain NULL;
update Create() to pull tax info from the rate card/rate data and call
SetTaxCodeID(...) and SetTaxBehavior(...) on the Ent builder (the same builder
where other fields are SetXxx) before Save(), ensuring you handle nullable
values (only call setters when data exists) and use the same identifiers
SetTaxCodeID and SetTaxBehavior to wire the schema fields through.

---

Nitpick comments:
In `@openmeter/ent/schema/taxcode.go`:
- Line 14: The import ordering in taxcode.go is inconsistent: move the
"entgo.io/ent/schema/mixin" import so it is grouped with the other entgo.io
imports (the existing entgo.io/ent and entgo.io/ent/schema/* imports) to match
the project's import grouping conventions; update the import block in taxcode.go
so mixin appears alongside the other entgo.io entries to keep imports organized
and consistent.
🪄 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: b6a261fb-3c39-415f-aa16-4d246a845425

📥 Commits

Reviewing files that changed from the base of the PR and between 3028994 and 7b8ee8c.

⛔ Files ignored due to path filters (60)
  • openmeter/ent/db/addonratecard.go is excluded by !**/ent/db/**
  • openmeter/ent/db/addonratecard/addonratecard.go is excluded by !**/ent/db/**
  • openmeter/ent/db/addonratecard/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/addonratecard_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/addonratecard_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/addonratecard_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride/billingcustomeroverride.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingcustomeroverride_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline/billinginvoiceline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup/billinginvoicesplitlinegroup.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoicesplitlinegroup_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline/billingstandardinvoicedetailedline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingstandardinvoicedetailedline_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig/billingworkflowconfig.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billingworkflowconfig_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/client.go is excluded by !**/ent/db/**
  • openmeter/ent/db/entmixinaccessor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/mutation.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard/planratecard.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/planratecard_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode/taxcode.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/taxcode_update.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (9)
  • openmeter/ent/schema/addon.go
  • openmeter/ent/schema/billing.go
  • openmeter/ent/schema/productcatalog.go
  • openmeter/ent/schema/subscription.go
  • openmeter/ent/schema/taxcode.go
  • tools/migrate/migrations/20260305143430_tax-code-fks.down.sql
  • tools/migrate/migrations/20260305143430_tax-code-fks.up.sql
  • tools/migrate/migrations/20260311101915_add_tax_behavior.down.sql
  • tools/migrate/migrations/20260311101915_add_tax_behavior.up.sql

Comment on lines +170 to +173
edge.From("tax_code", TaxCode.Type).
Ref("billing_workflow_configs").
Field("tax_code_id").
Unique(),

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.

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Schema-side index definitions for tax_code_id:"
rg -n -C2 'index\.Fields\("tax_code_id"\)|index\.Fields\("namespace",\s*"tax_code_id"\)' openmeter/ent/schema || true

echo
echo "SQL index definitions for tax_code_id:"
rg -n -i -C2 'create( unique)? index .*tax_code_id' -g '*.sql' || true

Repository: openmeterio/openmeter

Length of output: 151


🏁 Script executed:

# First, let's look at the actual billing.go file to see the foreign key definitions
wc -l openmeter/ent/schema/billing.go

Repository: openmeterio/openmeter

Length of output: 101


🏁 Script executed:

# Check the structure and find all tax_code_id references
rg -n "tax_code_id" openmeter/ent/schema/billing.go

Repository: openmeterio/openmeter

Length of output: 209


🏁 Script executed:

# Look for how indexes are typically defined in ent schema files
fd "\.go$" openmeter/ent/schema | head -5 | xargs -I {} rg -l "index\.Fields" {}

Repository: openmeterio/openmeter

Length of output: 241


🏁 Script executed:

# Check for any migration files related to tax_code
fd "\.sql$" | rg -i "tax_code|migration" | head -20

Repository: openmeterio/openmeter

Length of output: 1372


🏁 Script executed:

# Let's check an example schema file to see how indexes are defined
head -100 openmeter/ent/schema/addon.go | rg -A5 "Indexes"

Repository: openmeterio/openmeter

Length of output: 236


🏁 Script executed:

# Now let's look at the specific lines in billing.go where tax_code_id is defined
sed -n '165,180p' openmeter/ent/schema/billing.go
sed -n '270,280p' openmeter/ent/schema/billing.go
sed -n '490,505p' openmeter/ent/schema/billing.go
sed -n '710,725p' openmeter/ent/schema/billing.go
sed -n '960,975p' openmeter/ent/schema/billing.go

Repository: openmeterio/openmeter

Length of output: 1941


🏁 Script executed:

# Check the latest migration files for tax_code_id indexes
ls -ltr tools/migrate/migrations/*.sql | tail -10 | awk '{print $NF}' | xargs -I {} sh -c 'echo "=== {} ===" && rg -i "tax_code_id" {}'

Repository: openmeterio/openmeter

Length of output: 846


🏁 Script executed:

# Search for when tax_code was added to these entities - check git history or migration files
fd "\.sql$" tools/migrate/migrations | xargs rg -l "tax_code" | sort

Repository: openmeterio/openmeter

Length of output: 620


🏁 Script executed:

# Look at one of those migration files to see if indexes were added
cat tools/migrate/migrations/20250107094857_billing-tax-codes.up.sql 2>/dev/null || echo "File not found, let me search more broadly"

Repository: openmeterio/openmeter

Length of output: 107


🏁 Script executed:

# Find migration files that mention billing tables and tax_code
fd "\.sql$" tools/migrate/migrations | xargs rg -l "billing" | xargs rg -l "tax_code"

Repository: openmeterio/openmeter

Length of output: 506


🏁 Script executed:

# Check if there's an Indexes() method in billing.go schema for these entities
rg -n "func \(.*\) Indexes\(\)" openmeter/ent/schema/billing.go

Repository: openmeterio/openmeter

Length of output: 930


🏁 Script executed:

# Check the latest migration that adds tax_code-fks
cat tools/migrate/migrations/20260305143430_tax-code-fks.up.sql

Repository: openmeterio/openmeter

Length of output: 2440


🏁 Script executed:

# Check the specific Indexes() methods for the affected entities
sed -n '158,175p' openmeter/ent/schema/billing.go
sed -n '254,270p' openmeter/ent/schema/billing.go
sed -n '432,450p' openmeter/ent/schema/billing.go
sed -n '682,700p' openmeter/ent/schema/billing.go
sed -n '939,960p' openmeter/ent/schema/billing.go

Repository: openmeterio/openmeter

Length of output: 3039


Add indexes for the new tax_code_id foreign keys.

Postgres enforces these foreign keys but won't auto-index them. This means queries and FK checks on tax_codes updates/deletes could hit table scans—especially on billing_invoice_lines, which seems like a high-traffic table. Looking at the migration, the FKs are added without corresponding indexes. Consider adding schema indexes for these new columns (in the ent schema via index.Fields()), or verify they're in a separate migration. This matters for the performance-critical paths you're touching here.

Applies to: lines 170-173, 273-276, 494-497, 713-716, 964-967

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/billing.go` around lines 170 - 173, Add explicit indexes
for the new tax_code_id foreign-key fields so Postgres won't rely on table
scans: for each schema that defines edge.From("tax_code",
TaxCode.Type).Ref(...).Field("tax_code_id") (e.g., billing_workflow_configs,
billing_invoice_lines and the other schemas at the referenced locations), add an
index via index.Fields("tax_code_id") in the ent schema definition (or ensure a
separate migration creates those indexes). Update the schema methods that
declare tax_code_id to include index declaration so FK checks/queries on TaxCode
use the index.

Comment on lines +186 to 187
TaxMixin{},
}

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.

⚠️ Potential issue | 🟠 Major

Please wire these new tax fields into the current create builders.

Right now the schema can store tax_code_id / tax_behavior, but the existing inserts in openmeter/billing/adapter/customeroverride.go:35-40 and openmeter/billing/adapter/stdinvoicelines.go:84-104,453-472 still never set them. Because these mixin fields are nillable, ent won't fail here — it will just persist NULL on fresh overrides, invoice lines, and detailed lines, so the new relationship stays unused on those paths.

Also applies to: 311-312, 873-874

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/billing.go` around lines 186 - 187, The create builders
for CustomerOverride, InvoiceLine and DetailedLine need to persist the new
TaxMixin fields: update the builder chains in the functions that construct
customer overrides and invoice/detailed lines to call the generated nillable
setters (e.g. SetNillableTaxCodeID(...) and SetNillableTaxBehavior(...)) using
the source object's tax fields (e.g. src.TaxCodeID, src.TaxBehavior or
equivalent), so NULL is only stored when the source is nil; apply this change to
the create chains currently in the customer override creator and the invoice
line / detailed line creators so the new relationship is actually set on insert.

Comment on lines +176 to +179
edge.From("tax_code", TaxCode.Type).
Ref("plan_rate_cards").
Field("tax_code_id").
Unique(),

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.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check for tax field usage in plan adapter
rg -n "SetTaxCodeID|SetTaxBehavior|SetNillableTaxCodeID|SetNillableTaxBehavior" --type go openmeter/productcatalog/

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# Find the adapter file and examine rateCardBulkCreate function
find openmeter/productcatalog -name "*.go" -type f | xargs grep -l "rateCardBulkCreate" 2>/dev/null

Repository: openmeterio/openmeter

Length of output: 159


🏁 Script executed:

# Check what fields PlanRateCard has in the schema
cat -n openmeter/ent/schema/productcatalog.go | sed -n '1,250p'

Repository: openmeterio/openmeter

Length of output: 6308


🏁 Script executed:

# Look for TaxCodeID or TaxBehavior fields in PlanRateCard definition
ast-grep --pattern 'func (PlanRateCard) Fields() {
  $$$
}'

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# Find TaxMixin definition
find openmeter -name "*.go" -type f | xargs grep -l "type TaxMixin" 2>/dev/null

Repository: openmeterio/openmeter

Length of output: 96


🏁 Script executed:

# Look at the rateCardBulkCreate function in phase.go
grep -n "rateCardBulkCreate" openmeter/productcatalog/plan/adapter/phase.go -A 50

Repository: openmeterio/openmeter

Length of output: 2345


🏁 Script executed:

# Check TaxMixin definition and what fields it adds
cat -n openmeter/ent/schema/taxcode.go | grep -A 20 "type TaxMixin"

Repository: openmeterio/openmeter

Length of output: 533


🏁 Script executed:

# Check what asPlanRateCardRow function does and what it returns
grep -n "func asPlanRateCardRow" openmeter/productcatalog/plan/adapter/phase.go -A 30

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# Check the RateCard schema to see what tax-related fields exist
cat -n openmeter/ent/schema/productcatalog.go | grep -A 50 "type RateCard struct"

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# Search for RateCard schema definition
rg "type RateCard struct" --type go openmeter/ent/schema/

Repository: openmeterio/openmeter

Length of output: 120


🏁 Script executed:

# Find the asPlanRateCardRow function more carefully
rg -n "asPlanRateCardRow" openmeter/productcatalog/plan/adapter/ -A 40

Repository: openmeterio/openmeter

Length of output: 4963


🏁 Script executed:

# Check if TaxCodeID and TaxBehavior are being set in the adapter
rg -n "SetTaxCodeID|SetTaxBehavior|tax_code_id|tax_behavior" openmeter/productcatalog/plan/adapter/ --type go

Repository: openmeterio/openmeter

Length of output: 47


Tax fields not wired in the plan adapter.

The schema adds TaxMixin (which provides tax_code_id and tax_behavior fields) and the new tax_code relationship, but rateCardBulkCreate in openmeter/productcatalog/plan/adapter/phase.go doesn't set these fields when creating PlanRateCard entities. Since they're nullable, this won't cause failures, but you'll need to wire them through before tax code functionality can actually work with plans.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/productcatalog.go` around lines 176 - 179,
rateCardBulkCreate currently omits the TaxMixin fields so PlanRateCard rows are
created without tax_code_id and tax_behavior; update rateCardBulkCreate in the
plan adapter to read tax fields from the input rate-card DTO and populate the
PlanRateCard creation calls with tax_code_id (or set the tax_code edge) and
tax_behavior so the TaxMixin and tax_code relationship are persisted for each
created PlanRateCard. Ensure you use the same field/edge names used in the
schema (TaxMixin, tax_code, tax_code_id, tax_behavior, PlanRateCard) when
mapping incoming data to the entity creation.

Comment on lines +228 to +231
edge.From("tax_code", TaxCode.Type).
Ref("subscription_items").
Field("tax_code_id").
Unique(),

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.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if TaxCodeID or TaxBehavior setters are used anywhere in subscription repo
rg -n "SetTaxCodeID|SetTaxBehavior|SetNillableTaxCodeID|SetNillableTaxBehavior" --type go openmeter/subscription/

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# First, let's check the schema to verify nullable declarations
head -n 250 openmeter/ent/schema/subscription.go | tail -n 30

Repository: openmeterio/openmeter

Length of output: 954


🏁 Script executed:

# Now let's look at the subscriptionitemrepo.go Create() method mentioned
sed -n '123,151p' openmeter/subscription/repo/subscriptionitemrepo.go

Repository: openmeterio/openmeter

Length of output: 1450


🏁 Script executed:

# Search for any TODOs/FIXMEs related to tax in subscription code
rg -i "TODO|FIXME" --type go openmeter/subscription/ -A 2 | grep -i "tax\|TaxCode\|TaxBehavior" -B 2

Repository: openmeterio/openmeter

Length of output: 47


🏁 Script executed:

# Look for tax_code_id and tax_behavior field definitions in schema
rg -n "tax_code_id|tax_behavior|TaxCodeID|TaxBehavior" openmeter/ent/schema/subscription.go -B 2 -A 2

Repository: openmeterio/openmeter

Length of output: 194


🏁 Script executed:

# Check if these are new fields by looking at the full SubscriptionItem schema
rg -n "func.*SubscriptionItem.*Fields" openmeter/ent/schema/subscription.go -A 100

Repository: openmeterio/openmeter

Length of output: 3409


The tax_code_id and tax_behavior fields aren't wired up yet in the adapter.

The schema edge and field are defined, but looking at subscriptionitemrepo.go's Create() method (lines 123-151), there's no SetTaxCodeID or SetTaxBehavior being called. Since these columns are nullable, new subscription items will just have NULL values for these fields.

Is this intentional for a follow-up, or should we wire through the tax info from the rate card now?

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/subscription.go` around lines 228 - 231, The Create()
method in subscriptionitemrepo.go currently omits setting the subscription
item's tax fields, so tax_code_id and tax_behavior (defined in the
SubscriptionItem schema edge/fields) remain NULL; update Create() to pull tax
info from the rate card/rate data and call SetTaxCodeID(...) and
SetTaxBehavior(...) on the Ent builder (the same builder where other fields are
SetXxx) before Save(), ensuring you handle nullable values (only call setters
when data exists) and use the same identifiers SetTaxCodeID and SetTaxBehavior
to wire the schema fields through.

@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.

🧹 Nitpick comments (1)
openmeter/ent/schema/taxcode.go (1)

1-84: Reminder: Regenerate code and migrations.

As per coding guidelines, don't forget to:

  1. Run make generate to regenerate all Ent code
  2. Run atlas migrate --env local diff <migration-name> to generate the database migrations for the new FK columns and fields

Based on learnings: "Regenerate Go code by running make generate after editing ent schema files in openmeter/ent/schema/" and "Use atlas migrate --env local diff <migration-name> to generate database migrations after editing ent schema."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ent/schema/taxcode.go` around lines 1 - 84, You added/modified Ent
schema types (TaxCode, TaxMixin, TaxCodeAppMappingsValueScanner and fields like
tax_code_id and tax_behavior) but haven't regenerated code or created DB
migrations; run make generate to regenerate Ent code and then run atlas migrate
--env local diff <migration-name> to produce migrations for the new/changed FK
columns and JSON/enum fields so the code and database schema stay in sync.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@openmeter/ent/schema/taxcode.go`:
- Around line 1-84: You added/modified Ent schema types (TaxCode, TaxMixin,
TaxCodeAppMappingsValueScanner and fields like tax_code_id and tax_behavior) but
haven't regenerated code or created DB migrations; run make generate to
regenerate Ent code and then run atlas migrate --env local diff <migration-name>
to produce migrations for the new/changed FK columns and JSON/enum fields so the
code and database schema stay in sync.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 355a3ede-4f21-49c2-9df2-9690939e68ca

📥 Commits

Reviewing files that changed from the base of the PR and between 7b8ee8c and 6e3c322.

📒 Files selected for processing (1)
  • openmeter/ent/schema/taxcode.go

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tools/migrate/migrations/20260313135547_tax-code-fk.up.sql`:
- Around line 2-30: Split each foreign-key addition into two operations to avoid
long locks: add the FK constraint with NOT VALID, then run a separate ALTER
TABLE ... VALIDATE CONSTRAINT for each of the constraints listed (e.g.
addon_rate_cards_tax_codes_addon_rate_cards,
billing_customer_overrides_tax_codes_billing_customer_overrides,
billing_invoice_lines_tax_codes_billing_invoice_lines,
billing_invoice_split_line_groups_tax_codes_billing_invoice_spl,
billing_standard_invoice_detailed_lines_tax_codes_billing_stand,
billing_workflow_configs_tax_codes_billing_workflow_configs,
plan_rate_cards_tax_codes_plan_rate_cards,
subscription_items_tax_codes_subscription_items); keep the column additions
(tax_behavior, tax_code_id) and index creation as-is, only change the ADD
CONSTRAINT clauses to be NOT VALID and add a separate VALIDATE CONSTRAINT step
for each named constraint.
🪄 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: 2f751575-265f-4bc1-9448-a195a0e1906a

📥 Commits

Reviewing files that changed from the base of the PR and between 6e3c322 and fde3cb6.

⛔ Files ignored due to path filters (2)
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (3)
  • openmeter/ent/schema/taxcode.go
  • tools/migrate/migrations/20260313135547_tax-code-fk.down.sql
  • tools/migrate/migrations/20260313135547_tax-code-fk.up.sql

turip
turip previously approved these changes Mar 19, 2026
@turip
turip merged commit 4971c89 into main Mar 19, 2026
22 of 23 checks passed
@turip
turip deleted the feat/tax-codes-v3-add-fk-keys branch March 19, 2026 16:28

This branch was previously deployed

1 inactive deployment
dev — 4637d962 Deployed Mar 19, 2026 by borbelyr-kong via Migration Checks #12665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants