-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
50 lines (41 loc) · 1.38 KB
/
Copy pathinit_db.py
File metadata and controls
50 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import mysql.connector
from db import get_connection, load_db_config
def main():
"""
Initialisiert die Datenbank:
1. Erstellt die Datenbank, falls sie nicht existiert
2. Führt alle ausstehenden Migrationen aus
"""
# Zuerst sicherstellen, dass die Datenbank existiert
cfg = load_db_config()
db_name = cfg["database"]
# Verbindung ohne Datenbank herstellen, um sie ggf. anzulegen
server_conn = mysql.connector.connect(
host=cfg["host"],
user=cfg["user"],
password=cfg["password"],
)
try:
cur = server_conn.cursor()
cur.execute(
f"CREATE DATABASE IF NOT EXISTS `{db_name}` "
"CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
)
server_conn.commit()
cur.close()
finally:
server_conn.close()
# Führe Migrationen aus (dies erstellt auch die initialen Tabellen)
print("🔄 Führe Datenbank-Migrationen aus...")
try:
from migrate import main as run_migrations
run_migrations()
print("✅ Datenbankinitialisierung abgeschlossen.")
except ImportError:
print("⚠️ migrate.py nicht gefunden, überspringe Migrationen.")
print(" (Dies ist normal bei der ersten Installation)")
except Exception as e:
print(f"❌ Fehler bei Migrationen: {e}")
raise
if __name__ == "__main__":
main()