BEGIN;

CREATE TABLE merchants (
  id uuid primary key default gen_random_uuid(),
  created_at timestamp without time zone NOT NULL default CURRENT_TIMESTAMP,
  first_key TEXT NOT NULL default md5(''||now()::text||random()::text),
  second_key TEXT NOT NULL default md5(''||now()::text||random()::text),
  first_ttl timestamp without time zone NOT NULL default CURRENT_TIMESTAMP + (10 * interval '1 minute'),
  second_ttl timestamp without time zone NOT NULL default CURRENT_TIMESTAMP + (20 * interval '1 minute'),
  updated_at timestamp
);

CREATE TRIGGER merchants_updated_at_timestamp BEFORE UPDATE ON merchants
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();

CREATE UNIQUE INDEX index_on_merchants_pkey ON merchants (id);
CREATE UNIQUE INDEX index_on_merchants_firstkey ON merchants (first_key);
CREATE UNIQUE INDEX index_on_merchants_secondkey ON merchants (second_key);

COMMIT;
