Skip to content

Conversation

@turip
Copy link
Member

@turip turip commented Apr 3, 2025

Given we are shifting towards rateCard/Line level discounts remove the internal logic (that was never exposed) for invoice level discount handling.

Table deletion will happen later.

Summary by CodeRabbit

  • Refactor
    • Simplified invoice processing by removing discount-related functionality and validations.
  • Database
    • Updated the schema and adjusted foreign key relationships to align with the new invoice structure.
  • Tests
    • Revised test expectations to reflect the streamlined invoice data without discount fields.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 3, 2025

📝 Walkthrough

Walkthrough

This pull request removes discount-related functionality from the billing system. It eliminates discount checks, related methods, fields, types, and even complete files that managed invoice discounts across adapter, entity, query, and mutation layers. Changes include updating discount types from enums to strings, removing discount edges and predicates, and introducing a new field for line identifiers. Additionally, corresponding tests and SQL migration scripts have been updated to reflect these schema and business logic modifications.

Changes

Files/Area Summary of Changes
Billing Adapter Files
openmeter/billing/adapter/invoice.go
openmeter/billing/adapter/invoicediscounts.go
openmeter/billing/invoice.go
openmeter/billing/invoicediscount.go
Removed discount expansion checks, update logic, discount validations, and entire discount-related file containing type definitions and mappings.
Billing Invoice Entity & DB Files
openmeter/ent/db/billinginvoice.go
openmeter/ent/db/billinginvoice/billinginvoice.go
openmeter/ent/db/billinginvoice/where.go
openmeter/ent/db/billinginvoice_create.go
openmeter/ent/db/billinginvoice_query.go
openmeter/ent/db/billinginvoice_update.go
Removed discount edge fields, query methods, constants, ordering functions, and simplified the invoice entity by eliminating discount relationship handling.
Billing Invoice Discount Entity & DB Files
openmeter/ent/db/billinginvoicediscount.go
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go
openmeter/ent/db/billinginvoicediscount/where.go
openmeter/ent/db/billinginvoicediscount_create.go
openmeter/ent/db/billinginvoicediscount_query.go
openmeter/ent/db/billinginvoicediscount_update.go
Changed the discount type field from enum to string; removed discount edges, query, create, and update functions; and streamlined discount handling logic.
Billing Invoice Line Files
openmeter/ent/db/billinginvoiceline.go
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go
openmeter/ent/db/billinginvoiceline/where.go
openmeter/ent/db/billinginvoiceline_create.go
openmeter/ent/db/billinginvoiceline_query.go
openmeter/ent/db/billinginvoiceline_update.go
Removed invoice discount references; added a new line_ids field with corresponding predicate and mutation methods, replacing previous discount logic.
Client, Schema & Mutation Files
openmeter/ent/db/client.go
openmeter/ent/db/migrate/schema.go
openmeter/ent/db/mutation.go
openmeter/ent/db/setorclear.go
openmeter/ent/schema/billing.go
Removed discount query methods from clients; updated schema definitions and mutation types to remove discount fields and integrate line_ids instead.
Tests
test/billing/invoice_test.go
test/billing/invoicediscount_test.go
Removed discount field expectations in test cases and deleted the entire invoice discount test suite.
Migration Scripts
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.down.sql
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.up.sql
Adjusted foreign key constraints by dropping discount-related keys and adding constraints for the new line_ids field, updating relational mappings.

Possibly Related PRs

  • feat: invoice discounts api v2 #2576: This PR also focuses on restructuring and removing invoice-level discounts, reflecting a similar effort to simplify discount management in the billing system.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

🔧 SQLFluff (3.3.0)
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.down.sql

User Error: No dialect was specified. You must configure a dialect or specify one on the command line using --dialect after the command. Available dialects:
ansi, athena, bigquery, clickhouse, databricks, db2, duckdb, exasol, greenplum, hive, impala, mariadb, materialize, mysql, oracle, postgres, redshift, snowflake, soql, sparksql, sqlite, starrocks, teradata, trino, tsql, vertica

tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.up.sql

