-- +goose Up
START TRANSACTION;

CREATE SCHEMA IF NOT EXISTS runner;

CREATE TABLE IF NOT EXISTS runner.scrapejob (
	id SERIAL PRIMARY KEY,
	startedTs TIMESTAMP NOT NULL DEFAULT NOW(),
	completedTs TIMESTAMP,
	targetSiteName VARCHAR(512) NOT NULL,
	auctionsFound INT NOT NULL DEFAULT 0,
	errors TEXT NOT NULL DEFAULT ''
);


-- +goose StatementBegin
DO
$do$
BEGIN
   IF NOT EXISTS (
      SELECT FROM pg_catalog.pg_roles
      WHERE rolname = 'runner-service') THEN

		CREATE USER "runner-service" WITH PASSWORD 'runner-service';
   END IF;
END
$do$;
-- +goose StatementEnd

GRANT CONNECT ON DATABASE bh to "runner-service";
GRANT USAGE ON SCHEMA runner TO "runner-service";
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA runner TO "runner-service";
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA runner TO "runner-service";

COMMIT;


-- +goose Down
