Firebase Cloud Messaging 1. Versuch

This commit is contained in:
concierge committed 2026-11-08 12:25:35 +01:00
1 parent f1fb926c2f
commit 4c8b11c4d5
25 files changed
+2111 -31

No files matched your search

+47 -1
View File
@@ -32,18 +32,27 @@ def close_db(e=None):
def ensure_ws_schema(db):
"""Legt die Tabellen für den prozessübergreifenden Broker-Zustand an."""
"""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),
@@ -51,6 +60,22 @@ def ensure_ws_schema(db):
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,
@@ -82,11 +107,32 @@ def ensure_ws_schema(db):
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()