70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sqlite3
|
|
import os
|
|
|
|
# Prüfen, ob die Datenbank existiert
|
|
db_path = 'systades.db'
|
|
if not os.path.exists(db_path):
|
|
print(f"Datenbank {db_path} existiert nicht.")
|
|
exit(1)
|
|
|
|
# Verbindung zur Datenbank herstellen
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# Prüfen, ob die User-Tabelle existiert
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user';")
|
|
if not cursor.fetchone():
|
|
print("Die Tabelle 'user' existiert nicht. Erstelle neue Tabelle...")
|
|
cursor.execute('''
|
|
CREATE TABLE user (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username VARCHAR(80) NOT NULL UNIQUE,
|
|
email VARCHAR(120) NOT NULL UNIQUE,
|
|
password VARCHAR(512) NOT NULL,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
is_active BOOLEAN DEFAULT 1,
|
|
role VARCHAR(20) DEFAULT 'user'
|
|
)
|
|
''')
|
|
conn.commit()
|
|
print("Die Tabelle 'user' wurde erfolgreich erstellt.")
|
|
else:
|
|
# Überprüfen, ob die Spalte 'password' existiert
|
|
cursor.execute("PRAGMA table_info(user);")
|
|
columns = cursor.fetchall()
|
|
column_names = [column[1] for column in columns]
|
|
|
|
if 'password' not in column_names:
|
|
print("Die Spalte 'password' fehlt in der Tabelle 'user'. Füge Spalte hinzu...")
|
|
cursor.execute("ALTER TABLE user ADD COLUMN password VARCHAR(512);")
|
|
conn.commit()
|
|
print("Die Spalte 'password' wurde erfolgreich hinzugefügt.")
|
|
else:
|
|
print("Die Spalte 'password' existiert bereits in der Tabelle 'user'.")
|
|
|
|
# Überprüfen der aktualisierten Spaltenstruktur
|
|
print("\nAktualisierte Tabellenspalten der 'user'-Tabelle:")
|
|
cursor.execute("PRAGMA table_info(user);")
|
|
updated_columns = cursor.fetchall()
|
|
for column in updated_columns:
|
|
print(f"Column: {column[1]}, Type: {column[2]}, NOT NULL: {column[3]}, Default: {column[4]}, Primary Key: {column[5]}")
|
|
|
|
# Datenbanktabellen anzeigen
|
|
print("\nAlle Tabellen in der Datenbank:")
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
|
tables = cursor.fetchall()
|
|
for table in tables:
|
|
print(f"- {table[0]}")
|
|
|
|
# Schemaüberprüfung der user-Tabelle
|
|
print("\nSchema der 'user'-Tabelle:")
|
|
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='user';")
|
|
schema = cursor.fetchone()
|
|
if schema:
|
|
print(schema[0])
|
|
|
|
conn.close()
|
|
print("\nDatenbankaktualisierung abgeschlossen.") |