Repository navigation
feat(api): add organization default tax codes schema and migration - #4294
Conversation
📝 WalkthroughWalkthroughThis PR introduces a new ChangesOrganizationDefaultTaxCodes Entity Introduction
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
368554e to
fedc9a6
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 `@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
⛔ Files ignored due to path filters (27)
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/organizationdefaulttaxcodes.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes/organizationdefaulttaxcodes.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes/where.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_create.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_delete.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_query.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_update.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/**tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (4)
openmeter/ent/schema/organizationdefaulttaxcodes.goopenmeter/ent/schema/taxcode.gotools/migrate/migrations/20260506075359_add_organization_default_tax_codes.down.sqltools/migrate/migrations/20260506075359_add_organization_default_tax_codes.up.sql
994204c to
098c38a
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (2)
openmeter/ent/schema/organizationdefaulttaxcodes.go (1)
55-61: ⚡ Quick winAdd indexes on the FK columns to avoid sequential scans
The
Indexes()method only covers thenamespaceuniqueness constraint. The two FK fields —invoicing_tax_code_idandcredit_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 reasonTaxMixin.Indexes()explicitly addsindex.Fields("tax_code_id")for its FK column — same pattern applies here.Don't forget to re-run
make generateandatlas 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
/entskill."🤖 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 winMissing indexes on the two FK columns
invoicing_tax_code_idandcredit_grant_tax_code_idhave no indexes in the migration. When a row intax_codesis deleted or updated, PostgreSQL checks whether the FK constraint is still satisfied by searching for matching rows inorganization_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
⛔ Files ignored due to path filters (27)
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/organizationdefaulttaxcodes.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes/organizationdefaulttaxcodes.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes/where.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_create.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_delete.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_query.gois excluded by!**/ent/db/**openmeter/ent/db/organizationdefaulttaxcodes_update.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/**tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (4)
openmeter/ent/schema/organizationdefaulttaxcodes.goopenmeter/ent/schema/taxcode.gotools/migrate/migrations/20260506082128_add_organization_default_tax_codes.down.sqltools/migrate/migrations/20260506082128_add_organization_default_tax_codes.up.sql
@coderabbitai
Summary by CodeRabbit