Repository navigation
feat: adopt legacy Ent databases into Atlas migrations - #4705
Conversation
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR changes PostgreSQL migration defaults from runtime Ent migrations to versioned migrations, adds an ChangesMigration modes and adoption entrypoint
Estimated code review effort: 5 (Critical) | ~120 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant Operator
participant MigrateCommand
participant Migrator
participant LegacyEntBaseline
participant AtlasMigrations
Operator->>MigrateCommand: Run adopt-ent
MigrateCommand->>Migrator: AdoptLegacyEnt
Migrator->>Migrator: Inspect state and lock database
Migrator->>LegacyEntBaseline: Apply frozen schema and reconciliation
LegacyEntBaseline-->>Migrator: Baseline ready
Migrator->>AtlasMigrations: Force baseline version
AtlasMigrations-->>Operator: Adoption completed
Operator->>MigrateCommand: Run normal migrate
MigrateCommand->>AtlasMigrations: Apply versioned migrations
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
app/config/postgres.go (1)
81-84: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd punctuation for better readability in the error message.
The error message looks great and is super helpful! Just a tiny nitpick: adding a period or some parentheses before "see" will make it even clearer to read.
📝 Proposed tweak
case "ent": - return errors.New("ent auto-migration is no longer supported; run 'openmeter-jobs migrate adopt-ent' once, then run the normal migration see docs/database-migration.md") + return errors.New("ent auto-migration is no longer supported; run 'openmeter-jobs migrate adopt-ent' once, then run the normal migration (see docs/database-migration.md)") default:🤖 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 `@app/config/postgres.go` around lines 81 - 84, Update the error message returned by the "ent" case in the migration configuration switch to add punctuation before the documentation reference, separating the migration instruction from “see docs/database-migration.md” for clearer readability.tools/migrate/legacyent/reconciliation/001_usage_period_functions.sql (1)
456-456: 🚀 Performance & Scalability | 🔵 TrivialHeads up: the whole historical backfill runs inside one big transaction.
om_func_update_usage_period_durations_batchbatches internally (2000 entitlements at a time, with progressRAISE NOTICEs), but the outer call is wrapped in the single transaction that covers this entire script (perReconcile's per-file transaction design). For installs with a very large number of metered entitlements, that means no durable progress until the whole thing commits, and a long-held transaction/lock for the duration. Retries are safe (idempotent), so this isn't a defect — just worth knowing before running against a big production dataset.🤖 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/legacyent/reconciliation/001_usage_period_functions.sql` at line 456, Document that the call to om_func_update_usage_period_durations_batch runs within the migration’s single transaction, so internal batching does not provide durable progress until the script commits. Preserve the existing idempotent backfill invocation and make the transaction and large-production-dataset considerations clear near this call.tools/migrate/legacy_ent_parity_test.go (1)
19-103: 🗄️ Data Integrity & Integration | 🔵 Trivial | 🏗️ Heavy liftNice schema-parity test — but consider covering the actual data backfill too.
Both
canonicalDBandadoptedDBstart empty, so this suite verifies schema/object/seed-row parity but never exercises001_usage_period_functions.sql's historicalusage_resetsbackfill (om_func_update_usage_period_durations_batch) against pre-existing legacy rows — that's the most complex, highest-risk piece of the whole reconciliation bundle. Worth adding a test that seeds representativeentitlements/usage_resets/billing_invoice_linesrows on a legacy-shaped schema beforeReconcile, then asserts the computedusage_period_interval/anchorvalues and thebilling.subscription.sync.ignoreannotation backfill are correct. Given how critical (and one-shot/irreversible) this adoption path is for real deployments, an e2e run of the actualopenmeter-jobs migrate adopt-entCLI against a seeded legacy-like database would also be a valuable addition, if not already covered elsewhere in the stack.🤖 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/legacy_ent_parity_test.go` around lines 19 - 103, Extend TestLegacyEntAdoptionSchemaParity to seed representative legacy-shaped entitlements, usage_resets, and billing_invoice_lines rows before calling legacyent.Reconcile. Assert that the reconciliation/backfill computes the expected usage_period_interval and anchor values through om_func_update_usage_period_durations_batch, and verifies the billing.subscription.sync.ignore annotation backfill. Keep the existing schema, programmable-object, and seed-state parity assertions intact.Source: Path instructions
🤖 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 `@tools/migrate/legacyent/reconciliation/001_usage_period_functions.sql`:
- Line 2: Move the intervalstyle override from the function body into the
affected function definition’s SET clause, using SET intervalstyle = 'iso_8601'.
Remove the body-level override while preserving the function’s existing interval
handling.
---
Nitpick comments:
In `@app/config/postgres.go`:
- Around line 81-84: Update the error message returned by the "ent" case in the
migration configuration switch to add punctuation before the documentation
reference, separating the migration instruction from “see
docs/database-migration.md” for clearer readability.
In `@tools/migrate/legacy_ent_parity_test.go`:
- Around line 19-103: Extend TestLegacyEntAdoptionSchemaParity to seed
representative legacy-shaped entitlements, usage_resets, and
billing_invoice_lines rows before calling legacyent.Reconcile. Assert that the
reconciliation/backfill computes the expected usage_period_interval and anchor
values through om_func_update_usage_period_durations_batch, and verifies the
billing.subscription.sync.ignore annotation backfill. Keep the existing schema,
programmable-object, and seed-state parity assertions intact.
In `@tools/migrate/legacyent/reconciliation/001_usage_period_functions.sql`:
- Line 456: Document that the call to
om_func_update_usage_period_durations_batch runs within the migration’s single
transaction, so internal batching does not provide durable progress until the
script commits. Preserve the existing idempotent backfill invocation and make
the transaction and large-production-dataset considerations clear near this
call.
🪄 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: fa5477d9-37b2-487c-b420-52bfe5935046
⛔ Files ignored due to path filters (1)
go.modis excluded by!**/*.mod
📒 Files selected for processing (22)
AGENTS.mdapp/common/database.goapp/config/config.goapp/config/config_test.goapp/config/postgres.gocmd/jobs/migrate/migrate.goconfig.example.yamldeploy/charts/openmeter/templates/configmap.yamldocs/database-migration.mdtools/migrate/legacy_ent.gotools/migrate/legacy_ent_parity_test.gotools/migrate/legacy_ent_test.gotools/migrate/legacyent/README.mdtools/migrate/legacyent/legacyent.gotools/migrate/legacyent/reconciliation/001_usage_period_functions.sqltools/migrate/legacyent/reconciliation/002_subscription_trigger.sqltools/migrate/legacyent/reconciliation/003_billing_schema_level.sqltools/migrate/legacyent/reconciliation/004_charges_search_view.sqltools/migrate/legacyent/reconciliation/005_canonical_object_names.sqltools/migrate/legacyent/reconciliation/999_verify.sqltools/migrate/legacyent/schema.gotools/migrate/migrate.go
Summary
openmeter-jobs migrate adopt-entas a one-time bridge for databases previously managed withpostgres.autoMigrate: ent12ab7b082035f2f93972c7f98973c5502107c157, reconcile Atlas-only data and database objects, and record baseline20260709134422openmeter-jobs migratebehavior unchanged except for rejecting Ent modeWhy
Ent automigration only synchronizes table schema. The Atlas migration history also contains data backfills, functions, triggers, views, singleton state, and canonical object names. Supporting both paths allows Ent-managed databases to diverge from databases upgraded through Atlas.
The adoption command deliberately stops at the frozen baseline. Operators then run the normal migration command to reach the version embedded in their target OpenMeter release.
Upgrade flow
openmeter-jobs migrate adopt-ent.openmeter-jobs migrate.Validation
POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./tools/migrate/...nix develop --impure .#ci -c go vet -tags=dynamic ./app/config ./app/common ./cmd/jobs/migrate ./tools/migrate/...go mod tidy -diffmake migrate-check-validateThe migration parity test compares a from-scratch Atlas database with the frozen Ent adoption path, including relational schema, views, functions, triggers, extensions, and migration-owned seed state.
Summary by CodeRabbit
adopt-entmigration job for databases previously managed by Ent.postgres.autoMigratemodes:migration,migration-job, andfalse.postgres.autoMigratefromenttomigration.postgres.autoMigrate: entand now prompts to runadopt-entfirst.Greptile Summary
This PR moves legacy Ent-managed databases onto Atlas migrations. The main changes are:
openmeter-jobs migrate adopt-entcommand adopts legacy Ent databases.Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
Reviews (2): Last reviewed commit: "fix: avoid replaying legacy adoption bac..." | Re-trigger Greptile
Context used (3)