nom-nom-nix-gc/migrations/V1__init.sql

72 lines
2 KiB
SQL

CREATE TABLE Users (
id UUID PRIMARY KEY NOT NULL,
user_name text NOT NULL
);
CREATE TABLE Keys (
id SERIAL PRIMARY KEY NOT NULL,
name text NOT NULL,
key_dump jsonb NOT NULL,
user_id UUID NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES Users(id)
);
CREATE TABLE PendingRegistrations (
id uuid PRIMARY KEY NOT NULL,
user_id UUID NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES Users(id)
);
CREATE TABLE BinaryCaches (
id SERIAL PRIMARY KEY NOT NULL,
name text NOT NULL,
access_key text NOT NULL,
secret_key text NOT NULL,
region text NOT NULL,
endpoint text NOT NULL
);
CREATE TABLE Projects (
id SERIAL PRIMARY KEY NOT NULL,
name text NOT NULL UNIQUE,
binary_cache_id integer NOT NULL,
closure_generation_nb integer NOT NULL,
-- TODO: figure out rules
CONSTRAINT fk_project_binary_cache FOREIGN KEY (binary_cache_id) REFERENCES BinaryCaches(id)
);
CREATE TABLE ProjectTokens (
id SERIAL PRIMARY KEY NOT NULL,
token UUID NOT NULL,
project_id integer NOT NULL,
CONSTRAINT fk_project_project_token FOREIGN KEY (project_id) REFERENCES Projects(id)
);
CREATE TABLE Closures (
id SERIAL PRIMARY KEY NOT NULL,
project_id INTEGER NOT NULL,
objects text[] NOT NULL,
date timestamp NOT NULL,
CONSTRAINT fk_project_closure FOREIGN KEY (project_id) REFERENCES Projects(id)
);
CREATE TABLE Objects (
id SERIAL PRIMARY KEY NOT NULL,
key text NOT NULL
);
CREATE TABLE ObjectClosure (
object_id INTEGER NOT NULL,
closure_id INTEGER NOT NULL,
CONSTRAINT fk_objectclosure_object FOREIGN KEY (object_id) REFERENCES Objects(id),
CONSTRAINT fk_objectclosure_closure FOREIGN KEY (closure_id) REFERENCES Closures(id),
PRIMARY KEY (object_id, closure_id)
);
-- We'll mostly querying the Keys using the associated uid.
CREATE INDEX idx_keys_uid ON Keys USING HASH (user_id);
-- We'll be often sorting Closures through their datetime.
CREATE INDEX idx_date_closures ON Closures USING HASH (date);
-- We'll be querying objects through their names.
CREATE INDEX idx_objects_key ON Objects USING HASH (key);