DatabaseInitializer

This commit is contained in:
NOrtmann1
2026-06-24 16:36:11 +02:00
parent ed31210e3a
commit 3d65f79d2c
4 changed files with 80 additions and 100 deletions
+5 -38
View File
@@ -9,48 +9,18 @@ require_once 'Article.php';
class DatabaseArticleManager implements ArticleManagerDAO {
private static $instance = null;
private $dbPath;
/**
* Konstruktor
*
* Erstellt die Datenbankverbindung und die Tabelle articles.
* @throws InternalServerErrorException
* @throws RuntimeException
*/
public function __construct()
{
if (!file_exists(__DIR__ . '/../../db/eduforgeDB.db')) {
try {
$db = $this->getConnection();
// Tabelle für Beiträge
$db->exec("
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
author TEXT,
category TEXT,
tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
);");
// Tabelle für Likes
$db->exec("
CREATE TABLE IF NOT EXISTS likes (
article_id INTEGER,
user_id TEXT,
PRIMARY KEY (article_id, user_id),
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE
);");
unset($db);
} catch (PDOException $e) {
throw new InternalServerErrorException($e->getMessage());
}
}
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
DatabaseInitializer::initialize($this->dbPath);
}
/**
@@ -62,12 +32,9 @@ class DatabaseArticleManager implements ArticleManagerDAO {
private function getConnection()
{
try {
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
$db = new PDO($dsn, null, null);
$db = new PDO('sqlite:' . $this->dbPath, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
} catch (PDOException $e) {
throw new RuntimeException("internal_error");
+4 -39
View File
@@ -12,46 +12,15 @@ require_once "Comment.php";
class DatabaseCommentManager implements CommentManagerDAO
{
private static $instance = null;
private $dbPath;
/**
* Erstellt die Kommentartabelle, falls diese noch nicht existiert.
*/
public function __construct()
{
try {
$db = $this->getConnection();
$db->exec("
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
parent_comment_id INTEGER NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE,
FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE
);
");
$columns = $db->query("PRAGMA table_info(comments);")->fetchAll(PDO::FETCH_ASSOC);
$hasParentColumn = false;
foreach ($columns as $column) {
if ($column["name"] === "parent_comment_id") {
$hasParentColumn = true;
break;
}
}
if (!$hasParentColumn) {
$db->exec("ALTER TABLE comments ADD COLUMN parent_comment_id INTEGER NULL;");
}
} catch (PDOException $e) {
throw new RuntimeException("internal_error");
}
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
DatabaseInitializer::initialize($this->dbPath);
}
/**
@@ -62,14 +31,10 @@ class DatabaseCommentManager implements CommentManagerDAO
private function getConnection()
{
try {
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
$db = new PDO($dsn, null, null);
$db = new PDO('sqlite:' . $this->dbPath, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
} catch (PDOException $e) {
throw new RuntimeException("internal_error");
}
+66
View File
@@ -0,0 +1,66 @@
<?php
/**
* Diese Klasse legt einmalig beim allerersten Start in der exakt korrekten Reihenfolge die Tabellen an
*
* @author Niklas Ortmann
*/
class DatabaseInitializer {
public static function initialize($dbPath) {
if (!file_exists($dbPath)) {
try {
$db = new PDO('sqlite:' . $dbPath);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
$db->exec("
CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY,
vorname TEXT NOT NULL,
nachname TEXT NOT NULL,
password TEXT NOT NULL
);
");
$db->exec("
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
author TEXT,
category TEXT,
tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
);
");
$db->exec("
CREATE TABLE IF NOT EXISTS likes (
article_id INTEGER,
user_id TEXT,
PRIMARY KEY (article_id, user_id),
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE
);
");
$db->exec("
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
parent_comment_id INTEGER NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE,
FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE
);
");
} catch (PDOException $e) {
throw new RuntimeException("internal-error");
}
}
}
}
+5 -23
View File
@@ -11,6 +11,7 @@ require_once "UserManagerDAO.php";
class DatabaseUserManager implements UserManagerDAO {
private static $instance = null;
private $dbPath;
/**
* Konstruktor.
@@ -22,23 +23,8 @@ class DatabaseUserManager implements UserManagerDAO {
*/
public function __construct()
{
try {
$db = $this->getConnection();
$db->exec("
CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY,
vorname TEXT NOT NULL,
nachname TEXT NOT NULL,
password TEXT NOT NULL
);
");
unset($db);
} catch (PDOException $e) {
throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden.");
}
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
DatabaseInitializer::initialize($this->dbPath);
}
/**
@@ -51,16 +37,12 @@ class DatabaseUserManager implements UserManagerDAO {
private function getConnection()
{
try {
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
$db = new PDO($dsn, null, null);
$db = new PDO('sqlite:' . $this->dbPath, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
} catch (PDOException $e) {
throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen.");
throw new RuntimeException("internal_error");
}
}