Ecto + PostgreSQL: Should Ecto expose a helper for automatic schema drift detection after restore?
0 reputation · 21 Sept 2024, 08:33 UTC
0 reputation · 21 Sept 2024, 08:33 UTC
When a PostgreSQL database is restored from a pg_dump archive, the application’s Ecto schema modules may no longer match the on‑disk structure. The migrations table can become out of sync if the backup predates recent migrations, leading to runtime errors or data inconsistencies.
Currently, Ecto offers no built‑in mechanism to compare the restored database schema with the code‑defined schema. Developers must write custom test scripts that query the database via Repo.all/1 or inspect the migrations table manually.
Given this gap, an unresolved decision in the community is whether Ecto should provide an API that automatically performs this comparison after a restore, catching drift before the application starts.
mix ecto.migrate?33275 reputation · 21 Sept 2024, 11:22 UTC
Yes – Ecto could provide an optional helper that compares a restored PostgreSQL schema with the code‑defined schema and reports any drift before the application starts. Making it opt‑in avoids unnecessary overhead for users who do not need the check.
migrations table manually.migrations table out of sync, causing runtime errors or data inconsistencies.Migration module for introspection, while Ecto 2.x relies only on the migrations table.A drift‑detection helper would:
Repo and collect all schema modules (MyApp.Accounts.User, etc.).information_schema for tables, columns, data types, nullability, and default values.mix ecto.detect_drift, that performs the steps above and exits with a non‑zero status if any drift is found.mix ecto.migrate with a flag like --check-drift that runs the detect task before executing migrations.migrations table when the Migration module is unavailable; for 3.x use the module to list applied migrations and improve accuracy.mix ecto.detect_drift against a restored staging database to catch drift early.Whether the helper should halt application startup on drift (hard failure) or merely log a warning. If a hard failure is required, the helper would need to be invoked early in the supervision tree; otherwise a simple Mix task suffices.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.