hasql-queue-1.2.0.2: A PostgreSQL backed queue

Safe HaskellNone
LanguageHaskell2010

Hasql.Queue.Migrate

Description

Functions for migrating the database to create the necessary functions for the package.

Users can use these functions or copy and paste the tables to create these tables through a standalone migration system.

Synopsis

Documentation

migrationQueryString Source #

Arguments

:: String

value column type, e.g. int4 or jsonb.

-> String 

The DDL statements to create the schema given a value type.

migrate Source #

Arguments

:: Connection 
-> String

The type of the value column

-> IO () 

This function creates a table and enumeration type that is appriopiate for the queue. The following sql is used.

DO $$
 CREATE OR REPLACE FUNCTION notify_on(channel text) RETURNs VOID AS $$
   BEGIN
     EXECUTE (format(E'NOTIFY %I', channel));
   END;
 $$ LANGUAGE plpgsql;

 CREATE OR REPLACE FUNCTION listen_on(channel text) RETURNS VOID AS $$
   BEGIN
     EXECUTE (format(E'LISTEN %I', channel));
   END;
 $$ LANGUAGE plpgsql;

 CREATE OR REPLACE FUNCTION unlisten_on(channel text) RETURNS VOID AS $$
   BEGIN
     EXECUTE (format(E'UNLISTEN %I', channel));
   END;
 $$ LANGUAGE plpgsql;

   DO $$
 BEGIN
   IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = state_t) THEN
     CREATE TYPE state_t AS ENUM (enqueued, failed);
   END IF;
 END$$;

 CREATE SEQUENCE IF NOT EXISTS modified_index START 1;

 CREATE TABLE IF NOT EXISTS payloads
 ( id BIGSERIAL PRIMARY KEY
 , attempts int NOT NULL DEFAULT 0
 , state state_t NOT NULL DEFAULT enqueued
 , modified_at int8 NOT NULL DEFAULT nextval(modified_index)
 , value ${VALUE_TYPE} NOT NULL
 );

 CREATE INDEX IF NOT EXISTS active_modified_at_idx ON payloads USING btree (modified_at, state)
   WHERE (state = enqueued);

The VALUE_TYPE needs to passed in through the second argument.

teardown :: Connection -> IO () Source #

Drop everything created by migrate