Files
Sternsinger/CMBNet/cmbnet/db/database.py
T

298 lines
11 KiB
Python

import sqlite3
import os
from pathlib import Path
from flask import current_app, g
import click
def get_db():
"""Stellt eine Datenbankverbindung für den aktuellen Request bereit"""
if 'db' not in g:
db_path = current_app.config['DATABASE_PATH']
# Stelle sicher, dass das instance Verzeichnis existiert
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
g.db = sqlite3.connect(db_path, timeout=30)
g.db.row_factory = sqlite3.Row
# Mehrere WSGI-Worker schreiben gleichzeitig in dieselbe SQLite-Datei.
# busy_timeout zuerst setzen, damit auch das Umschalten auf WAL bei
# parallelen Verbindungen warten kann statt sofort zu scheitern.
# WAL erlaubt parallele Leser und reduziert die Blockierzeit bei
# Schreibern.
g.db.execute('PRAGMA busy_timeout = 30000')
g.db.execute('PRAGMA journal_mode = WAL')
g.db.execute('PRAGMA synchronous = NORMAL')
return g.db
def close_db(e=None):
"""Schließt die Datenbankverbindung am Ende des Requests"""
db = g.pop('db', None)
if db is not None:
db.close()
def ensure_ws_schema(db):
"""Legt die Tabellen für den prozessübergreifenden Broker-Zustand an.
``ws_clients`` wird bei bestehenden Installationen um die Spalten für
Client-Typ und FCM-Aufwachsignale ergänzt. SQLite kennt kein
``ADD COLUMN IF NOT EXISTS``; deshalb wird vorher ``PRAGMA table_info``
ausgewertet.
"""
db.executescript('''
CREATE TABLE IF NOT EXISTS ws_clients (
tenant_id INTEGER NOT NULL,
client_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
client_type TEXT,
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,
last_wake_at REAL,
last_wake_reason TEXT,
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)
);
CREATE TABLE IF NOT EXISTS ws_push_tokens (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tenant_id INTEGER NOT NULL,
client_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
token TEXT UNIQUE NOT NULL,
client_type TEXT NOT NULL,
platform TEXT,
auto_position INTEGER NOT NULL DEFAULT 0,
invalid INTEGER NOT NULL DEFAULT 0,
created_at REAL NOT NULL,
updated_at REAL NOT NULL,
FOREIGN KEY (tenant_id) REFERENCES tenants(id),
FOREIGN KEY (account_id) REFERENCES accounts(id)
);
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)
);
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)
);
CREATE TABLE IF NOT EXISTS ws_workers (
worker_id TEXT PRIMARY KEY,
started_at REAL NOT NULL,
heartbeat_at REAL NOT NULL
);
CREATE TABLE IF NOT EXISTS cache_state (
key TEXT PRIMARY KEY,
revision INTEGER NOT NULL DEFAULT 0,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_ws_clients_account
ON ws_clients(tenant_id, account_id);
CREATE INDEX IF NOT EXISTS idx_ws_push_tokens_client
ON ws_push_tokens(tenant_id, client_id);
CREATE INDEX IF NOT EXISTS idx_ws_push_tokens_offline
ON ws_push_tokens(tenant_id, invalid, client_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);
''')
# Bestehende Datenbanken nachziehen. Die Spalten sind nullable bzw. haben
# Defaults, damit laufende Clients ohne Datenverlust weiterarbeiten.
client_columns = {
row['name'] for row in db.execute('PRAGMA table_info(ws_clients)').fetchall()
}
for name, definition in (
('client_type', 'TEXT'),
('last_wake_at', 'REAL'),
('last_wake_reason', 'TEXT'),
):
if name not in client_columns:
db.execute(f'ALTER TABLE ws_clients ADD COLUMN {name} {definition}')
db.execute(
'CREATE INDEX IF NOT EXISTS idx_ws_clients_type '
'ON ws_clients(tenant_id, client_type, disconnected_at)'
)
db.commit()
def ensure_api_schema(db):
"""Legt die Tabelle für die externen API-Zugriffstoken an."""
db.executescript('''
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)
);
CREATE INDEX IF NOT EXISTS idx_api_tokens_tenant
ON api_tokens(tenant_id);
''')
db.commit()
def ensure_street_schema(db):
"""Ergänzt bestehende Datenbanken um das Straßenfeld ``zusatz``."""
streets_table = db.execute(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'streets'"
).fetchone()
if not streets_table:
return
columns = {
row['name'] for row in db.execute('PRAGMA table_info(streets)').fetchall()
}
if 'zusatz' not in columns:
db.execute(
"ALTER TABLE streets ADD COLUMN zusatz TEXT NOT NULL DEFAULT ''"
)
db.commit()
def ensure_schema():
"""Ergänzt bestehende Datenbanken um das Gruppen-Account-Zuordnungsmodell.
Neue Installationen erhalten das Schema über ``migrations/schema.sql``.
Für bestehende Datenbanken wird die frühere 1:1-Verknüpfung
``groups.account_id`` in die neue Tabelle ``group_accounts`` übernommen
und die alte Spalte durch einen Tabellen-Neubau entfernt.
"""
db = get_db()
ensure_ws_schema(db)
ensure_api_schema(db)
ensure_street_schema(db)
groups_table = db.execute(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'groups'"
).fetchone()
if not groups_table:
return
db.executescript('''
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)
);
CREATE INDEX IF NOT EXISTS idx_group_accounts_group
ON group_accounts(tenant_id, group_id);
CREATE INDEX IF NOT EXISTS idx_groups_tenant ON groups(tenant_id);
''')
db.commit()
columns = {
row['name'] for row in db.execute('PRAGMA table_info(groups)').fetchall()
}
if 'account_id' not in columns:
return
db.execute(
'''INSERT OR IGNORE INTO group_accounts (tenant_id, group_id, account_id)
SELECT g.tenant_id, g.id, g.account_id
FROM groups g
JOIN accounts a
ON a.id = g.account_id AND a.tenant_id = g.tenant_id
WHERE g.account_id IS NOT NULL AND a.role = 'Gruppe' '''
)
db.commit()
# SQLite kann eine UNIQUE-Spalte nicht per ALTER TABLE DROP COLUMN
# entfernen. Deshalb wird die Tabelle ohne die alte Spalte neu aufgebaut.
# Die bereits kopierten Zuordnungen bleiben dabei erhalten.
fk_enabled = db.execute('PRAGMA foreign_keys').fetchone()[0]
db.execute('PRAGMA foreign_keys = OFF')
try:
db.execute('DROP TABLE IF EXISTS groups_new')
db.execute('BEGIN IMMEDIATE')
db.execute('''
CREATE TABLE groups_new (
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)
)
''')
db.execute('''
INSERT INTO groups_new
(id, tenant_id, aliasname, state, last_lat, last_lon,
last_pos_at, created_at, updated_at)
SELECT id, tenant_id, aliasname, state, last_lat, last_lon,
last_pos_at, created_at, updated_at
FROM groups
''')
db.execute('DROP TABLE groups')
db.execute('ALTER TABLE groups_new RENAME TO groups')
db.execute('CREATE INDEX IF NOT EXISTS idx_groups_tenant ON groups(tenant_id)')
db.commit()
except sqlite3.DatabaseError as exc:
db.rollback()
try:
db.execute('DROP TABLE IF EXISTS groups_new')
db.commit()
except sqlite3.DatabaseError:
pass
current_app.logger.warning(
'groups.account_id konnte nicht entfernt werden: %s', exc
)
finally:
db.execute('PRAGMA foreign_keys = ON' if fk_enabled else 'PRAGMA foreign_keys = OFF')
def init_db():
"""Initialisiert die Datenbank mit dem Schema"""
db = get_db()
schema_path = Path(current_app.root_path).parent / 'migrations' / 'schema.sql'
with open(schema_path, 'r', encoding='utf-8') as f:
db.executescript(f.read())
db.commit()
ensure_schema()
@click.command('init-db')
def init_db_command():
"""CLI-Befehl zum Initialisieren der Datenbank"""
init_db()
click.echo('Datenbank wurde initialisiert.')