User Error: No dialect was specified. You must configure a dialect or specify one on the command line using --dialect after the command. Available dialects:
ansi, athena, bigquery, clickhouse, databricks, db2, duckdb, exasol, greenplum, hive, impala, mariadb, materialize, mysql, oracle, postgres, redshift, snowflake, soql, sparksql, sqlite, starrocks, teradata, trino, tsql, vertica

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@turip turip added release-note/ignore Ignore this change when generating release notes area/billing labels Apr 3, 2025
@turip turip force-pushed the feat/remove-invoice-discounts branch from 3adc094 to 3c7fa86 Compare April 3, 2025 19:54
@turip turip marked this pull request as ready for review April 3, 2025 20:04
@turip turip requested a review from a team as a code owner April 3, 2025 20:04
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (5)
test/billing/invoice_test.go (5)

2150-2170: 🛠️ Refactor suggestion

Remove Discount Assertions from UBP Progressive Invoicing Tests

In this section the test currently expects discount details on detailed line items (e.g. a discount keyed by billing.LineMaximumSpendReferenceID with an amount of 1000). Since the PR intends to remove invoice‐level discount handling, all assertions related to discounts should be removed.
• Remove the discount expectation(s) from the lineExpectations map.
• Update any related expected totals so that discount amounts are no longer factored in (likely defaulting to 0).


3090-3098: 🛠️ Refactor suggestion

Adjust Expected Totals in Non‑Progressive Invoicing Tests for Discount Removal

The expected totals for the invoice are still asserting a non‑zero value for DiscountsTotal (e.g. 1000). With the removal of invoice discounts, these expectations need to be updated (likely setting DiscountsTotal to 0) to reflect the new business logic.


3193-3226: 🛠️ Refactor suggestion

Update Detailed Line Assertions in Helper Function

The helper function requireDetailedLines is verifying discount amounts by grouping child lines using discount reference IDs. Since invoice discounts are now removed, this helper should be updated to either skip discount validations or assert that the discounts map is empty.


3228-3246: 🛠️ Refactor suggestion

Reassess the Expected Totals Structure

The expectedTotals struct still contains a field for DiscountsTotal, and several tests are asserting non‑zero discount totals. In light of the discount removal, please confirm whether these fields should be removed altogether or simply expected to be 0. The test assertions (in multiple scenarios) must be updated accordingly.


1-3705: 💡 Verification agent

❓ Verification inconclusive

Overall Consistency with PR Objectives

This pull request’s objective is to remove all invoice‑level discount handling. There remain several references within the tests (both in detailed line expectations and in overall invoice totals) that verify discount values. Please ensure that all tests are updated to remove discount logic so that they reflect the simplified invoice structure. In particular, any constant or key identifiers such as billing.LineMaximumSpendReferenceID used solely for discount purposes should also be removed (or at least set to a default “no discount” value) in the test expectations.


Action Required: Remove Discount Handling from Test Assertions

Please update the tests in test/billing/invoice_test.go to align with the PR’s objective of eliminating invoice‑level discount handling. In particular:

  • Remove all assertions and expectations that verify discount amounts or rely on discount keys (e.g. references to billing.LineMaximumSpendReferenceID).
  • Adjust detailed line expectations and overall invoice totals so that they no longer consider any discount values or calculations.
  • Ensure that any default behavior for discount-related fields (if still present) is set to reflect “no discount” (e.g. a zero or nil value) in the test assertions.

Once these updates are made, the tests will consistently reflect the simplified invoice structure without discount handling.

🧹 Nitpick comments (6)
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.up.sql (2)

1-2: Enhance Idempotence for Dropping the Foreign Key Constraint on "billing_invoice_discounts"

The migration correctly removes the foreign key constraint from the "billing_invoice_discounts" table as intended. To further improve robustness—particularly if this migration might be run more than once or if the constraint is unexpectedly absent—you might consider using the IF EXISTS clause. For example:

-ALTER TABLE "billing_invoice_discounts" DROP CONSTRAINT "billing_invoice_discounts_billing_invoices_invoice_discounts";
+ALTER TABLE "billing_invoice_discounts" DROP CONSTRAINT IF EXISTS "billing_invoice_discounts_billing_invoices_invoice_discounts";

3-4: Enhance Idempotence for Dropping the Foreign Key Constraint on "billing_invoice_lines"

