CREATE TABLE IF NOT EXISTS virtual_domains (
    id integer primary key generated by default as identity,
    name varchar(50) NOT NULL UNIQUE
);
INSERT INTO virtual_domains (name) VALUES ('tunuifranken.info') ON CONFLICT (name) DO NOTHING;

CREATE TABLE IF NOT EXISTS virtual_users (
    id integer primary key generated by default as identity,
    domain_id integer NOT NULL,
    email varchar(254) NOT NULL UNIQUE,
    password varchar(150) NOT NULL,
    quota bigint NOT NULL DEFAULT 0,
    FOREIGN KEY (domain_id) REFERENCES virtual_domains(id)
    ON DELETE CASCADE
);
INSERT INTO virtual_users (domain_id, email, password, quota) VALUES (
    (SELECT id FROM virtual_domains WHERE name = 'tunuifranken.info'),
    'abuse@tunuifranken.info',
    'foo',
    0
) ON CONFLICT (email) DO UPDATE SET password = EXCLUDED.password, quota = EXCLUDED.quota;

CREATE TABLE IF NOT EXISTS virtual_aliases (
    id integer primary key generated by default as identity,
    domain_id integer NOT NULL,
    source varchar(254) NOT NULL UNIQUE,
    destination varchar(254) NOT NULL,
    FOREIGN KEY (domain_id) REFERENCES virtual_domains(id)
    ON DELETE CASCADE
);
