-- Cargo's cache-tracking schema, as cargo itself creates it in
-- `$CARGO_HOME/.global-cache`.
--
-- This is not grunk's schema to define; it is reproduced here so that tests can
-- build a cargo home without running cargo. Keeping it in one file means the
-- unit tests and the end-to-end tests agree about what they are testing
-- against, and that there is a single place to look when cargo's schema moves.

CREATE TABLE global_data (
    last_auto_gc INTEGER NOT NULL
);

CREATE TABLE registry_index (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT UNIQUE NOT NULL,
    timestamp INTEGER NOT NULL
);

CREATE TABLE registry_crate (
    registry_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    size INTEGER NOT NULL,
    timestamp INTEGER NOT NULL,
    PRIMARY KEY (registry_id, name),
    FOREIGN KEY (registry_id) REFERENCES registry_index (id) ON DELETE CASCADE
);

CREATE TABLE registry_src (
    registry_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    size INTEGER,
    timestamp INTEGER NOT NULL,
    PRIMARY KEY (registry_id, name),
    FOREIGN KEY (registry_id) REFERENCES registry_index (id) ON DELETE CASCADE
);

CREATE TABLE git_db (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT UNIQUE NOT NULL,
    timestamp INTEGER NOT NULL
);

CREATE TABLE git_checkout (
    git_id INTEGER NOT NULL,
    name TEXT UNIQUE NOT NULL,
    size INTEGER,
    timestamp INTEGER NOT NULL,
    PRIMARY KEY (git_id, name),
    FOREIGN KEY (git_id) REFERENCES git_db (id) ON DELETE CASCADE
);