Similarly, the script drops the foreign key constraint from the "billing_invoice_lines" table. Adding the IF EXISTS clause would ensure this command does not error out if the constraint is already absent:

-ALTER TABLE "billing_invoice_lines" DROP CONSTRAINT "billing_invoice_lines_billing_invoice_discounts_lines";
+ALTER TABLE "billing_invoice_lines" DROP CONSTRAINT IF EXISTS "billing_invoice_lines_billing_invoice_discounts_lines";
test/billing/invoice_test.go (1)

1-10: Organize and Modularize Large Test Suites

This test file is very long and covers many different invoicing scenarios. For improved maintainability and readability it would be beneficial to split these tests into logically grouped files or packages (for example, grouping UBP progressive, UBP non-progressive, and gathering invoice tests separately).

openmeter/ent/db/mutation.go (1)

14301-14301: Slice capacity set to 31.
Please ensure this capacity matches the maximum possible fields.

openmeter/ent/db/billinginvoiceline_update.go (2)

455-459: Consider clarifying or documenting how multiple IDs are stored.
If “line_ids” must hold multiple IDs, you may want to either store them in an array-type field (e.g., []string) or clarify via documentation that they are comma-separated (or similarly delimited). This helps avoid ambiguity and usage errors.


1551-1555: Same consideration for single-entity updates.
If multiple IDs are indeed stored in one field, consider documenting or converting to a more explicit structure. Otherwise, this setter is correct.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3bece3e and 3c7fa86.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (31)
  • openmeter/billing/adapter/invoice.go (0 hunks)
  • openmeter/billing/adapter/invoicediscounts.go (0 hunks)
  • openmeter/billing/invoice.go (1 hunks)
  • openmeter/billing/invoicediscount.go (0 hunks)
  • openmeter/ent/db/billinginvoice.go (1 hunks)
  • openmeter/ent/db/billinginvoice/billinginvoice.go (0 hunks)
  • openmeter/ent/db/billinginvoice/where.go (0 hunks)
  • openmeter/ent/db/billinginvoice_create.go (0 hunks)
  • openmeter/ent/db/billinginvoice_query.go (1 hunks)
  • openmeter/ent/db/billinginvoice_update.go (0 hunks)
  • openmeter/ent/db/billinginvoicediscount.go (3 hunks)
  • openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (0 hunks)
  • openmeter/ent/db/billinginvoicediscount/where.go (2 hunks)
  • openmeter/ent/db/billinginvoicediscount_create.go (5 hunks)
  • openmeter/ent/db/billinginvoicediscount_query.go (3 hunks)
  • openmeter/ent/db/billinginvoicediscount_update.go (4 hunks)
  • openmeter/ent/db/billinginvoiceline.go (5 hunks)
  • openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (3 hunks)
  • openmeter/ent/db/billinginvoiceline/where.go (2 hunks)
  • openmeter/ent/db/billinginvoiceline_create.go (5 hunks)
  • openmeter/ent/db/billinginvoiceline_query.go (2 hunks)
  • openmeter/ent/db/billinginvoiceline_update.go (4 hunks)
  • openmeter/ent/db/client.go (0 hunks)
  • openmeter/ent/db/migrate/schema.go (6 hunks)
  • openmeter/ent/db/mutation.go (22 hunks)
  • openmeter/ent/db/setorclear.go (1 hunks)
  • openmeter/ent/schema/billing.go (3 hunks)
  • test/billing/invoice_test.go (1 hunks)
  • test/billing/invoicediscount_test.go (0 hunks)
  • tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.down.sql (1 hunks)
  • tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.up.sql (1 hunks)
💤 Files with no reviewable changes (10)
  • openmeter/ent/db/billinginvoice/billinginvoice.go
  • openmeter/billing/adapter/invoice.go
  • openmeter/ent/db/billinginvoice_create.go
  • openmeter/billing/invoicediscount.go
  • test/billing/invoicediscount_test.go
  • openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go
  • openmeter/ent/db/billinginvoice/where.go
  • openmeter/ent/db/billinginvoice_update.go
  • openmeter/ent/db/client.go
  • openmeter/billing/adapter/invoicediscounts.go
