Files
coder/coderd/database/migrations/000160_provisioner_job_status.up.sql
Colin Adler 8e684c8195 feat: run all migrations in a transaction (#10966)
Updates coder/customers#365

This PR updates our migration framework to run all migrations in a single transaction. This is the same behavior we had in v1 and ensures that failed migrations don't bring the whole deployment down. If a migration fails now, it will automatically be rolled back to the previous version, allowing the deployment to continue functioning.
2023-12-01 16:11:10 -06:00

35 lines
1.8 KiB
SQL

CREATE TYPE provisioner_job_status AS ENUM ('pending', 'running', 'succeeded', 'canceling', 'canceled', 'failed', 'unknown');
COMMENT ON TYPE provisioner_job_status IS 'Computed status of a provisioner job. Jobs could be stuck in a hung state, these states do not guarantee any transition to another state.';
ALTER TABLE provisioner_jobs ADD COLUMN
job_status provisioner_job_status NOT NULL GENERATED ALWAYS AS (
CASE
-- Completed means it is not in an "-ing" state
WHEN completed_at IS NOT NULL THEN
CASE
-- The order of these checks are important.
-- Check the error first, then cancelled, then completed.
WHEN error != '' THEN 'failed'::provisioner_job_status
WHEN canceled_at IS NOT NULL THEN 'canceled'::provisioner_job_status
ELSE 'succeeded'::provisioner_job_status
END
-- Not completed means it is in some "-ing" state
ELSE
CASE
-- This should never happen because all errors set
-- should also set a completed_at timestamp.
-- But if there is an error, we should always return
-- a failed state.
WHEN error != '' THEN 'failed'::provisioner_job_status
WHEN canceled_at IS NOT NULL THEN 'canceling'::provisioner_job_status
-- Not done and not started means it is pending
WHEN started_at IS NULL THEN 'pending'::provisioner_job_status
ELSE 'running'::provisioner_job_status
END
END
-- Stored so we do not have to recompute it every time.
) STORED;
COMMENT ON COLUMN provisioner_jobs.job_status IS 'Computed column to track the status of the job.';