pgscript = '''
CREATE SCHEMA IF NOT EXISTS _amebo_;
DROP TABLE IF EXISTS _amebo_.credentials;

SET search_path TO _amebo_;
    CREATE TABLE IF NOT EXISTS _amebo_.credentials(
        rowid integer unique generated always as identity,
        username text primary key,
        password text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS _amebo_.applications (
        rowid integer unique generated always as identity,
        application text primary key,
        address text NOT NULL,
        secret text NOT NULL DEFAULT '',
        apikey text,
        active integer NOT NULL DEFAULT 1,
        timestamped text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS _amebo_.actions (
        rowid integer unique generated always as identity,
        action text primary key,  -- i.e. v1.customers.created
        application text NOT NULL REFERENCES applications(application),
        schemata text NOT NULL,
        timestamped text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS _amebo_.events (
        rowid integer unique generated always as identity,
        event uuid primary key default gen_random_uuid(),
        action text NOT NULL references actions(action),
        deduper text NOT NULL,
        payload text NOT NULL,
        metadata text,  -- can be null and is optional, empty object sent if absent
        timestamped text NOT NULL,

        UNIQUE(deduper, payload)
    );

    CREATE TABLE IF NOT EXISTS _amebo_.subscriptions (
        rowid integer unique generated always as identity,
        subscription uuid primary key default gen_random_uuid(),  -- autogenerated for ease
        application text NOT NULL references applications(application),  -- subscribing app i.e. producer
        action text NOT NULL references actions(action),
        max_retries integer not null default 3,
        handler text NOT NULL,
        description text,
        timestamped text NOT NULL,

        UNIQUE(application, action, handler)
    );

    CREATE TABLE IF NOT EXISTS _amebo_.gists (
        rowid integer unique generated always as identity,
        gist uuid primary key default gen_random_uuid(),
        event uuid references events(event),
        subscription uuid references subscriptions(subscription),
        completed integer NOT NULL default 0,
        acknowledged integer not null default 0,
        sleep_until timestamptz,
        retries integer NOT NULL,
        timestamped text NOT NULL,

        UNIQUE(event, subscription)
    );

    CREATE TABLE IF NOT EXISTS _amebo_.redactions (
        rowid integer unique generated always as identity,
        action text NOT NULL references actions(action),
        field_path text NOT NULL,
        timestamped text NOT NULL,

        UNIQUE(action, field_path)
    );

    -- migrations for existing databases
    ALTER TABLE _amebo_.applications ADD COLUMN IF NOT EXISTS apikey text;
    ALTER TABLE _amebo_.applications ADD COLUMN IF NOT EXISTS active integer NOT NULL DEFAULT 1;

SET search_path TO public;
'''
