
ALTER TABLE api.segments
  ADD COLUMN created_at TIMESTAMP(6) WITHOUT TIME ZONE;

ALTER TABLE api.segments
  ADD COLUMN updated_at TIMESTAMP(6) WITHOUT TIME ZONE;



-- in migration 8, we already created tg__updated_at() so we don't need to create that,
-- but we do need to create a function which will the row's created_at
CREATE FUNCTION public.tg__created_at() RETURNS trigger
  LANGUAGE plpgsql
  SET search_path TO 'pg_catalog', 'public', 'pg_temp'
  AS $$
    BEGIN
      NEW.created_at = now();
      RETURN NEW;
    END;
  $$;

-- create a trigger which runs the tg__updated_at() function when a /segment is updated
CREATE TRIGGER segment_updated_at
  AFTER UPDATE ON api.segments
  FOR EACH ROW
  EXECUTE PROCEDURE public.tg__updated_at();


-- create a trigger which runs the tg__created_at() function when a /segment is created
CREATE TRIGGER segment_created_at
  AFTER INSERT ON api.segments
  FOR EACH ROW
  EXECUTE PROCEDURE public.tg__created_at();

-- create a trigger which runs the tg__created_at() function when a /stream is created
CREATE TRIGGER stream_created_at
  AFTER INSERT ON api.streams
  FOR EACH ROW
  EXECUTE PROCEDURE public.tg__created_at();
