Skip to content

feat(database): add new advanced filter group by column to features - #3470

Merged
tothandras merged 1 commit into
mainfrom
feat/api-features-v2
Oct 7, 2025
Merged

tothandras merged 1 commit into
mainfrom
feat/api-features-v2

Conversation

@tothandras

@tothandras tothandras commented Oct 6, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Added advanced meter group-by filters for Features.
    • API now supports setting/clearing advanced group-by filters; reads prefer the advanced field and fall back to existing filters when absent.
  • Chores
    • Database migration adds an advanced_meter_group_by_filters column and auto-populates it from existing meter_group_by_filters where applicable.
  • Tests
    • Added migration tests covering null, empty, single, and multi-filter scenarios to validate backfill and compatibility.

@tothandras
tothandras requested a review from a team as a code owner October 6, 2025 14:31
@tothandras tothandras added the release-note/misc Miscellaneous changes label Oct 6, 2025
@coderabbitai

coderabbitai Bot commented Oct 6, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Renames the ent/db feature package to the alias dbfeature across the codebase, adds a new nullable JSON column advanced_meter_group_by_filters to Feature (schema, ent models, builders, predicates, queries), updates productcatalog adapter logic to set/use this field, and includes SQL migrations with a test to backfill data.

Changes

Cohort / File(s) Summary
Alias refactor: feature → dbfeature (ENT runtime, queries, edges, mutations)
openmeter/ent/db/client.go, openmeter/ent/db/ent.go, openmeter/ent/db/runtime.go, openmeter/ent/db/mutation.go, openmeter/ent/db/predicate/predicate.go, openmeter/ent/db/addonratecard.go, openmeter/ent/db/addonratecard/*, openmeter/ent/db/entitlement.go, openmeter/ent/db/entitlement/*, openmeter/ent/db/planratecard.go, openmeter/ent/db/planratecard/*
Replaces imports and symbol usage from feature.* to dbfeature.* for tables, fields, predicates, labels, and edge specs; comments updated to reference dbfeature. No behavioral changes beyond identifier source.
Feature entity: new field wiring
openmeter/ent/db/feature.go, openmeter/ent/db/feature/feature.go, openmeter/ent/db/feature/where.go, openmeter/ent/db/feature_create.go, openmeter/ent/db/feature_update.go, openmeter/ent/db/setorclear.go, openmeter/ent/db/feature_delete.go, openmeter/ent/db/feature_query.go, openmeter/ent/db/migrate/schema.go
Adds AdvancedMeterGroupByFilters (JSON) to Feature: struct, columns, predicates (IsNil/NotNil), create/update/upsert setters, optional SetOrClear helper, scan/assign, stringer, schema registration; adjusts query/update/delete builders to dbfeature.*. FeatureQuery.Order now uses dbfeature.OrderOption.
Schema definition
openmeter/ent/schema/feature.go
Introduces Optional JSON field advanced_meter_group_by_filters with type feature.MeterGroupByFilters; imports external type.
Migrations
tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql, ...down.sql
Adds nullable jsonb column advanced_meter_group_by_filters to features; backfills from meter_group_by_filters by wrapping values as objects with $eq; down migration drops the column.
Migration test
tools/migrate/feature_advanced_meter_group_by_filters_test.go
Tests migration across NULL, empty, single, and multi-filter cases; verifies column presence and correct backfill behavior without altering original field.
Product catalog adapter
openmeter/productcatalog/adapter/feature.go
On create, sets both AdvancedMeterGroupByFilters (raw struct) and MeterGroupByFilters (map form). Mapping prefers AdvancedMeterGroupByFilters when present; otherwise converts legacy map.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Pre-merge checks and finishing touches

✅ 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 primary change—adding a new advanced filter group‐by column to the features table in the database—and uses concise, conventional commit style with an appropriate scope. It directly reflects the main functionality introduced by the pull request without extraneous detail.
Docstring Coverage ✅ Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/api-features-v2

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.

@tothandras
tothandras marked this pull request as draft October 6, 2025 14:38

@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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 735c956 and a9a519d.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (32)
  • openmeter/ent/db/addonratecard.go (2 hunks)
  • openmeter/ent/db/addonratecard/addonratecard.go (1 hunks)
  • openmeter/ent/db/addonratecard_create.go (2 hunks)
  • openmeter/ent/db/addonratecard_query.go (3 hunks)
  • openmeter/ent/db/addonratecard_update.go (5 hunks)
  • openmeter/ent/db/client.go (10 hunks)
  • openmeter/ent/db/ent.go (2 hunks)
  • openmeter/ent/db/entitlement.go (2 hunks)
  • openmeter/ent/db/entitlement/entitlement.go (1 hunks)
  • openmeter/ent/db/entitlement_create.go (2 hunks)
  • openmeter/ent/db/entitlement_query.go (3 hunks)
  • openmeter/ent/db/feature.go (5 hunks)
  • openmeter/ent/db/feature/feature.go (3 hunks)
  • openmeter/ent/db/feature/where.go (2 hunks)
  • openmeter/ent/db/feature_create.go (13 hunks)
  • openmeter/ent/db/feature_delete.go (3 hunks)
  • openmeter/ent/db/feature_query.go (21 hunks)
  • openmeter/ent/db/feature_update.go (27 hunks)
  • openmeter/ent/db/migrate/schema.go (1 hunks)
  • openmeter/ent/db/mutation.go (22 hunks)
  • openmeter/ent/db/planratecard.go (2 hunks)
  • openmeter/ent/db/planratecard/planratecard.go (1 hunks)
  • openmeter/ent/db/planratecard_create.go (2 hunks)
  • openmeter/ent/db/planratecard_query.go (3 hunks)
  • openmeter/ent/db/planratecard_update.go (5 hunks)
  • openmeter/ent/db/predicate/predicate.go (1 hunks)
  • openmeter/ent/db/runtime.go (2 hunks)
  • openmeter/ent/db/setorclear.go (2 hunks)
  • openmeter/ent/schema/feature.go (2 hunks)
  • openmeter/productcatalog/adapter/feature.go (2 hunks)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.down.sql (1 hunks)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (24)
openmeter/ent/db/planratecard_update.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/productcatalog/adapter/feature.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMeterGroupByFiltersToMapString (102-116)
openmeter/ent/db/entitlement.go (2)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
openmeter/ent/db/feature/feature.go (1)
  • Label (14-14)
openmeter/ent/db/addonratecard.go (2)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
openmeter/ent/db/feature/feature.go (1)
  • Label (14-14)
openmeter/ent/db/setorclear.go (2)
openmeter/ent/db/feature_update.go (2)
  • FeatureUpdate (23-27)
  • FeatureUpdateOne (489-494)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature_create.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (26)
  • DefaultCreatedAt (98-98)
  • DefaultUpdatedAt (100-100)
  • DefaultID (110-110)
  • NamespaceValidator (104-104)
  • NameValidator (106-106)
  • KeyValidator (108-108)
  • Table (46-46)
  • FieldID (16-16)
  • FieldCreatedAt (18-18)
  • FieldUpdatedAt (20-20)
  • FieldDeletedAt (22-22)
  • FieldMetadata (24-24)
  • FieldNamespace (26-26)
  • FieldName (28-28)
  • FieldKey (30-30)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • EntitlementTable (48-48)
  • Columns (71-84)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
openmeter/ent/db/planratecard_create.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/ent/db/feature_delete.go (2)
openmeter/ent/db/feature/feature.go (3)
  • Table (46-46)
  • FieldID (16-16)
  • Label (14-14)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
openmeter/ent/db/planratecard_query.go (2)
openmeter/ent/db/feature/feature.go (2)
  • Table (46-46)
  • FieldID (16-16)
openmeter/ent/db/feature/where.go (1)
  • IDIn (29-31)
openmeter/ent/db/addonratecard_query.go (2)
openmeter/ent/db/feature/feature.go (2)
  • Table (46-46)
  • FieldID (16-16)
openmeter/ent/db/feature/where.go (1)
  • IDIn (29-31)
openmeter/ent/db/ent.go (1)
openmeter/ent/db/feature/feature.go (2)
  • Table (46-46)
  • ValidColumn (87-94)
openmeter/ent/db/feature_query.go (2)
openmeter/ent/db/feature/feature.go (12)
  • OrderOption (114-114)
  • Table (46-46)
  • FieldID (16-16)
  • EntitlementTable (48-48)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
  • Label (14-14)
  • ValidColumn (87-94)
  • Columns (71-84)
openmeter/ent/db/ent.go (2)
  • NotFoundError (285-287)
  • NotSingularError (312-314)
openmeter/ent/db/addonratecard_create.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/ent/db/addonratecard_update.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/ent/db/mutation.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (7)
  • FieldMetadata (24-24)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • FieldName (28-28)
  • FieldKey (30-30)
openmeter/ent/db/client.go (3)
openmeter/ent/db/feature/feature.go (8)
  • Table (46-46)
  • FieldID (16-16)
  • EntitlementTable (48-48)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
openmeter/ent/db/feature.go (2)
  • Feature (18-48)
  • Feature (91-106)
openmeter/ent/db/feature/where.go (1)
  • ID (14-16)
openmeter/ent/db/feature/where.go (3)
openmeter/ent/db/feature.go (2)
  • Feature (18-48)
  • Feature (91-106)
openmeter/ent/schema/feature.go (5)
  • Feature (14-16)
  • Feature (18-24)
  • Feature (26-36)
  • Feature (38-47)
  • Feature (49-61)
openmeter/ent/db/feature/feature.go (1)
  • FieldAdvancedMeterGroupByFilters (36-36)
openmeter/ent/db/feature_update.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (15)
  • Table (46-46)
  • Columns (71-84)
  • FieldID (16-16)
  • FieldUpdatedAt (20-20)
  • FieldDeletedAt (22-22)
  • FieldMetadata (24-24)
  • FieldName (28-28)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • EntitlementTable (48-48)
  • RatecardTable (55-55)
  • AddonRatecardTable (62-62)
  • Label (14-14)
openmeter/ent/db/entitlement_create.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/ent/db/planratecard.go (2)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
openmeter/ent/db/feature/feature.go (1)
  • Label (14-14)
openmeter/ent/schema/feature.go (1)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/entitlement_query.go (2)
openmeter/ent/db/feature/feature.go (2)
  • Table (46-46)
  • FieldID (16-16)
openmeter/ent/db/feature/where.go (1)
  • IDIn (29-31)
openmeter/ent/db/feature.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (5)
  • FieldMetadata (24-24)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldID (16-16)
  • FieldArchivedAt (38-38)
openmeter/ent/db/runtime.go (3)
openmeter/ent/db/feature.go (2)
  • Feature (18-48)
  • Feature (91-106)
openmeter/ent/schema/feature.go (5)
  • Feature (14-16)
  • Feature (18-24)
  • Feature (26-36)
  • Feature (38-47)
  • Feature (49-61)
openmeter/ent/db/feature/feature.go (4)
  • DefaultUpdatedAt (100-100)
  • UpdateDefaultUpdatedAt (102-102)
  • NameValidator (106-106)
  • KeyValidator (108-108)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: Migration Checks
  • GitHub Check: Test
  • GitHub Check: Code Generators
  • GitHub Check: Build
  • GitHub Check: Lint
  • GitHub Check: Release note label
  • GitHub Check: Analyze (go)
🔇 Additional comments (10)
openmeter/ent/db/entitlement/entitlement.go (1)

115-115: LGTM: Documentation updated to reflect package rename.

The comment correctly reflects the rename from feature to dbfeature package, consistent with the broader refactoring in this PR. The database table name "features" on line 116 appropriately remains unchanged.

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

16-16: LGTM! Package alias refactoring is consistent.

The import alias change from feature to dbfeature and corresponding updates to edge target ID specifications are systematically applied for the AddonRateCardUpdate builder.

Also applies to: 464-464, 477-477


964-964: LGTM! UpdateOne builder changes match the Update builder.

Edge target specifications in AddonRateCardUpdateOne correctly use the dbfeature.FieldID alias, maintaining consistency with the AddonRateCardUpdate builder changes.

Also applies to: 977-977

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

18-18: LGTM! Comprehensive and consistent package alias refactoring.

All references to the feature package have been systematically updated to use the dbfeature alias throughout the query builder, including:

  • Order types and method signatures
  • Edge specifications for entitlement, ratecard, and addon_ratecard
  • Error construction (NotFoundError, NotSingularError labels)
  • Field validation and selection
  • Query specification and SQL construction

The refactoring is complete and maintains consistency across all query operations.

Also applies to: 27-27, 65-65, 82-84, 104-106, 126-128, 144-144, 166-166, 193-195, 220-222, 261-261, 325-325, 388-388, 408-408, 430-430, 513-513, 543-543, 576-576, 609-609, 618-620, 650-653

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

17-19: LGTM! Dual import strategy enables new field support.

The use of both dbfeature (for ent constants) and feature (for domain types) imports allows proper integration of the new advanced_meter_group_by_filters field. The SetAdvancedMeterGroupByFilters method follows the established pattern for field setters.

Also applies to: 116-120


231-239: LGTM! Field integration in createSpec is correct.

The create specification properly integrates the new advanced_meter_group_by_filters field (lines 348-351) with JSON field type, and all table/field/column references correctly use the dbfeature alias for defaults, validators, and edge specifications.

Also applies to: 256-272, 305-305, 313-351, 360-393


458-550: LGTM! Complete upsert integration for the new field.

The advanced_meter_group_by_filters field is fully integrated across all upsert paths:

  • FeatureUpsert: Set, Update, and Clear operations (lines 534-550)
  • FeatureUpsertOne: Set, Update, and Clear operations (lines 721-740)
  • FeatureUpsertBulk: Set, Update, and Clear operations (lines 1081-1100)

All operations follow the established pattern and correctly use dbfeature for field constants.

Also applies to: 577-598, 721-740, 935-957, 1081-1100

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

16-19: LGTM! New field methods added to FeatureUpdate.

The dual import strategy and new SetAdvancedMeterGroupByFilters and ClearAdvancedMeterGroupByFilters methods follow the established pattern for field updates in the FeatureUpdate builder.

Also applies to: 99-109


275-283: LGTM! SQL update logic correctly handles the new field.

The sqlSave implementation for FeatureUpdate properly integrates the new field with both Set (line 330) and Clear (line 333) operations. All field and edge specifications consistently use dbfeature references, and error construction uses dbfeature.Label.

Also applies to: 294-339, 345-474, 478-478


560-570: LGTM! FeatureUpdateOne mirrors FeatureUpdate changes.

The FeatureUpdateOne builder has complete support for the new field (lines 560-570, 820-824) and all references consistently use dbfeature for field constants, validators, defaults, and edge specifications. The implementation maintains parity with the FeatureUpdate builder.

Also applies to: 749-757, 768-830, 836-965, 972-972

Comment on lines +34673 to +34675
func (m *FeatureMutation) SetAdvancedMeterGroupByFilters(fgbf feature.MeterGroupByFilters) {
m.advanced_meter_group_by_filters = &fgbf
}

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 | 🔴 Critical

Remove stale clear flag when re-setting the field

SetAdvancedMeterGroupByFilters never removes dbfeature.FieldAdvancedMeterGroupByFilters from clearedFields. After calling ClearAdvancedMeterGroupByFilters, any later SetAdvanced... leaves the field flagged as cleared and the end builder will still write NULL, dropping the freshly assigned value. Please mirror the pattern used by the other setters:

 func (m *FeatureMutation) SetAdvancedMeterGroupByFilters(fgbf feature.MeterGroupByFilters) {
 	m.advanced_meter_group_by_filters = &fgbf
+	delete(m.clearedFields, dbfeature.FieldAdvancedMeterGroupByFilters)
 }
🤖 Prompt for AI Agents
In openmeter/ent/db/mutation.go around lines 34673-34675,
SetAdvancedMeterGroupByFilters currently sets m.advanced_meter_group_by_filters
but does not clear the stale clear flag; update the setter to also delete
m.clearedFields[dbfeature.FieldAdvancedMeterGroupByFilters] (or call the
equivalent remove on the clearedFields collection) before assigning the pointer
so it mirrors other setters and prevents the field from being written as NULL
after a prior ClearAdvancedMeterGroupByFilters call.

@tothandras
tothandras marked this pull request as ready for review October 6, 2025 15:57

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

🧹 Nitpick comments (2)
tools/migrate/feature_meter_group_by_test.go (2)

24-218: Consider adding down migration and idempotency verification.

The test currently only verifies the up migration. For comprehensive migration testing, consider adding:

  1. Down migration verification: Test that rolling back the migration properly removes the advanced_meter_group_by_filters column and preserves the original meter_group_by_filters data.

  2. Idempotency check: Verify that running the up migration twice produces the same result without errors or data corruption.

Example structure for down migration test:

{
    // TEST SCENARIO 3: Verify down migration
    // After rolling back, advanced_meter_group_by_filters column should be removed
    // and meter_group_by_filters should remain unchanged
    version:   20251006141236,
    direction: directionDown,
    action: func(t *testing.T, db *sql.DB) {
        // Verify column no longer exists
        var columnExists bool
        err := db.QueryRow(`
            SELECT EXISTS (
                SELECT 1 
                FROM information_schema.columns 
                WHERE table_name = 'features' 
                AND column_name = 'advanced_meter_group_by_filters'
            )
        `).Scan(&columnExists)
        require.NoError(t, err)
        require.False(t, columnExists, "advanced_meter_group_by_filters column should not exist after rollback")
        
        // Verify original data is preserved
        var originalFilters sql.NullString
        err = db.QueryRow(`
            SELECT meter_group_by_filters
            FROM features
            WHERE id = $1
        `, singleFeatureId.String()).Scan(&originalFilters)
        require.NoError(t, err)
        require.JSONEq(t, `{"region":"us-east-1"}`, originalFilters.String)
    },
},

203-215: Column existence check is redundant but harmless.

The column existence check at lines 203-215 is technically redundant since the previous SELECT queries (lines 154-201) would have already failed if the column didn't exist. However, having an explicit check improves test clarity and provides a more specific error message if the migration fails to create the column.

If you prefer to keep the test concise, you could remove this check, as the earlier queries already validate the column's presence indirectly.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a9a519d and bcbf8a1.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • tools/migrate/feature_meter_group_by_test.go (1 hunks)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql
🔇 Additional comments (1)
tools/migrate/feature_meter_group_by_test.go (1)

11-219: Well-structured migration test with correct verification logic.

The test properly validates the migration's data transformation from simple key-value filters to advanced filters with $eq operators. The use of parameterized queries, NULL handling with sql.NullString, and JSON comparison with require.JSONEq are all appropriate.

@tothandras
tothandras force-pushed the feat/api-features-v2 branch from bcbf8a1 to 3d724b6 Compare October 7, 2025 07:35
@tothandras
tothandras force-pushed the feat/api-features-v2 branch from 3d724b6 to 9a24ac9 Compare October 7, 2025 07:37

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

♻️ Duplicate comments (2)
tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql (1)

17-19: Acknowledge the performance concern and test requirement.

The WHERE clause correctly handles edge cases (SQL NULL, JSON null, and type checking). However, as noted in the previous review, this UPDATE will perform a full table scan and could timeout on large tables.

As suggested in the previous review:

  1. Add a test to verify the migration behavior
  2. Consider making this a manual deployment step if the table is large
  3. Monitor execution time in staging before running in production
openmeter/ent/db/mutation.go (1)

34673-34675: Remove stale clear flag when setting the field.

This is the same issue flagged in the previous review: SetAdvancedMeterGroupByFilters does not remove the field from clearedFields. If ClearAdvancedMeterGroupByFilters is called first, then SetAdvancedMeterGroupByFilters is called later, the field will remain marked as cleared and the builder will write NULL, discarding the newly assigned value.

Apply this diff to fix:

 func (m *FeatureMutation) SetAdvancedMeterGroupByFilters(fgbf feature.MeterGroupByFilters) {
 	m.advanced_meter_group_by_filters = &fgbf
+	delete(m.clearedFields, dbfeature.FieldAdvancedMeterGroupByFilters)
 }
🧹 Nitpick comments (4)
tools/migrate/feature_advanced_meter_group_by_filters_test.go (2)

172-176: Prefer JSONEq for JSON comparisons

Use JSONEq instead of string equality for "{}", avoiding formatting issues from driver/DB.

-					require.Equal(t, "{}", originalFilters.String, "Original filters should remain empty object")
+					require.JSONEq(t, "{}", originalFilters.String, "Original filters should remain empty object")
-					require.True(t, advancedFilters.Valid, "Advanced filters should not be NULL")
-					require.Equal(t, "{}", advancedFilters.String, "Advanced filters should be empty object")
+					require.True(t, advancedFilters.Valid, "Advanced filters should not be NULL")
+					require.JSONEq(t, "{}", advancedFilters.String, "Advanced filters should be empty object")

206-214: Scope the column existence check to current schema

Add table_schema filter to avoid false positives if multiple schemas exist.

-						SELECT EXISTS (
-							SELECT 1 
-							FROM information_schema.columns 
-							WHERE table_name = 'features' 
-							AND column_name = 'advanced_meter_group_by_filters'
-						)
+						SELECT EXISTS (
+							SELECT 1
+							FROM information_schema.columns
+							WHERE table_schema = current_schema()
+							  AND table_name = 'features'
+							  AND column_name = 'advanced_meter_group_by_filters'
+						)
openmeter/productcatalog/adapter/feature.go (2)

48-52: Set advanced always when provided; set legacy only if convertible

len(...) > 0 skips explicit empty {}. Also, only set the legacy map when conversion succeeds.

-	if len(feat.MeterGroupByFilters) > 0 {
-		query = query.
-			SetAdvancedMeterGroupByFilters(feat.MeterGroupByFilters).
-			SetMeterGroupByFilters(feature.ConvertMeterGroupByFiltersToMapString(feat.MeterGroupByFilters))
-	}
+	if feat.MeterGroupByFilters != nil {
+		query = query.SetAdvancedMeterGroupByFilters(feat.MeterGroupByFilters)
+		if m := feature.ConvertMeterGroupByFiltersToMapString(feat.MeterGroupByFilters); m != nil {
+			query = query.SetMeterGroupByFilters(m)
+		}
+	}

258-263: Detect presence by nil, not length

Prefer advanced filters whenever present, even if empty {}; fallback to legacy only if advanced is nil.

-	// Use advanced meter group by filters if available
-	if len(entity.AdvancedMeterGroupByFilters) > 0 {
+	// Use advanced meter group by filters if available (even if empty)
+	if entity.AdvancedMeterGroupByFilters != nil {
 		f.MeterGroupByFilters = entity.AdvancedMeterGroupByFilters
-	} else if len(entity.MeterGroupByFilters) > 0 {
+	} else if entity.MeterGroupByFilters != nil {
 		f.MeterGroupByFilters = feature.ConvertMapStringToMeterGroupByFilters(entity.MeterGroupByFilters)
 	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bcbf8a1 and 9a24ac9.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (33)
  • openmeter/ent/db/addonratecard.go (2 hunks)
  • openmeter/ent/db/addonratecard/addonratecard.go (1 hunks)
  • openmeter/ent/db/addonratecard_create.go (2 hunks)
  • openmeter/ent/db/addonratecard_query.go (3 hunks)
  • openmeter/ent/db/addonratecard_update.go (5 hunks)
  • openmeter/ent/db/client.go (10 hunks)
  • openmeter/ent/db/ent.go (2 hunks)
  • openmeter/ent/db/entitlement.go (2 hunks)
  • openmeter/ent/db/entitlement/entitlement.go (1 hunks)
  • openmeter/ent/db/entitlement_create.go (2 hunks)
  • openmeter/ent/db/entitlement_query.go (3 hunks)
  • openmeter/ent/db/feature.go (5 hunks)
  • openmeter/ent/db/feature/feature.go (3 hunks)
  • openmeter/ent/db/feature/where.go (2 hunks)
  • openmeter/ent/db/feature_create.go (13 hunks)
  • openmeter/ent/db/feature_delete.go (3 hunks)
  • openmeter/ent/db/feature_query.go (21 hunks)
  • openmeter/ent/db/feature_update.go (27 hunks)
  • openmeter/ent/db/migrate/schema.go (1 hunks)
  • openmeter/ent/db/mutation.go (22 hunks)
  • openmeter/ent/db/planratecard.go (2 hunks)
  • openmeter/ent/db/planratecard/planratecard.go (1 hunks)
  • openmeter/ent/db/planratecard_create.go (2 hunks)
  • openmeter/ent/db/planratecard_query.go (3 hunks)
  • openmeter/ent/db/planratecard_update.go (5 hunks)
  • openmeter/ent/db/predicate/predicate.go (1 hunks)
  • openmeter/ent/db/runtime.go (2 hunks)
  • openmeter/ent/db/setorclear.go (2 hunks)
  • openmeter/ent/schema/feature.go (2 hunks)
  • openmeter/productcatalog/adapter/feature.go (2 hunks)
  • tools/migrate/feature_advanced_meter_group_by_filters_test.go (1 hunks)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.down.sql (1 hunks)
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (17)
  • openmeter/ent/db/entitlement_query.go
  • tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.down.sql
  • openmeter/ent/db/planratecard.go
  • openmeter/ent/db/entitlement.go
  • openmeter/ent/db/feature/where.go
  • openmeter/ent/db/migrate/schema.go
  • openmeter/ent/db/entitlement_create.go
  • openmeter/ent/db/addonratecard_create.go
  • openmeter/ent/db/addonratecard_query.go
  • openmeter/ent/db/addonratecard_update.go
  • openmeter/ent/db/planratecard_create.go
  • openmeter/ent/db/addonratecard.go
  • openmeter/ent/db/runtime.go
  • openmeter/ent/db/predicate/predicate.go
  • openmeter/ent/db/entitlement/entitlement.go
  • openmeter/ent/db/ent.go
  • openmeter/ent/db/addonratecard/addonratecard.go
🧰 Additional context used
🧬 Code graph analysis (12)
openmeter/ent/db/planratecard_update.go (1)
openmeter/ent/db/feature/feature.go (1)
  • FieldID (16-16)
openmeter/ent/db/feature.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (12)
  • FieldMetadata (24-24)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldID (16-16)
  • FieldNamespace (26-26)
  • FieldName (28-28)
  • FieldKey (30-30)
  • FieldMeterSlug (32-32)
  • FieldCreatedAt (18-18)
  • FieldUpdatedAt (20-20)
  • FieldDeletedAt (22-22)
  • FieldArchivedAt (38-38)
openmeter/ent/db/feature_delete.go (2)
openmeter/ent/db/feature/feature.go (3)
  • Table (46-46)
  • FieldID (16-16)
  • Label (14-14)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
openmeter/ent/db/planratecard_query.go (2)
openmeter/ent/db/feature/feature.go (2)
  • Table (46-46)
  • FieldID (16-16)
openmeter/ent/db/feature/where.go (1)
  • IDIn (29-31)
openmeter/ent/db/feature_query.go (2)
openmeter/ent/db/feature/feature.go (12)
  • OrderOption (114-114)
  • Table (46-46)
  • FieldID (16-16)
  • EntitlementTable (48-48)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
  • Label (14-14)
  • ValidColumn (87-94)
  • Columns (71-84)
openmeter/ent/db/ent.go (2)
  • NotFoundError (285-287)
  • NotSingularError (312-314)
openmeter/productcatalog/adapter/feature.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMeterGroupByFiltersToMapString (102-116)
openmeter/ent/schema/feature.go (1)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature_create.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (20)
  • DefaultCreatedAt (98-98)
  • DefaultUpdatedAt (100-100)
  • DefaultID (110-110)
  • NamespaceValidator (104-104)
  • NameValidator (106-106)
  • KeyValidator (108-108)
  • Table (46-46)
  • FieldID (16-16)
  • FieldCreatedAt (18-18)
  • FieldUpdatedAt (20-20)
  • FieldDeletedAt (22-22)
  • FieldMetadata (24-24)
  • FieldNamespace (26-26)
  • FieldName (28-28)
  • FieldKey (30-30)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • Columns (71-84)
openmeter/ent/db/mutation.go (3)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/schema/feature.go (5)
  • Feature (14-16)
  • Feature (18-24)
  • Feature (26-36)
  • Feature (38-47)
  • Feature (49-61)
openmeter/ent/db/feature/feature.go (7)
  • FieldMetadata (24-24)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • FieldName (28-28)
  • FieldKey (30-30)
openmeter/ent/db/client.go (5)
openmeter/ent/db/addonratecard/addonratecard.go (2)
  • Table (59-59)
  • FieldID (19-19)
openmeter/ent/db/entitlement/entitlement.go (2)
  • Table (83-83)
  • FieldID (19-19)
openmeter/ent/db/feature/feature.go (8)
  • Table (46-46)
  • FieldID (16-16)
  • EntitlementTable (48-48)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
openmeter/ent/db/planratecard/planratecard.go (2)
  • Table (59-59)
  • FieldID (19-19)
openmeter/ent/db/feature/where.go (1)
  • ID (14-16)
openmeter/ent/db/setorclear.go (2)
openmeter/ent/db/feature_update.go (2)
  • FeatureUpdate (23-27)
  • FeatureUpdateOne (489-494)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature_update.go (3)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/feature/feature.go (21)
  • UpdateDefaultUpdatedAt (102-102)
  • NameValidator (106-106)
  • Table (46-46)
  • Columns (71-84)
  • FieldID (16-16)
  • FieldUpdatedAt (20-20)
  • FieldDeletedAt (22-22)
  • FieldMetadata (24-24)
  • FieldName (28-28)
  • FieldMeterSlug (32-32)
  • FieldMeterGroupByFilters (34-34)
  • FieldAdvancedMeterGroupByFilters (36-36)
  • FieldArchivedAt (38-38)
  • EntitlementTable (48-48)
  • EntitlementColumn (53-53)
  • RatecardTable (55-55)
  • RatecardColumn (60-60)
  • AddonRatecardTable (62-62)
  • AddonRatecardColumn (67-67)
  • Label (14-14)
  • ValidColumn (87-94)
openmeter/ent/db/ent.go (1)
  • NotFoundError (285-287)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: E2E
  • GitHub Check: Quickstart
  • GitHub Check: Lint
  • GitHub Check: Migration Checks
  • GitHub Check: Test
  • GitHub Check: Build
  • GitHub Check: Code Generators
  • GitHub Check: Analyze (go)
🔇 Additional comments (19)
openmeter/ent/schema/feature.go (1)

33-33: Clarify the relationship between meter_group_by_filters and advanced_meter_group_by_filters.

Both fields coexist in the schema. Consider documenting:

  • Whether one supersedes the other
  • Migration/deprecation strategy for the old field
  • If validation is needed for the new field (several other fields have validators)
tools/migrate/migrations/20251006141236_feature-advanced-meter-group-by-filters.up.sql (2)

1-2: LGTM!

Adding the nullable column is a safe, non-blocking operation.


6-16: Verify $eq-only transformation

Migration wraps all meter_group_by_filters entries in {"$eq": value}. Manually confirm that no existing filters rely on other operators or non-text values that would be lost.

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

2173-2186: Advanced SetOrClear helpers look correct

Nil-clears and non-nil-sets mirror existing pattern. Type usage aligns with feature.MeterGroupByFilters.

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

16-20: Alias migration to dbfeature looks consistent

Imports/identifiers updated coherently across the file.

Please re-run ent code generation to ensure no local manual edits drift from templates and all new methods are covered by generated code.


99-109: Advanced filters field wired end-to-end — LGTM

Set/Clear methods and SQL spec handling for advanced_meter_group_by_filters are correct in both Update and UpdateOne.

Also applies to: 329-334, 560-570, 820-825

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

65-69: dbfeature alias refactor and query wiring look good

Order, predicates, columns, and labels are consistently updated; edge loaders reference correct FK columns.

Also applies to: 82-86, 104-108, 126-129, 144-146, 166-167, 193-196, 220-223, 261-265, 325-335, 381-384, 404-411, 431-434, 609-646, 649-655

openmeter/ent/db/feature_create.go (5)

1-20: LGTM: Import aliasing is clear and prevents naming conflicts.

The introduction of the dbfeature alias for openmeter/ent/db/feature and the unaliased import of openmeter/productcatalog/feature provides clear disambiguation between the database schema package and the domain model package. This pattern allows the code to reference database constants via dbfeature.* while using the domain type feature.MeterGroupByFilters in the public API.


116-120: LGTM: New field setter follows ent patterns correctly.

The SetAdvancedMeterGroupByFilters method correctly uses the domain type feature.MeterGroupByFilters from the productcatalog package and delegates to the mutation object, following the same pattern as other field setters in this builder.


348-351: LGTM: Field creation logic is consistent with other JSON fields.

The handling of AdvancedMeterGroupByFilters in createSpec correctly sets the field value when present in the mutation, using the appropriate field constant and type (TypeJSON). This matches the pattern used for the existing MeterGroupByFilters field.


534-550: LGTM: Upsert methods are complete and consistent.

The upsert methods for AdvancedMeterGroupByFilters across FeatureUpsert, FeatureUpsertOne, and FeatureUpsertBulk provide complete functionality:

  • Set: assigns a new value
  • Update: uses the value from the create operation
  • Clear: sets the field to NULL

This pattern is consistent with other optional JSON fields in the builder.

Also applies to: 721-740, 1081-1100


231-403: LGTM: Comprehensive refactoring to dbfeature alias.

All references to database schema constants (DefaultCreatedAt, DefaultUpdatedAt, DefaultID, validators, field names, table names, and column names) have been correctly updated to use the dbfeature alias. This change is consistent throughout the file and maintains the same semantics as before while improving clarity.

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

44-44: LGTM! Import structure is correct.

The dual import approach is appropriate:

  • dbfeature alias for schema constants
  • feature package for the domain type MeterGroupByFilters

This follows the established pattern for other schema packages in the file.

Also applies to: 66-66


34175-34175: LGTM! Field correctly added to mutation struct.

The new field uses the proper domain type feature.MeterGroupByFilters as a pointer, allowing nil state for this optional field.


34677-34719: LGTM! Getter, clear, and reset methods are correctly implemented.

The methods follow the established patterns:

  • Getter returns value with exists flag
  • OldValue retrieval validates operation type and fetches from database
  • Clear correctly marks field as cleared
  • Reset properly removes the clear flag

34402-34402: LGTM! Constant references correctly updated.

All field constant references have been properly updated from feature.Field* to dbfeature.Field* to reflect the new import alias.

Also applies to: 34407-34407, 34414-34414, 34451-34451, 34456-34456, 34463-34463, 34608-34608, 34613-34613, 34620-34620, 34657-34657, 34662-34662, 34669-34669, 34755-34755, 34760-34760, 34767-34767


34966-35001: LGTM! Fields() method correctly updated.

The field count has been properly incremented to 11, and the new advanced_meter_group_by_filters field is correctly included in the list with the appropriate constant reference.


35026-35027: LGTM! All switch cases correctly updated.

The new field has been properly integrated into all mutation methods:

  • Field() returns the value
  • OldField() retrieves the old value
  • SetField() validates type and calls setter
  • ClearedFields() checks cleared status
  • ClearField() clears the field
  • ResetField() resets the field

All use the correct constant references and type assertions.

Also applies to: 35057-35058, 35133-35139, 35189-35190, 35221-35223, 35262-35264


35276-35276: LGTM! Edge constant references correctly updated.

All edge constant references have been properly updated from feature.Edge* to dbfeature.Edge* across all edge-related methods, maintaining consistency with the new import structure.

Also applies to: 35279-35279, 35282-35282, 35291-35291, 35297-35297, 35303-35303, 35317-35317, 35320-35320, 35323-35323, 35332-35332, 35338-35338, 35344-35344, 35358-35358, 35361-35361, 35364-35364, 35373-35373, 35375-35375, 35377-35377, 35395-35395, 35398-35398, 35401-35401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants