updated some files

This commit is contained in:
ben
2026-08-18 10:50:11 +02:00
parent b85b8dbb25
commit 5f77a4d995
2 changed files with 126 additions and 17 deletions
+80 -14
View File
@@ -1,12 +1,13 @@
import sqlite3 #!/bin/python3
DB_NAME = "calendar.db" import sqlite3
from pathlib import Path
DB_NAME = Path(__file__).parent / "calendar.db"
conn = sqlite3.connect(DB_NAME) conn = sqlite3.connect(DB_NAME)
# SQLite foreign keys activeren
conn.execute("PRAGMA foreign_keys = ON;") conn.execute("PRAGMA foreign_keys = ON;")
cur = conn.cursor() cur = conn.cursor()
@@ -14,11 +15,28 @@ cur.execute("""
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE, username TEXT NOT NULL UNIQUE,
email TEXT UNIQUE,
email_verified INTEGER NOT NULL DEFAULT 0,
password_hash TEXT NOT NULL password_hash TEXT NOT NULL
); );
""") """)
cur.execute("""
CREATE TABLE IF NOT EXISTS email_verifications (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
token_hash TEXT NOT NULL UNIQUE,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE
);
""")
cur.execute(""" cur.execute("""
CREATE TABLE IF NOT EXISTS rooms ( CREATE TABLE IF NOT EXISTS rooms (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -26,7 +44,8 @@ CREATE TABLE IF NOT EXISTS rooms (
owner_id INTEGER NOT NULL, owner_id INTEGER NOT NULL,
invite_code TEXT NOT NULL UNIQUE, invite_code TEXT NOT NULL UNIQUE,
FOREIGN KEY (owner_id) REFERENCES users(id) FOREIGN KEY (owner_id)
REFERENCES users(id)
); );
""") """)
@@ -54,7 +73,6 @@ CREATE TABLE IF NOT EXISTS room_members (
cur.execute(""" cur.execute("""
CREATE TABLE IF NOT EXISTS events ( CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id INTEGER NOT NULL, room_id INTEGER NOT NULL,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
@@ -75,6 +93,9 @@ CREATE TABLE IF NOT EXISTS events (
min_people INTEGER DEFAULT 0, min_people INTEGER DEFAULT 0,
invitation_mode TEXT NOT NULL DEFAULT 'room'
CHECK(invitation_mode IN ('room', 'custom')),
FOREIGN KEY (room_id) FOREIGN KEY (room_id)
REFERENCES rooms(id) REFERENCES rooms(id)
ON DELETE CASCADE, ON DELETE CASCADE,
@@ -85,6 +106,29 @@ CREATE TABLE IF NOT EXISTS events (
""") """)
cur.execute("""
CREATE TABLE IF NOT EXISTS event_invitations (
event_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'pending'
CHECK(status IN ('pending', 'accepted', 'declined')),
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (event_id, user_id),
FOREIGN KEY (event_id)
REFERENCES events(id)
ON DELETE CASCADE,
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE
);
""")
cur.execute(""" cur.execute("""
CREATE TABLE IF NOT EXISTS event_signups ( CREATE TABLE IF NOT EXISTS event_signups (
event_id INTEGER NOT NULL, event_id INTEGER NOT NULL,
@@ -106,7 +150,6 @@ CREATE TABLE IF NOT EXISTS event_signups (
""") """)
# Snellere zoekopdrachten
cur.execute(""" cur.execute("""
CREATE INDEX IF NOT EXISTS idx_room_members_user CREATE INDEX IF NOT EXISTS idx_room_members_user
ON room_members(user_id); ON room_members(user_id);
@@ -117,16 +160,39 @@ CREATE INDEX IF NOT EXISTS idx_events_room
ON events(room_id); ON events(room_id);
""") """)
cur.execute("""
CREATE INDEX IF NOT EXISTS idx_event_invitations_user
ON event_invitations(user_id);
""")
cur.execute("""
CREATE INDEX IF NOT EXISTS idx_event_invitations_event
ON event_invitations(event_id);
""")
cur.execute("""
CREATE INDEX IF NOT EXISTS idx_event_signups_user
ON event_signups(user_id);
""")
cur.execute("""
CREATE INDEX IF NOT EXISTS idx_email_verifications_user
ON email_verifications(user_id);
""")
conn.commit() conn.commit()
print("Database initialized.")
print(f"Database: {DB_NAME}")
print("Tables:")
print("Database aangemaakt.") for row in cur.execute("""
print("Tabellen:") SELECT name
FROM sqlite_master
for row in cur.execute( WHERE type = 'table'
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" ORDER BY name;
): """):
print(" -", row[0]) print(" -", row[0])
+43
View File
@@ -0,0 +1,43 @@
#!/bin/python3
import sqlite3
from pathlib import Path
DB_NAME = Path(__file__).parent / "calendar.db"
try:
conn = sqlite3.connect(DB_NAME)
conn.execute("PRAGMA foreign_keys = ON;")
cur = conn.cursor()
# Migrations
conn.commit()
conn.close()
print("Database updated.")
except sqlite3.Error as e:
print(f"Database error: {e}")
try:
conn.rollback()
conn.close()
except (NameError, sqlite3.Error):
pass
raise SystemExit(1)
except Exception as e:
print(f"Error: {e}")
try:
conn.rollback()
conn.close()
except (NameError, sqlite3.Error):
pass
raise SystemExit(1)