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

    CREATE TABLE IF NOT EXISTS credentials(
        username varchar(128) primary key,
        password varchar not null
    );

    CREATE TABLE IF NOT EXISTS producers (
        name varchar(128) primary key,
        location varchar(512) not null,
        passphrase text not null,
        timestamped varchar(32) not null
    );

    CREATE TABLE IF NOT EXISTS actions (
        action varchar(128) primary key not null,
        producer varchar(128) NOT NULL REFERENCES producers(name),
        schemata text not null,
        timestamped varchar(32) not null
    );

    CREATE TABLE IF NOT EXISTS events (
        event integer primary key not null,
        action varchar(128) not null references actions(action),
        deduper varchar(128) not null,
        payload text not null,
        timestamped varchar(32) not null,

        UNIQUE(deduper, payload)
    );

    CREATE TABLE IF NOT EXISTS subscribers (
        subscription integer primary key not null,  -- autogenerated for ease
        subscriber varchar(128) not null references producers(name),  -- subscribing app
        action varchar(128) not null references actions(action),
        handler varchar(256) not null,
        description text,
        timestamped varchar(32) not null,

        UNIQUE(subscriber, action, handler)
    );

    CREATE TABLE IF NOT EXISTS gists (
        event integer references events(event),
        subscription integer references subscribers(subscription),
        completed integer not null,
        timestamped varchar(32) not null,

        UNIQUE(event, subscriber)
    );
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
'''
