138 lines
4.5 KiB
SQL
138 lines
4.5 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,
|
|
account_id INTEGER UNIQUE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (tenant_id) REFERENCES tenants(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,
|
|
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)
|
|
);
|
|
|
|
-- 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)
|
|
);
|
|
|
|
-- 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_streets_tenant ON streets(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_tokens_token ON ws_tokens(token);
|
|
CREATE INDEX IF NOT EXISTS idx_ws_messages_ts ON ws_messages_store(ts);
|