Fixes #[issue-number]
This PR completes the migration from storing ChartMogul UUIDs in entity metadata to dedicated database columns. After the database migration (V3) was successfully applied, new entities (customers, plans, subscriptions, invoices) created were not having their ChartMogul UUIDs persisted because the Ent schema and repository layers were not aware of the new columns.
This PR fixes the complete sync pipeline by:
- Adding the
chartmogul_uuidfield to Ent schemas for Customer, Plan, Invoice - Adding the
chartmogul_invoice_uuidfield to Ent schema for Subscription (subscriptions use invoice UUID since they're created via invoice imports in ChartMogul) - Updating all repository Create/Update methods to persist ChartMogul UUIDs
- Updating all domain model
FromEntconverters to read ChartMogul UUIDs from the database - Ensuring backward compatibility with metadata fallback for pre-migration entities
- ❌ Before: New customers/plans/subscriptions/invoices created after V3 migration had ChartMogul UUIDs in memory but not persisted to database
- ✅ After: All new entities have their ChartMogul UUIDs properly stored in dedicated database columns
- Added
chartmogul_uuidfield toent/schema/customer.go - Added
chartmogul_uuidfield toent/schema/plan.go - Added
chartmogul_invoice_uuidfield toent/schema/subscription.go(note: different field name because subscriptions store invoice UUID) - Added
chartmogul_uuidfield toent/schema/invoice.go - Regenerated Ent code with
make generate-ent
- Updated
internal/repository/ent/customer.go:- Added conditional setter in
Create()method - Added conditional setter in
Update()method
- Added conditional setter in
- Updated
internal/repository/ent/plan.go:- Added conditional setter in
Create()method - Added conditional setter in
Update()method
- Added conditional setter in
- Updated
internal/repository/ent/subscription.go:- Added conditional setter in
Update()method forchartmogul_invoice_uuid
- Added conditional setter in
- Updated
internal/repository/ent/invoice.go:- Added conditional setter in
Update()method
- Added conditional setter in
- Updated
internal/domain/customer/model.go:- Enhanced
FromEnt()to convert Ent'sstringto domain's*stringfor ChartMogul UUID
- Enhanced
- Updated
internal/domain/plan/model.go:- Enhanced
FromEnt()to convert ChartMogul UUID
- Enhanced
- Updated
internal/domain/subscription/model.go:- Enhanced
GetSubscriptionFromEnt()to convert ChartMogul invoice UUID
- Enhanced
- Updated
internal/domain/invoice/model.go:- Enhanced
FromEnt()to convert ChartMogul UUID
- Enhanced
- Created
CHARTMOGUL_ENT_SCHEMA_FIX.mdwith detailed technical documentation - Updated existing migration documentation
- Service layer already had correct ChartMogul sync logic
- Backward compatibility with metadata fallback remains intact
- No changes needed in
internal/service/customer.go,plan.go,subscription.go,invoice.go
- Code compiles successfully:
go build ./internal/... - No breaking changes introduced
- Verified migration V3 columns exist:
customers.chartmogul_uuidplans.chartmogul_uuidsubscriptions.chartmogul_invoice_uuidinvoices.chartmogul_uuid
- Verified indexes are in place
After deploying, verify:
# Test new customer creation
# Create a new customer via API
# Then check database:
docker compose exec -T postgres psql -U flexprice -d flexprice -c "
SELECT id, name, chartmogul_uuid, created_at
FROM customers
WHERE chartmogul_uuid IS NOT NULL
ORDER BY created_at DESC
LIMIT 5;
"
# Expected: New customers should have chartmogul_uuid populated- Database migration already applied (V3 migration adds columns and migrates existing data)
- Deploy this PR to fix Ent schema and repository layer
- Restart services with
docker compose down && docker compose up -d --build - Verify new entities have ChartMogul UUIDs in database columns
- (Optional) After validation period, remove metadata fallback code
- ✅ Service layer still reads from metadata if column is empty
- ✅ Existing pre-migration entities continue to work
- ✅ No data loss or breaking changes
- My code follows the project's code style
- Code compiles without errors
- Ent code regenerated after schema changes
- All repository methods updated to handle new fields
- Domain model converters updated
- Backward compatibility maintained
- Documentation created (
CHARTMOGUL_ENT_SCHEMA_FIX.md) - No breaking changes introduced
- Runtime testing performed (to be done post-deployment)
- Logs verified showing "Stored ChartMogul UUID" messages
# New customer created but UUID not in database
customers table:
id | name | chartmogul_uuid | metadata
----------------------------|-------------|-----------------|------------------
cust_01KCENPEWPXMKBF... | Test Co | NULL | {...}
# New customer created with UUID in database column
customers table:
id | name | chartmogul_uuid | metadata
----------------------------|-------------|----------------------|----------
cust_01KCENPEWPXMKBF... | Test Co | cus-abc123-uuid | {...}
- Customers, Plans, Invoices: Store their own ChartMogul entity UUID →
chartmogul_uuid - Subscriptions: Created via invoice imports in ChartMogul, so we store the invoice UUID →
chartmogul_invoice_uuid
- Ent schema: Uses
stringtype for ChartMogul UUID fields - Domain model: Uses
*string(pointer to string) for nullable fields - Conversion:
FromEnt()functions convert empty string tonilpointer
// Conditional setter to only update when UUID is provided
if entity.ChartMogulUUID != nil {
updateBuilder = updateBuilder.SetNillableChartmogulUUID(entity.ChartMogulUUID)
}CHARTMOGUL_ENT_SCHEMA_FIX.md- Technical implementation detailsmigrations/postgres/V3__add_chartmogul_uuid_columns.up.sql- Database migrationmigrations/postgres/V3__add_chartmogul_uuid_columns.down.sql- Rollback migration
-
Pull latest code:
git pull origin main
-
Rebuild and restart:
docker compose down docker compose up -d --build
-
Verify services are healthy:
docker compose ps docker compose logs -f flexprice-api
-
Test ChartMogul sync:
- Create a new customer via API
- Check logs for "Stored ChartMogul UUID in customer" message
- Verify database column is populated
- Code compiles successfully
- Ent code generated without errors
- New customers have
chartmogul_uuidin database (verify post-deployment) - New plans have
chartmogul_uuidin database (verify post-deployment) - New subscriptions have
chartmogul_invoice_uuidin database (verify post-deployment) - New invoices have
chartmogul_uuidin database (verify post-deployment) - Logs show successful ChartMogul UUID storage
- No breaking changes or regressions
- Backward compatibility maintained for pre-migration data
@[team-member] - Please review Ent schema changes and repository updates
- ChartMogul API Documentation: https://dev.chartmogul.com/
- Ent Framework Documentation: https://entgo.io/
- Migration V3 Specification:
migrations/postgres/V3__add_chartmogul_uuid_columns.up.sql