Skip to content

feat(api): add organization default tax codes schema and migration - #4294

Merged
borbelyr-kong merged 2 commits into
mainfrom
feat/org-tax-config-ent
May 6, 2026
Merged

borbelyr-kong merged 2 commits into
mainfrom
feat/org-tax-config-ent

Conversation

@borbelyr-kong

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

Copy link
Copy Markdown
Contributor

@coderabbitai

Summary by CodeRabbit

  • New Features
    • Organizations can now configure default tax codes at the organization level for invoicing and credit grant operations, streamlining tax code management across transactions.

@coderabbitai

coderabbitai Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR introduces a new OrganizationDefaultTaxCodes entity to store organization-level default tax code references for invoicing and credit grant workflows, including the corresponding ent schema, reverse relationship edges, and database migrations.

Changes

OrganizationDefaultTaxCodes Entity Introduction

Layer / File(s) Summary
Data Shape
openmeter/ent/schema/organizationdefaulttaxcodes.go
New schema defines OrganizationDefaultTaxCodes with namespace (immutable), invoicing_tax_code_id, and credit_grant_tax_code_id fields, plus IDMixin and TimeMixin. Includes a unique index on namespace.
Relationship Definitions
openmeter/ent/schema/taxcode.go
Two reverse edges added to TaxCode: organization_default_invoicing and organization_default_credit_grant, each referencing the new entity with appropriate storage keys.
Database Schema
tools/migrate/migrations/20260506082128_add_organization_default_tax_codes.up.sql, .down.sql
Migration creates organization_default_tax_codes table with soft-delete support, foreign keys to tax_codes, and unique indexes on id and namespace. Down migration reverses all changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • openmeterio/openmeter#4280: Introduces API types, handlers, and endpoints for the OrganizationDefaultTaxCodes model that this PR adds at the schema and database layer.

Suggested reviewers

  • turip
  • tothandras
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 describes the main change: adding organization default tax codes schema and migration, which matches the actual changes in the PR.
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.

✏️ 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 feat/org-tax-config-ent

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: 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 `@openmeter/ent/schema/organizationdefaulttaxcodes.go`:
- Around line 53-58: The unique index on namespace in
OrganizationDefaultTaxCodes.Indexes() must include a partial predicate to ignore
soft-deleted rows; update the Index definition to add
entsql.IndexWhere("deleted_at IS NULL") to the unique index (matching the
pattern used in TaxCode.Indexes()), then re-run make generate and regenerate the
migration with atlas migrate --env local diff <name>.

In
`@tools/migrate/migrations/20260506075359_add_organization_default_tax_codes.up.sql`:
- Line 17: The current migration creates a UNIQUE index on "namespace" that
doesn't exclude soft-deleted rows; update the Ent schema for
OrganizationDefaultTaxCodes by adding the IndexWhere("deleted_at IS NULL")
annotation in openmeter/ent/schema/organizationdefaulttaxcodes.go (the comment
in that file shows where), then re-run make generate and run atlas migrate --env
local diff to regenerate this migration so the CREATE UNIQUE INDEX becomes a
partial index that ignores rows with deleted_at IS NULL.
🪄 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: eeed7c60-2172-46ea-a45a-c309db8bff7a

📥 Commits

Reviewing files that changed from the base of the PR and between b5be28c and 368554e.

