-- In the prev. migration I added a CHECK, but I forgot to add the default


ALTER TABLE IF EXISTS api.records
  ALTER COLUMN created_at SET DEFAULT now();

ALTER TABLE IF EXISTS api.records
  ALTER COLUMN updated_at SET DEFAULT now();


-- create a function which updates the row's updated_at
CREATE FUNCTION public.tg__updated_at() RETURNS trigger
  LANGUAGE plpgsql
  SET search_path TO 'pg_catalog', 'public', 'pg_temp'
  AS $$
    BEGIN
      NEW.updated_at = now();
      RETURN NEW;
    END;
  $$;

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