🧰 Additional context used
🧬 Code Definitions (8)
openmeter/billing/invoice.go (3)
api/api.gen.go (1)
  • InvoiceExpand (3016-3016)
api/client/go/client.gen.go (1)
  • InvoiceExpand (2756-2756)
api/client/javascript/src/client/schemas.ts (1)
  • InvoiceExpand (9023-9023)
openmeter/ent/db/setorclear.go (1)
openmeter/ent/db/billinginvoiceline_update.go (2)
  • BillingInvoiceLineUpdate (29-33)
  • BillingInvoiceLineUpdateOne (1130-1135)
openmeter/ent/db/billinginvoiceline_update.go (3)
openmeter/ent/db/billinginvoiceline/where.go (1)
  • LineIds (198-200)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (1)
  • FieldLineIds (80-80)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (1)
  • FieldLineIds (37-37)
openmeter/ent/db/billinginvoicediscount_update.go (5)
openmeter/ent/db/billinginvoicediscount/where.go (1)
  • InvoiceID (99-101)
openmeter/ent/db/billinginvoiceline/where.go (1)
  • InvoiceID (137-139)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (2)
  • FieldInvoiceID (31-31)
  • FieldType (33-33)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (2)
  • FieldInvoiceID (48-48)
  • FieldType (60-60)
openmeter/ent/db/billinginvoice/billinginvoice.go (1)
  • FieldType (82-82)
openmeter/ent/db/billinginvoicediscount/where.go (7)
openmeter/ent/db/billinginvoicediscount.go (2)
  • BillingInvoiceDiscount (18-45)
  • BillingInvoiceDiscount (48-65)
openmeter/ent/db/predicate/predicate.go (1)
  • BillingInvoiceDiscount (34-34)
openmeter/ent/db/billinginvoice/billinginvoice.go (1)
  • FieldType (82-82)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (1)
  • FieldType (33-33)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (1)
  • FieldType (60-60)
openmeter/ent/db/billinginvoice/where.go (4)
  • TypeEQ (2159-2162)
  • TypeNEQ (2165-2168)
  • TypeIn (2171-2177)
  • TypeNotIn (2180-2186)
openmeter/ent/db/billinginvoiceline/where.go (4)
  • TypeEQ (1118-1121)
  • TypeNEQ (1124-1127)
  • TypeIn (1130-1136)
  • TypeNotIn (1139-1145)
openmeter/ent/db/billinginvoiceline_create.go (3)
openmeter/ent/db/billinginvoiceline/where.go (1)
  • LineIds (198-200)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (1)
  • FieldLineIds (80-80)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (1)
  • FieldLineIds (37-37)
openmeter/ent/db/billinginvoiceline/where.go (5)
openmeter/ent/db/billinginvoiceline.go (2)
  • BillingInvoiceLine (27-101)
  • BillingInvoiceLine (224-247)
openmeter/ent/db/predicate/predicate.go (1)
  • BillingInvoiceLine (40-40)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (1)
  • FieldLineIds (80-80)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (1)
  • FieldLineIds (37-37)
openmeter/ent/db/billinginvoicediscount/where.go (2)
  • LineIdsIsNil (629-631)
  • LineIdsNotNil (634-636)
openmeter/ent/db/mutation.go (3)
openmeter/ent/db/billinginvoiceline/where.go (1)
  • LineIds (198-200)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (1)
  • FieldLineIds (80-80)
openmeter/ent/db/billinginvoicediscount/billinginvoicediscount.go (1)
  • FieldLineIds (37-37)
🔇 Additional comments (74)
openmeter/ent/db/billinginvoiceline/billinginvoiceline.go (3)

79-80: LGTM - New field added for line IDs

The addition of FieldLineIds constant properly defines the string representation for the line_ids field in the database, which aligns with the PR objective of transitioning away from invoice-level discounts toward line item level handling.


214-218: LGTM - Proper validation for new field

The update to the ValidColumn function correctly ensures that the new line_ids field is recognized as a valid column when validating column names.


415-418: LGTM - Ordering function added for new field

The addition of the ByLineIds function allows for proper ordering of query results based on the line_ids field, which is consistent with other ordering functions in the file.

openmeter/ent/db/billinginvoicediscount_query.go (3)

