PRAGMA foreign_keys= OFF;
PRAGMA journal_mode = WAL;
BEGIN TRANSACTION;
CREATE TABLE tunnels
(
    tun_id            INTEGER   NOT NULL PRIMARY KEY,
    cksum_crc32       INTEGER   NOT NULL,
    "desc"            TEXT,
    server_v4         TEXT      NOT NULL,
    current_client_v4 TEXT      NOT NULL,
    tunnel_server_v6  TEXT      NOT NULL,
    tunnel_client_v6  TEXT      NOT NULL,
    prefix_64         TEXT      NOT NULL,
    prefix_48         TEXT,
    rdns_1            TEXT,
    rdns_2            TEXT,
    rdns_3            TEXT,
    rdns_4            TEXT,
    rdns_5            TEXT,
    created           TIMESTAMP NOT NULL,
    checked           TIMESTAMP NOT NULL,
    updated           TIMESTAMP
);
CREATE TABLE client_ips
(
    id           INTEGER   NOT NULL PRIMARY KEY AUTOINCREMENT,
    tun_id       INTEGER   NOT NULL,
    client_ip    INTEGER   NOT NULL,
    when_set     TIMESTAMP NOT NULL,
    when_fetched TIMESTAMP,
    CONSTRAINT client_ips_tunnels_FK FOREIGN KEY (tun_id) REFERENCES tunnels (tun_id) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO sqlite_sequence
VALUES ('client_ips', 0);
CREATE TABLE metadata
(
    key     TEXT NOT NULL,
    value   TEXT,
    created TIMESTAMP NOT NULL,
    updated TIMESTAMP
);
COMMIT;
PRAGMA foreign_keys= ON;
