CREATE TABLE IF NOT EXISTS users (
    id int NOT NULL UNIQUE GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    login text NOT NULL UNIQUE,
    credential text NOT NULL UNIQUE,
    name text
);

CREATE TABLE IF NOT EXISTS crates (
    id int NOT NULL UNIQUE GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name text NOT NULL UNIQUE,
    publisher int NOT NULL REFERENCES users (id),
    owners int[] NOT NULL
);

CREATE TYPE version_feature AS (
    feature text,
    enables text[]
);

CREATE TABLE IF NOT EXISTS versions (
    id int NOT NULL UNIQUE GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    vers text NOT NULL,
    cksum char(64) NOT NULL,
    yanked boolean NOT NULL,
    links text,
    crate_id int NOT NULL REFERENCES crates (id),
    features version_feature[] NOT NULL,
    authors text[] NOT NULL,
    description text,
    documentation text,
    homepage text,
    readme text,
    readme_file text,
    keywords text[] NOT NULL,
    categories text[] NOT NULL,
    license text,
    license_file text,
    repository text,
    badges jsonb NOT NULL
);

CREATE TYPE dependency_kind AS ENUM (
    'dev',
    'build',
    'normal'
);

CREATE TABLE IF NOT EXISTS deps (
    id int NOT NULL UNIQUE GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name text NOT NULL,
    version_req text NOT NULL,
    optional boolean NOT NULL,
    default_features boolean NOT NULL,
    target text,
    kind dependency_kind NOT NULL,
    registry text,
    package text,
    features text[] NOT NULL,
    version_id int NOT NULL REFERENCES versions (id)
);
