"""Datenbankzugriff (PyMySQL) + automatisches Schema-Init. Verwendung: from db import get_connection, init_db """ import pymysql from pymysql.cursors import DictCursor from config import Config def get_connection(): """Öffnet eine neue Verbindung zur MySQL-Datenbank.""" return pymysql.connect( host=Config.DB_HOST, port=Config.DB_PORT, user=Config.DB_USER, password=Config.DB_PASSWORD, database=Config.DB_DATABASE, charset="utf8mb4", cursorclass=DictCursor, autocommit=True, ) SCHEMA = """ CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(100) NOT NULL UNIQUE, email VARCHAR(255), salt VARCHAR(128) NOT NULL, password_hash VARCHAR(512) NOT NULL, is_admin TINYINT(1) NOT NULL DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS notes ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, note_date DATE NOT NULL, bullet_type ENUM('.', 'x', '-', '?') NOT NULL DEFAULT '-', note_text TEXT NOT NULL, priority TINYINT NOT NULL DEFAULT 0, status ENUM('open', 'done', 'moved') NOT NULL DEFAULT 'open', moved_to_date DATE NULL, moved_to_future TINYINT(1) NOT NULL DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_notes_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS future_log ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, month_date DATE NOT NULL, note_text TEXT NOT NULL, status ENUM('open', 'done') NOT NULL DEFAULT 'open', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CONSTRAINT fk_future_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; """ def init_db(): """Erstellt das Schema, falls es noch nicht existiert.""" conn = get_connection() try: with conn.cursor() as cur: for statement in SCHEMA.split(";"): stmt = statement.strip() if stmt: cur.execute(stmt) finally: conn.close()