Add basic db structure

This commit is contained in:
2026-01-13 13:36:57 +02:00
parent 63376af86f
commit bcbbef1a82
42 changed files with 1235 additions and 162 deletions

0
migrations/.diesel_lock Normal file
View File

View File

@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();

View File

@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS users;

View File

@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS users (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
password_salt TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
display_name TEXT,
date_of_birth DATE,
phone_number TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
last_login_at TIMESTAMPTZ,
-- u128 bitfield for permissions
permissions NUMERIC(39,0) NOT NULL DEFAULT 0
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS clients;

View File

@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS clients (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
date_of_birth DATE,
phone_number TEXT,
gov_id_number TEXT,
house_number TEXT,
address_line TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
country TEXT,
worker_user_id BIGINT,
CONSTRAINT fk_worker_user_id FOREIGN KEY (worker_user_id) REFERENCES users(id) ON DELETE CASCADE
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS warehouses;

View File

@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS warehouses (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS inventory_catalog;

View File

@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS inventory_catalog (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS inventory;

View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS inventory (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
warehouse_id BIGINT NOT NULL,
catalog_id BIGINT NOT NULL,
count BIGINT NOT NULL DEFAULT 0,
UNIQUE (warehouse_id, catalog_id),
CONSTRAINT fk_warehouse_id FOREIGN KEY (warehouse_id) REFERENCES warehouses(id) ON DELETE CASCADE,
CONSTRAINT fk_catalog_id FOREIGN KEY (catalog_id) REFERENCES inventory_catalog(id) ON DELETE CASCADE
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS tickets;

View File

@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS tickets (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
)

View File

@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS invoices;

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS invoices (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
client_id BIGINT NOT NULL,
amount REAL NOT NULL, -- f32
CONSTRAINT fk_client_id FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE CASCADE
)

View File

@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS services;

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS services (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
client_id BIGINT NOT NULL,
CONSTRAINT fk_client_id FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE CASCADE
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS assigned_warehouse_managers;

View File

@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS assigned_warehouse_managers (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL,
warehouse_id BIGINT NOT NULL,
assigned_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT fk_warehouse_id FOREIGN KEY (warehouse_id) REFERENCES warehouses(id) ON DELETE CASCADE,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)

View File

@@ -0,0 +1 @@
DROP TABLE IF EXISTS warehouse_actions;

View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS warehouse_actions (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL,
warehouse_id BIGINT NOT NULL,
count BIGINT NOT NULL,
reason TEXT NOT NULL,
timestamp TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT fk_warehouse_id FOREIGN KEY (warehouse_id) REFERENCES warehouses(id) ON DELETE CASCADE,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)