DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'ratelimiter'
        AND column_name = 'request_timestamp'
) THEN
ALTER TABLE public.ratelimiter
    RENAME COLUMN request_timestamp TO expires_at;
END IF;
IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'ratelimiter'
        AND column_name = 'request_count'
) THEN
ALTER TABLE public.ratelimiter
    RENAME COLUMN request_count TO count;
END IF;
END $$;


DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint
    WHERE conname = 'ratelimiter_pkey'
    AND conrelid = 'public.ratelimiter'::regclass
  ) THEN
    ALTER TABLE public.ratelimiter
    ADD CONSTRAINT ratelimiter_pkey PRIMARY KEY (key);
  END IF;
END $$;