19-30: Confirmed removal of relationship fields from BillingInvoiceDiscountQuery struct.

The changes to the struct definition align with the PR objective of removing invoice-level discount handling. The withInvoice and withLines fields have been removed, which eliminates the ability to load related invoice and line entities.


245-259: Clone method properly updated to reflect removed fields.

The Clone method has been correctly modified to exclude the previously available withInvoice and withLines fields, maintaining consistency with the struct definition changes.


335-361: Related entity loading logic properly removed from sqlAll method.

The sqlAll method has been simplified to remove all logic related to loading invoice and line entities, which is consistent with the removal of the relationship fields from the struct and the PR's objective to eliminate invoice discount functionality.

openmeter/ent/db/billinginvoicediscount/where.go (3)

103-106: Addition of Type convenience function is consistent with codebase style

The new Type function follows the established pattern in the codebase for equality predicates on fields, providing a convenient shorthand for the TypeEQ function.


524-526: Parameter type change from enum to string aligns with PR objectives

The change from billing.InvoiceDiscountType to string in these core type functions (TypeEQ, TypeNEQ, TypeIn, TypeNotIn) is consistent with the PR objective of removing invoice discount enums. The implementation is also simplified by removing the unnecessary temporary variable (vc).

Also applies to: 529-531, 534-536, 539-541


543-586: Addition of string comparison predicates enhances query capabilities

The new functions provide a comprehensive set of string operations for the type field:

  • Comparison operators (GT, GTE, LT, LTE)
  • String pattern matching (Contains, HasPrefix, HasSuffix)
  • Case-insensitive operations (EqualFold, ContainsFold)

These additions align with the transition from enum to string type and follow consistent implementation patterns seen elsewhere in the codebase.

openmeter/ent/db/billinginvoicediscount_create.go (6)

107-111: Type field changed from enum to string

The parameter type in SetType has been changed from a specific enum type to a string, which aligns with the PR objective of removing invoice discount handling. This simplification makes the code more flexible while maintaining functionality until the related tables are deleted in a future PR.


280-283: Direct field setting instead of edge creation

The implementation now directly sets the InvoiceID field from the mutation instead of creating edges for invoice relationships. This is consistent with the simplification effort to remove discount-related logic from the system.


284-286: Type field now stored as string

The discount type is now stored as a string value rather than an enum type, aligning with the parameter type change in the SetType method. This change provides greater flexibility while the system transitions away from invoice-level discounts.


438-442: Upsert method signature updated for consistency

The SetType method signature in the BillingInvoiceDiscountUpsert struct has been updated to use a string parameter, maintaining consistency with the earlier changes to the creation methods.


639-644: UpsertOne method signature updated for consistency

The SetType method signature in the BillingInvoiceDiscountUpsertOne struct has been updated to use a string parameter, ensuring consistent handling of discount types across all related operations.


1014-1019: UpsertBulk method signature updated for consistency

The SetType method signature in the BillingInvoiceDiscountUpsertBulk struct has been updated to use a string parameter, completing the consistent approach to discount type handling across all create and update operations.

openmeter/ent/db/billinginvoiceline/where.go (2)

197-200: Addition of LineIds functions aligns with discount handling transition.

The new LineIds function correctly implements an equality predicate for the "line_ids" field as part of the broader transition from invoice-level discounts to line-item level discounts.


1696-1769:

Details

❓ Verification inconclusive

Complete predicate set for line_ids follows consistent patterns.

The implementation adds a comprehensive set of predicate functions for the "line_ids" field, following the same patterns as other fields in the codebase. These functions provide filtering capabilities essential for queries involving line identifiers.

Note that based on the relevant code snippets, the line_ids field is marked as deprecated with a comment suggesting to use line_discounts instead. Verify if these new predicates are being added as part of a transition strategy.


🏁 Script executed:

#!/bin/bash
# Check for usages of the LineIds predicates and line_ids field
rg -A 3 "LineIds[A-Z]" --type go

Length of output: 9912


Based on the new context, the predicate functions for the line_ids field are implemented in multiple locations (for both BillingInvoiceLine and BillingInvoiceDiscount) and follow the established pattern. However, the review comment noted that the line_ids field is marked as deprecated (with a recommendation to use line_discounts instead) and requested a check on whether these new predicates are added as part of a transition strategy.

