diff --git a/database/db_init.py b/database/db_init.py index 2622578..6b7c921 100644 --- a/database/db_init.py +++ b/database/db_init.py @@ -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) - -# SQLite foreign keys activeren conn.execute("PRAGMA foreign_keys = ON;") - cur = conn.cursor() @@ -14,11 +15,28 @@ cur.execute(""" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, + email TEXT UNIQUE, + email_verified INTEGER NOT NULL DEFAULT 0, 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(""" CREATE TABLE IF NOT EXISTS rooms ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -26,7 +44,8 @@ CREATE TABLE IF NOT EXISTS rooms ( owner_id INTEGER NOT NULL, 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(""" CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, - room_id INTEGER NOT NULL, creator_id INTEGER NOT NULL, @@ -75,6 +93,9 @@ CREATE TABLE IF NOT EXISTS events ( min_people INTEGER DEFAULT 0, + invitation_mode TEXT NOT NULL DEFAULT 'room' + CHECK(invitation_mode IN ('room', 'custom')), + FOREIGN KEY (room_id) REFERENCES rooms(id) 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(""" CREATE TABLE IF NOT EXISTS event_signups ( event_id INTEGER NOT NULL, @@ -93,20 +137,19 @@ CREATE TABLE IF NOT EXISTS event_signups ( status TEXT NOT NULL DEFAULT 'going' CHECK(status IN ('going', 'maybe', 'declined')), - PRIMARY KEY(event_id, user_id), + PRIMARY KEY (event_id, user_id), - FOREIGN KEY(event_id) + FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE, - FOREIGN KEY(user_id) + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); """) -# Snellere zoekopdrachten cur.execute(""" CREATE INDEX IF NOT EXISTS idx_room_members_user ON room_members(user_id); @@ -117,16 +160,39 @@ CREATE INDEX IF NOT EXISTS idx_events_room 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() +print("Database initialized.") +print(f"Database: {DB_NAME}") +print("Tables:") -print("Database aangemaakt.") -print("Tabellen:") - -for row in cur.execute( - "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" -): +for row in cur.execute(""" + SELECT name + FROM sqlite_master + WHERE type = 'table' + ORDER BY name; +"""): print(" -", row[0]) diff --git a/database/db_update.py b/database/db_update.py new file mode 100644 index 0000000..7fd3163 --- /dev/null +++ b/database/db_update.py @@ -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) \ No newline at end of file