245 lines
8.6 KiB
SQL
245 lines
8.6 KiB
SQL
-- CMBNet Datenbankschema
|
|
-- Mandantenfähigkeit: Jede Tabelle hat tenant_id
|
|
|
|
-- Mandanten
|
|
CREATE TABLE IF NOT EXISTS tenants (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
code TEXT UNIQUE NOT NULL,
|
|
name TEXT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Accounts
|
|
CREATE TABLE IF NOT EXISTS accounts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
role TEXT NOT NULL CHECK(role IN ('Gast', 'Beobachter', 'Gruppe', 'Dispatcher', 'Administrator')),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
UNIQUE(tenant_id, name)
|
|
);
|
|
|
|
-- Gruppen
|
|
CREATE TABLE IF NOT EXISTS groups (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
aliasname TEXT,
|
|
state TEXT NOT NULL DEFAULT 'offen' CHECK(state IN ('offen', 'unterwegs', 'online', 'pausierend', 'fertig')),
|
|
last_lat REAL,
|
|
last_lon REAL,
|
|
last_pos_at TIMESTAMP,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id)
|
|
);
|
|
|
|
-- Zuordnung von Gruppen-Accounts zu Gruppen (1:n: eine Gruppe, viele Accounts).
|
|
-- Ein Account der Rolle "Gruppe" darf höchstens einer Gruppe zugeordnet sein.
|
|
CREATE TABLE IF NOT EXISTS group_accounts (
|
|
tenant_id INTEGER NOT NULL,
|
|
group_id INTEGER NOT NULL,
|
|
account_id INTEGER NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (tenant_id, group_id, account_id),
|
|
UNIQUE (tenant_id, account_id),
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (group_id) REFERENCES groups(id),
|
|
FOREIGN KEY (account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Straßen
|
|
CREATE TABLE IF NOT EXISTS streets (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
name TEXT NOT NULL,
|
|
state TEXT NOT NULL DEFAULT 'unbearbeitet' CHECK(state IN ('unbearbeitet', 'zugewiesen', 'erledigt')),
|
|
group_id INTEGER,
|
|
hn_min INTEGER,
|
|
hn_max INTEGER,
|
|
bbox TEXT,
|
|
zusatz TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (group_id) REFERENCES groups(id)
|
|
);
|
|
|
|
-- Hausnummern-Koordinaten (Import aus Straßenverwaltung)
|
|
CREATE TABLE IF NOT EXISTS street_house_numbers (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
street_name TEXT NOT NULL COLLATE NOCASE,
|
|
house_number TEXT NOT NULL COLLATE NOCASE,
|
|
longitude REAL NOT NULL,
|
|
latitude REAL NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
UNIQUE(tenant_id, street_name, house_number)
|
|
);
|
|
|
|
-- Historie Straßen
|
|
CREATE TABLE IF NOT EXISTS street_state_history (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
street_id INTEGER NOT NULL,
|
|
from_state TEXT,
|
|
to_state TEXT NOT NULL,
|
|
group_id INTEGER,
|
|
actor_account_id INTEGER,
|
|
reason TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (street_id) REFERENCES streets(id),
|
|
FOREIGN KEY (actor_account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Historie Gruppen
|
|
CREATE TABLE IF NOT EXISTS group_state_history (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
group_id INTEGER NOT NULL,
|
|
from_state TEXT,
|
|
to_state TEXT NOT NULL,
|
|
last_lat REAL,
|
|
last_lon REAL,
|
|
last_pos_at TIMESTAMP,
|
|
actor_account_id INTEGER,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (group_id) REFERENCES groups(id),
|
|
FOREIGN KEY (actor_account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- WebSocket Tokens
|
|
CREATE TABLE IF NOT EXISTS ws_tokens (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
account_id INTEGER NOT NULL,
|
|
token TEXT UNIQUE NOT NULL,
|
|
session_id TEXT,
|
|
expires_at TIMESTAMP,
|
|
used BOOLEAN DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Zugriffstoken für die externe Gruppenpositions-API.
|
|
-- Die Tokens sind mandantenweit gültig und werden im Admin-Dashboard
|
|
-- verwaltet. Sie werden im Klartext gespeichert, damit sie dort angezeigt
|
|
-- werden können.
|
|
CREATE TABLE IF NOT EXISTS api_tokens (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
token TEXT UNIQUE NOT NULL,
|
|
created_by_account_id INTEGER,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (created_by_account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Prozessübergreifender WebSocket-Zustand
|
|
-- Bekannte Client-IDs, Account-Zuordnung, letzte Sequenznummer und
|
|
-- Verbindungsstatus. Die Tabelle überlebt Reconnects auf anderen Workern.
|
|
CREATE TABLE IF NOT EXISTS ws_clients (
|
|
tenant_id INTEGER NOT NULL,
|
|
client_id TEXT NOT NULL,
|
|
account_id INTEGER NOT NULL,
|
|
last_seq INTEGER NOT NULL DEFAULT 0,
|
|
worker_id TEXT,
|
|
authenticated INTEGER NOT NULL DEFAULT 0,
|
|
connected_at REAL,
|
|
disconnected_at REAL,
|
|
queue_started_at REAL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (tenant_id, client_id),
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Persistente Zustellaufträge pro Client. Jeder Worker liefert nur die
|
|
-- Aufträge seiner eigenen, lokal verbundenen Clients aus.
|
|
CREATE TABLE IF NOT EXISTS ws_client_queue (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
client_id TEXT NOT NULL,
|
|
code INTEGER NOT NULL,
|
|
payload TEXT NOT NULL,
|
|
code99_only INTEGER NOT NULL DEFAULT 0,
|
|
created_at REAL NOT NULL,
|
|
expires_at REAL,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id)
|
|
);
|
|
|
|
-- Gemeinsame Gruppenpräsenz. last_pong_at und online_since werden von allen
|
|
-- Workern gelesen und geschrieben.
|
|
CREATE TABLE IF NOT EXISTS ws_group_presence (
|
|
tenant_id INTEGER NOT NULL,
|
|
group_id INTEGER NOT NULL,
|
|
last_pong_at REAL,
|
|
online_since REAL,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (tenant_id, group_id),
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (group_id) REFERENCES groups(id)
|
|
);
|
|
|
|
-- Workerübergreifende Cache-Revisionen. Jeder Worker vergleicht die Revision
|
|
-- seines In-Memory-Caches mit diesem Wert und lädt bei Abweichung neu.
|
|
CREATE TABLE IF NOT EXISTS cache_state (
|
|
key TEXT PRIMARY KEY,
|
|
revision INTEGER NOT NULL DEFAULT 0,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Persistierte WebSocket Nachrichten für Code >= 1000
|
|
CREATE TABLE IF NOT EXISTS ws_messages_store (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
sender_id INTEGER,
|
|
receiver_id INTEGER,
|
|
code INTEGER NOT NULL,
|
|
ts INTEGER NOT NULL,
|
|
msg TEXT,
|
|
delivered_to TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id)
|
|
);
|
|
|
|
-- Transaktionslog
|
|
CREATE TABLE IF NOT EXISTS transaction_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id INTEGER NOT NULL,
|
|
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
actor_account_id INTEGER,
|
|
action TEXT NOT NULL,
|
|
entity_type TEXT NOT NULL,
|
|
entity_id INTEGER,
|
|
details TEXT,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
|
|
FOREIGN KEY (actor_account_id) REFERENCES accounts(id)
|
|
);
|
|
|
|
-- Indizes für Performance
|
|
CREATE INDEX IF NOT EXISTS idx_accounts_tenant ON accounts(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_groups_tenant ON groups(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_group_accounts_group
|
|
ON group_accounts(tenant_id, group_id);
|
|
CREATE INDEX IF NOT EXISTS idx_streets_tenant ON streets(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_street_house_numbers_lookup
|
|
ON street_house_numbers(tenant_id, street_name, house_number);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_tokens_token ON ws_tokens(token);
|
|
CREATE INDEX IF NOT EXISTS idx_api_tokens_tenant
|
|
ON api_tokens(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_messages_ts ON ws_messages_store(ts);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_clients_account
|
|
ON ws_clients(tenant_id, account_id);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_client_queue_client
|
|
ON ws_client_queue(tenant_id, client_id, id);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_client_queue_expires
|
|
ON ws_client_queue(expires_at);
|