From the search results, we see that the predicates are being defined and used throughout the codebase exactly as for non-deprecated fields. There is no additional handling (or commentary) in these predicate functions to indicate they belong to a transition phase. That is, while the functions are consistent and comprehensive, they mirror the already existing pattern without any special treatment for the deprecation note.

Action Items:

  • Confirm that the deprecation of the line_ids field is formally documented elsewhere (or in comments) and that the new predicates are not introducing any breaking changes.
  • If the deprecation strategy requires alternate predicates for line_discounts, consider adding clarifying comments or transitional logic to guide developers.

Rewritten Review Comment:

Attention: Verify deprecation transition for line_ids predicates

The new predicate functions for the line_ids field in both BillingInvoiceLine and BillingInvoiceDiscount follow the expected consistent pattern. However, please verify that these implementations are part of the intended deprecation transition—especially since internal documentation indicates that line_ids is deprecated in favor of line_discounts. It might be worth adding a note or additional handling if these predicates should behave differently during the transition.

  • Confirm if alternate predicates (or transition logic) for line_discounts are required.
  • Ensure that code comments/documents clearly indicate the deprecation strategy for line_ids.
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.down.sql (2)

4-6: Foreign Key Constraint on "billing_invoice_discounts" Table
The statement here properly adds the foreign key constraint linking the "invoice_id" column in "billing_invoice_discounts" to the "id" column in "billing_invoices". The clause "ON UPDATE NO ACTION" combined with "ON DELETE CASCADE" ensures that while updates to invoices don’t propagate, deletions will appropriately remove associated discount records.
Please confirm that the cascade behavior aligns with your intended data integrity policies for invoice deletions.


1-3:

Details

❓ Verification inconclusive

Foreign Key Constraint on "billing_invoice_lines" Table
The SQL statement correctly adds the foreign key constraint linking the "line_ids" column in "billing_invoice_lines" to the "id" column in "billing_invoice_discounts" with "ON UPDATE NO ACTION" and "ON DELETE NO ACTION".
However, the column name "line_ids" suggests that it might hold multiple values. Please verify that this column is intended to reference a single discount record. If it is meant to handle multiple identifiers, consider revisiting the schema design (e.g., using a join table) to properly normalize this relationship.


Action Required: Confirm the Intended Usage of "line_ids" in the Constraint

The SQL statement in
tools/migrate/migrations/20250403195308_deprecate-invoice-discounts.down.sql
correctly re-creates the foreign key constraint linking the "line_ids" column in "billing_invoice_lines" to the "id" column in "billing_invoice_discounts" with ON UPDATE NO ACTION and ON DELETE NO ACTION. However, the column name "line_ids" implies that it might be designed to hold multiple values. Please verify that this column is indeed meant to reference a single discount record. If it is intended to associate with multiple identifiers, consider refactoring the schema (for example, implementing a join table) to properly normalize the relationship.

openmeter/ent/db/setorclear.go (1)

898-910: Implementation follows established patterns

The new SetOrClearLineIds methods are well-implemented and follow the same pattern as other SetOrClear methods in the file. This addition supports the migration from invoice-level to line-item level discounts as stated in the PR objectives.

openmeter/ent/db/billinginvoiceline.go (4)

91-94: Appropriate deprecation notice included

The addition of the LineIds field with a clear deprecation notice is a good approach. This field appears to be a transitional element to help with the migration from invoice-level to line-item level discounts, as mentioned in the PR objectives.


125-125: Consistent with removal of invoice discount functionality

The reduction of loadedTypes array size from 10 to 9 aligns with the removal of invoice discount functionality mentioned in the PR objectives. This suggests that an edge (likely InvoiceDiscounts) has been removed.


456-462: Field assignment logic correctly implemented

The assignment logic for the new LineIds field is correctly implemented, consistent with the pattern used for other string pointer fields.


665-669: String method updated to include new field

The String method has been properly updated to include the new LineIds field, maintaining consistency in logging and debugging output.

openmeter/ent/db/billinginvoicediscount.go (4)

