pgscript = '''
CREATE SCHEMA IF NOT EXISTS _amebo_;

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,
        timestamped text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS _amebo_.actions (
        rowid integer unique generated always as identity,
        action text primary key,
        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 integer primary key generated always as identity,
        action text NOT NULL references actions(action),
        deduper text NOT NULL,
        payload text NOT NULL,
        timestamped text NOT NULL,

        UNIQUE(deduper, payload)
    );

    CREATE TABLE IF NOT EXISTS _amebo_.subscriptions (
        rowid integer unique generated always as identity,
        subscription integer primary key generated always as identity,  -- 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,
        cron_interval text NOT NULL,
        handler text NOT NULL,
        description text,
        timestamped text NOT NULL,

        UNIQUE(application, action, handler)
    );

    CREATE TABLE IF NOT EXISTS _amebo_.gists (
        rowid integer primary key generated always as identity,
        event integer references events(event),
        subscription integer references subscriptions(subscription),
        completed integer NOT NULL,
        retries integer NOT NULL,
        timestamped text NOT NULL,

        UNIQUE(event, subscription)
    );

SET search_path TO public;
'''
