ALTER TABLE questions
  ADD COLUMN IF NOT EXISTS source_tag TEXT,
  ADD COLUMN IF NOT EXISTS pipeline_key TEXT,
  ADD COLUMN IF NOT EXISTS pipeline_step TEXT;

CREATE INDEX IF NOT EXISTS questions_source_tag_idx ON questions (source_tag)
  WHERE source_tag IS NOT NULL;

CREATE TABLE IF NOT EXISTS user_pipeline_state (
  assigned_user_id  TEXT PRIMARY KEY REFERENCES users (firebase_uid) ON DELETE CASCADE,
  pipeline_key      TEXT NOT NULL,
  step              TEXT NOT NULL,
  context           JSONB NOT NULL DEFAULT '{}',
  updated_at        TIMESTAMPTZ NOT NULL DEFAULT now()
);
