-- Add field to mark games as finished and started

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_type t
        INNER JOIN pg_namespace p ON t.typnamespace=p.oid
        WHERE t.typname='status' AND p.nspname='games'
    ) THEN
        CREATE TYPE games.status AS ENUM ('started', 'finished');
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_class c
        INNER JOIN pg_namespace n ON n.oid = c.relnamespace
        INNER JOIN pg_attribute a ON a.attrelid = c.oid
        WHERE c.relname = 'games' AND n.nspname = 'games' AND a.attname = 'status'
    ) THEN
        ALTER TABLE games.games ADD COLUMN status games.status NULL;
    END IF;
END
$$;