initdbscript = '''
BEGIN;
    DROP TABLE IF EXISTS credentials;

    CREATE TABLE IF NOT EXISTS credentials(
        username text primary key,
        password text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS applications (
        application text primary key,
        address text NOT NULL,
        secret text NOT NULL,
        timestamped text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS actions (
        action text primary key,
        application text NOT NULL REFERENCES applications(application),
        schemata text NOT NULL,
        timestamped text NOT NULL
    );

    CREATE TABLE IF NOT EXISTS events (
        event integer primary key NOT NULL,
        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 subscriptions (
        subscription integer primary key NOT NULL,  -- 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 gists (
        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)
    );
COMMIT;
'''

insertscript = '''
    INSERT INTO credentials VALUES(?, ?)
'''

tidydatabase = '''
    CREATE TRIGGER IF NOT EXISTS
        prune_gists
    AFTER INSERT ON
        gists
    WHEN
        count(*) > 1_000_000
    BEGIN
        DELETE FROM gists WHERE timestamped < '14 days ago'
    END
'''