39-39: Type field switched from enum to string.

The Type field has been changed from billing.InvoiceDiscountType to string, which aligns with the removal of invoice discount functionality. This change simplifies the type handling but removes the type safety previously provided by the enum.


43-43: Added LineIds field to support line item level discounts.

A new LineIds field of type []string has been added, which seems to be part of the transition from invoice-level discounts to line item level discounts as mentioned in the PR objectives.


137-137: Updated Type field assignment in assignValues method.

The assignment of the Type field has been updated to directly assign a string value instead of converting from billing.InvoiceDiscountType, consistent with the type change on line 39.


218-218: Updated string representation for the Type field.

The string representation in the String() method has been updated to handle the Type field as a string rather than an enum.

openmeter/ent/db/migrate/schema.go (3)

481-482: Reordered and changed type of fields in BillingInvoiceDiscountsColumns.

The invoice_id field has been moved up and the type field has been changed from an enum to a string type. This aligns with the changes in the BillingInvoiceDiscount struct and supports the removal of invoice-level discount functionality.


510-510: Updated column reference in index definition.

The column reference in the index definition has been updated to reflect the repositioning of the invoice_id field in the BillingInvoiceDiscountsColumns structure.


1995-2000: Updated ForeignKeys references for BillingInvoiceLinesTable.

Foreign key references in BillingInvoiceLinesTable have been updated, which is part of the restructuring related to the removal of invoice discount functionality. The changes maintain the integrity of relationships between tables in the database schema.

openmeter/ent/db/billinginvoicediscount_update.go (8)

120-121: Use of string for the "type" field looks fine.
The change from an enum to a string is consistent with a simpler, more flexible approach to handling this field.


126-128: Nillable type setter is well-implemented.
This method guards against overwriting the existing field unless a non-nil value is provided.


239-241: Confirm necessity of "invoice_id".
Given the PR objective to remove invoice discount logic, verify if retaining “invoice_id” remains valid or should be stripped out.


243-243: String-based 'type' field assignment is consistent.
Assigning the field as type string aligns with the broader removal of the enum constraint.


366-367: Switched to string for 'type' method signature.
This matches the new data model direction and avoids dependencies on the removed enum.


372-374: Nillable type method is compatible with the recent refactor.
Maintains existing field values unless a non-nil pointer is set.


515-517: Reassess invoice_id usage if invoice discount logic is being removed.
Confirm continued usage or remove if no longer needed.


519-519: String-based 'type' field assignment in the update spec looks good.
No issues found in storing a plain string for the discount type.

openmeter/ent/db/mutation.go (20)

10821-10821: Initialize slice capacity.
This seems consistent with the code removing discount edges. No issues found here.


10895-10895: Consistent slice allocation.
This capacity looks fine for the current needs.


10927-10927: Edge slice capacity.
Initializing with capacity 8 is acceptable. No concerns.


11050-11051: Transition to string fields for invoice references.
Switching from typed references (e.g., an enum) to strings may reduce type safety. If this aligns with the new discount-removal approach, it’s all good.


11458-11463: InvoiceID setter logic.
This setter matches common ent patterns and appears correct.


11489-11498: Reset for invoice_id and setter for _type.
The changes for resetting the invoice_id field and setting the type field are in line with ent mutation style.


11509-11509: Retrieving old discount type.
This is an expected ent helper for the old field lookup.


11685-11685: Conditional check for invoice_id field.
No issues found.


11824-11824: Type assertion to string.
This aligns with the removal of enum-based discount logic.


11959-11959: Empty edges handling.
Returning an empty slice for added/removed/cleared edges is appropriate now that discount edges are dropped.

Also applies to: 11971-11971, 11983-11983


13884-13931: SetLineIds, OldLineIds, ClearLineIds implementation.
This is standard ent mutation logic. Looks correct.


14392-14394: Adding LineIds field conditionally.
Appending the line IDs field when set is standard practice.


14463-14463: Retrieving the LineIds field.
Logic appears correct.


14535-14535: Retrieving old LineIds.
This matches ent’s pattern for the old field value.


14755-14761: Setting line_ids via type assertion.
Returning an error if the value isn't a string is appropriate.


14825-14827: Checking if line_ids was cleared.
Straightforward logic, no issues.


