DatabaseInitializer
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user