⛔ Files ignored due to path filters (27)
  • openmeter/ent/db/client.go is excluded by !**/ent/db/**
  • openmeter/ent/db/cursor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ent.go is excluded by !**/ent/db/**
  • openmeter/ent/db/entmixinaccessor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/expose.go is excluded by !**/ent/db/**
  • openmeter/ent/db/hook/hook.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/organizationdefaulttaxcodes.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes/organizationdefaulttaxcodes.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_delete.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/paginate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/predicate/predicate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/runtime.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.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/**
  • openmeter/ent/db/tx.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (4)
  • openmeter/ent/schema/organizationdefaulttaxcodes.go
  • openmeter/ent/schema/taxcode.go
  • tools/migrate/migrations/20260506075359_add_organization_default_tax_codes.down.sql
  • tools/migrate/migrations/20260506075359_add_organization_default_tax_codes.up.sql

Base automatically changed from feat/org-tax-config to main May 6, 2026 08:22
@borbelyr-kong
borbelyr-kong force-pushed the feat/org-tax-config-ent branch from 994204c to 098c38a Compare May 6, 2026 08:26
@borbelyr-kong
borbelyr-kong marked this pull request as ready for review May 6, 2026 08:34
@borbelyr-kong
borbelyr-kong requested a review from a team as a code owner May 6, 2026 08:34

@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 (2)
openmeter/ent/schema/organizationdefaulttaxcodes.go (1)

55-61: ⚡ Quick win

Add indexes on the FK columns to avoid sequential scans

The Indexes() method only covers the namespace uniqueness constraint. The two FK fields — invoicing_tax_code_id and credit_grant_tax_code_id — have no indexes. In PostgreSQL, foreign keys do not automatically receive indexes, which can lead to slower operations involving joins or foreign key lookups. This is the same reason TaxMixin.Indexes() explicitly adds index.Fields("tax_code_id") for its FK column — same pattern applies here.

Don't forget to re-run make generate and atlas migrate --env local diff <name> afterwards.

⚡ Suggested fix
 func (OrganizationDefaultTaxCodes) Indexes() []ent.Index {
 	return []ent.Index{
 		index.Fields("namespace").
 			Annotations(entsql.IndexWhere("deleted_at IS NULL")).
 			Unique(),
+		index.Fields("invoicing_tax_code_id"),
+		index.Fields("credit_grant_tax_code_id"),
 	}
 }

As per coding guidelines: "Follow Ent ORM patterns and PostgreSQL type gotchas as documented in the /ent skill."

🤖 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/ent/schema/organizationdefaulttaxcodes.go` around lines 55 - 61,
OrganizationDefaultTaxCodes.Indexes() currently only declares a unique index on
"namespace" and is missing indexes for the two FK columns; add index entries for
"invoicing_tax_code_id" and "credit_grant_tax_code_id" (using
index.Fields("invoicing_tax_code_id") and
index.Fields("credit_grant_tax_code_id") and include the same
entsql.IndexWhere("deleted_at IS NULL") annotation as the namespace index) so
foreign-key lookups avoid sequential scans, then re-run make generate and run
atlas migrate --env local diff <name>.
tools/migrate/migrations/20260506082128_add_organization_default_tax_codes.up.sql (1)

2-13: ⚡ Quick win

Missing indexes on the two FK columns

invoicing_tax_code_id and credit_grant_tax_code_id have no indexes in the migration. When a row in tax_codes is deleted or updated, PostgreSQL checks whether the FK constraint is still satisfied by searching for matching rows in organization_default_tax_codes; without an index, this requires a sequential scan. As this table grows with the number of organizations, that scan gets progressively more expensive.

The fix lives in the Ent schema — see the companion comment on organizationdefaulttaxcodes.go.

🤖 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/migrations/20260506082128_add_organization_default_tax_codes.up.sql`
around lines 2 - 13, Add indexes for the two foreign key columns on
organization_default_tax_codes to avoid sequential scans: create separate
indexes on invoicing_tax_code_id and credit_grant_tax_code_id (or a composite if
appropriate) in the migration that defines the table; ensure the index names are
unique and consistent with existing naming (e.g.,
idx_organization_default_tax_codes_invoicing_tax_code_id and
idx_organization_default_tax_codes_credit_grant_tax_code_id) and update the Ent
schema (organizationdefaulttaxcodes.go) to reflect these indexes so future
migrations and generated code include them.
🤖 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/ent/schema/organizationdefaulttaxcodes.go`:
- Around line 55-61: OrganizationDefaultTaxCodes.Indexes() currently only
declares a unique index on "namespace" and is missing indexes for the two FK
columns; add index entries for "invoicing_tax_code_id" and
"credit_grant_tax_code_id" (using index.Fields("invoicing_tax_code_id") and
index.Fields("credit_grant_tax_code_id") and include the same
entsql.IndexWhere("deleted_at IS NULL") annotation as the namespace index) so
foreign-key lookups avoid sequential scans, then re-run make generate and run
atlas migrate --env local diff <name>.

In
`@tools/migrate/migrations/20260506082128_add_organization_default_tax_codes.up.sql`:
- Around line 2-13: Add indexes for the two foreign key columns on
organization_default_tax_codes to avoid sequential scans: create separate
indexes on invoicing_tax_code_id and credit_grant_tax_code_id (or a composite if
appropriate) in the migration that defines the table; ensure the index names are
unique and consistent with existing naming (e.g.,
idx_organization_default_tax_codes_invoicing_tax_code_id and
idx_organization_default_tax_codes_credit_grant_tax_code_id) and update the Ent
schema (organizationdefaulttaxcodes.go) to reflect these indexes so future
migrations and generated code include them.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cb71abb1-f0a3-41e9-91ea-25b7daa08d4d

📥 Commits

Reviewing files that changed from the base of the PR and between 368554e and 098c38a.

⛔ Files ignored due to path filters (27)
  • openmeter/ent/db/client.go is excluded by !**/ent/db/**
  • openmeter/ent/db/cursor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ent.go is excluded by !**/ent/db/**
  • openmeter/ent/db/entmixinaccessor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/expose.go is excluded by !**/ent/db/**
  • openmeter/ent/db/hook/hook.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/organizationdefaulttaxcodes.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes/organizationdefaulttaxcodes.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_delete.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/organizationdefaulttaxcodes_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/paginate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/predicate/predicate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/runtime.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.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/**
  • openmeter/ent/db/tx.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (4)
  • openmeter/ent/schema/organizationdefaulttaxcodes.go
  • openmeter/ent/schema/taxcode.go
  • tools/migrate/migrations/20260506082128_add_organization_default_tax_codes.down.sql
  • tools/migrate/migrations/20260506082128_add_organization_default_tax_codes.up.sql

This branch was previously deployed

1 inactive deployment
dev — 098c38a8 Deployed May 6, 2026 by borbelyr-kong via Migration Checks #14118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants