How do you manage database schema migrations in a high-scale, production environment?
Using the provided migration metadata, write a query that reports the current production migration health for every service, including services without a production migration.
Output
- One row per service with
service_name, migration_version, latest_status, failed_step_count, total_step_count, and duration_seconds
- Include services without a production run, using NULL migration details and zero step counts
- Return the latest production run per service, ordered by
service_name ascending
Output details
- Break ties between production runs by
started_at descending, then run_id descending.
- Count failed steps where
status equals failed, and count every step in total_step_count.
- Return
duration_seconds as whole-number elapsed seconds, truncating fractional seconds.
- Set
duration_seconds to NULL unless both started_at and completed_at are present.
- Preserve every service record, including records sharing the same
service_name.