14875-14877: Clearing line_ids.
Implementation follows ent’s typical clear pattern.


14976-14985: Resetting line_ids and adjusting edge capacity.
Implementation looks consistent with the prior removal of discount edges.


15066-15066: Edges slice with capacity 9.
No concerns with the slice initialization.


15098-15098: Creating edges slice.
No issues found here.

openmeter/billing/invoice.go (2)

338-340: Discount field removed from Invoice struct

The Discounts field has been removed from the Invoice struct as part of the broader effort to remove invoice-level discount handling. This aligns with the PR objective to transition toward implementing discounts at the rate card or line item level.


342-354: Invoice validation simplified by removing discount validation

The Validate method has been appropriately simplified by removing discount-related validation logic. The method now only validates the InvoiceBase and Lines fields, which maintains the core validation requirements while removing the now-obsolete discount validation.

openmeter/ent/schema/billing.go (3)

371-380: Added deprecated field for backward compatibility

A new line_ids field has been added to the BillingInvoiceLine schema with a clear deprecation notice. This field acts as a transitional element while migrating away from invoice-level discounts to line-level discounts.


552-553: TODO comment added for future cleanup

A TODO comment has been added to indicate that the BillingInvoiceDiscount schema should be removed in a future update. This is good practice to mark technical debt that should be addressed after this change has been established.


570-570: Simplified discount type field

The discount type field has been changed from an enum to a simple string type, which simplifies the schema while maintaining backward compatibility during this transitional phase of removing invoice discounts.

openmeter/ent/db/billinginvoice_query.go (1)

626-635: Updated loadedTypes array size

The loadedTypes array size has been reduced from 9 to 8 elements, reflecting the removal of the invoice discounts related edge from the query structure. This change is consistent with the removal of invoice discount functionality across the codebase.

openmeter/ent/db/billinginvoiceline_query.go (1)

664-664: Array size reduction reflects removal of invoice discount functionality.

The change in the loadedTypes array size from what was previously likely 10 to the current 9 elements is consistent with the PR objective of removing invoice-level discount handling. This adjustment aligns with the removal of discount-related query functionality throughout the codebase.

openmeter/ent/db/billinginvoice.go (1)

161-161: Array size reduction reflects removal of InvoiceDiscounts field.

The change in the loadedTypes array size from 9 to 8 elements is consistent with the removal of the InvoiceDiscounts field from the BillingInvoiceEdges struct. This change aligns with the PR objective of removing invoice-level discount functionality, as the system transitions to implementing discounts at the rate card or line item level.

openmeter/ent/db/billinginvoiceline_update.go (6)

461-467: No issues with the nillable setter.
This pattern follows usual ent conventions and looks good.


469-473: Clearer usage is correct.
Clearing the line_ids field follows ent’s pattern and is straightforward.


818-823: Field assignment logic looks consistent.
Ensuring the new line_ids field is updated and cleared appropriately during sqlSave is correct and aligned with ent’s conventions.


1557-1563: Nillable logic remains standard.
No concerns; matches ent’s typical approach.


1565-1569: Clear method is appropriate.
Clearing this field aligns well with ent’s typical patterns.


1944-1949: SQL mutation handling is properly integrated.
The addition of line_ids field checks here is correct and appears to be in full alignment with ent’s generated approach.

openmeter/ent/db/billinginvoiceline_create.go (1)

313-325: New "line_ids" field handling methods added successfully

The implementation adds comprehensive support for a new "line_ids" field across all relevant struct types (Create, Upsert, UpsertOne, UpsertBulk). These changes align with the PR's objective of removing invoice-level discount handling in favor of rate card or line item level discounts.

The new field is properly integrated in:

  • Setter methods
  • Nullable setters
  • Create spec field mapping
  • Update operations
  • Bulk operations

The implementation follows the established patterns in the codebase for field management.

Also applies to: 717-720, 1302-1318, 1821-1840, 2510-2529

@turip turip enabled auto-merge (squash) April 3, 2025 20:56
@turip turip merged commit df8d963 into main Apr 4, 2025
32 checks passed
@turip turip deleted the feat/remove-invoice-discounts branch April 4, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing 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