From 85fe57ea0cbfd15fff226e538df86354d319850f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 13 Feb 2025 10:39:04 +0100 Subject: [PATCH] feat(database): :sparkles: add database table prefixing --- docker_dev/mysql_schema.sql | 18 ++++--- www/src/db_handler/mysql_handler.php | 72 ++++++++++++++-------------- www/src/setup/db.php | 4 +- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/docker_dev/mysql_schema.sql b/docker_dev/mysql_schema.sql index afc6d22..294266d 100644 --- a/docker_dev/mysql_schema.sql +++ b/docker_dev/mysql_schema.sql @@ -8,11 +8,9 @@ USE dragon_fire; -- DROP TABLE path_errcodes; -- DROP TABLE feed_cache; -CREATE TABLE posts ( +CREATE TABLE dev_posts ( post_id INTEGER AUTO_INCREMENT, - host VARCHAR(64) NOT NULL, - post_path VARCHAR(255) NOT NULL, post_path_depth INTEGER NOT NULL DEFAULT 0, @@ -29,13 +27,13 @@ CREATE TABLE posts ( post_settings_cache JSON DEFAULT NULL, PRIMARY KEY(post_id), - CONSTRAINT unique_post UNIQUE (host, post_path), + CONSTRAINT unique_post UNIQUE (post_path), - INDEX(host, post_path), - INDEX(host, post_path_depth, post_path), + INDEX(post_path), + INDEX(post_path_depth, post_path), - INDEX(host, post_created_at), - INDEX(host, post_updated_at), + INDEX(post_created_at), + INDEX(post_updated_at), FULLTEXT(post_path), FULLTEXT(post_tags), @@ -43,13 +41,13 @@ CREATE TABLE posts ( FULLTEXT(post_brief) ); -CREATE TABLE post_markdown ( +CREATE TABLE dev_post_markdown ( post_id INTEGER, post_markdown TEXT, PRIMARY KEY(post_id), - FOREIGN KEY(post_id) REFERENCES posts(post_id) + FOREIGN KEY(post_id) REFERENCES dev_posts(post_id) ON DELETE CASCADE, FULLTEXT(post_markdown) diff --git a/www/src/db_handler/mysql_handler.php b/www/src/db_handler/mysql_handler.php index bf63e9d..dfbad78 100644 --- a/www/src/db_handler/mysql_handler.php +++ b/www/src/db_handler/mysql_handler.php @@ -15,14 +15,17 @@ class MySQLHandler CONST SQL_WRITE_COLUMNS = ['path', 'title', 'brief']; private $sql_connection; + private $db_prefix; public $hostname; - public $debugging; - function __construct($sql_connection, $hostname) { + + + function __construct($sql_connection, $hostname, $db_prefix) { $this->sql_connection = $sql_connection; $this->hostname = $hostname; + $this->db_prefix = $db_prefix; $this->debugging = false; } @@ -48,10 +51,10 @@ class MySQLHandler $post_path = sanitize_post_path($post_path); $this->_exec(" - UPDATE posts + UPDATE {$this->db_prefix}_posts SET post_settings_cache=NULL - WHERE host = ? AND post_path LIKE ?; - ", "ss", $this->hostname, $post_path . "%"); + WHERE post_path LIKE ?; + ", "s", $post_path . "%"); } public function stub_postdata($path) { @@ -60,14 +63,13 @@ class MySQLHandler $qry = " - INSERT INTO posts - (host, post_path, post_path_depth) + INSERT INTO {$this->db_prefix}_posts + (post_path, post_path_depth) VALUES - ( ?, ?, ?) AS new + ( ?, ?) AS new ON DUPLICATE KEY UPDATE post_path=new.post_path;"; - $this->_exec($qry, "ssi", - $this->hostname, + $this->_exec($qry, "si", $post_path, $path_depth); } @@ -108,7 +110,6 @@ class MySQLHandler ); $sql_args = [ - $this->hostname, $post_path, substr_count($post_path, "/"), $data['title'], @@ -126,13 +127,12 @@ class MySQLHandler array_push($sql_args, json_encode($data)); $qry = - "INSERT INTO posts - (host, - post_path, post_path_depth, + "INSERT INTO {$this->db_prefix}_posts + (post_path, post_path_depth, post_title, post_tags, post_brief, post_metadata, post_settings_cache) VALUES - ( ?, ?, ?, ?, ?, ?, ?, null) AS new + ( ?, ?, ?, ?, ?, ?, null) AS new ON DUPLICATE KEY UPDATE post_title=new.post_title, post_tags=new.post_tags, @@ -141,7 +141,7 @@ class MySQLHandler post_updated_at=CURRENT_TIMESTAMP; "; - $this->_exec($qry, "ssissss", ...$sql_args); + $this->_exec($qry, "sissss", ...$sql_args); if(isset($post_markdown)) { $this->set_post_markdown($this->sql_connection->insert_id, $post_markdown); @@ -152,7 +152,7 @@ class MySQLHandler public function set_post_markdown($id, $markdown) { $qry = - "INSERT INTO post_markdown ( post_id, post_markdown ) + "INSERT INTO {$this->db_prefix}_post_markdown ( post_id, post_markdown ) VALUES (?, ?) AS new ON DUPLICATE KEY UPDATE post_markdown=new.post_markdown; "; @@ -167,9 +167,9 @@ class MySQLHandler $post_settings = $this->_exec(" SELECT post_settings_cache - FROM posts - WHERE post_path = ? AND host = ? - ", "ss", $post_path, $this->hostname)->fetch_assoc(); + FROM {$this->db_prefix}_posts + WHERE post_path = ? + ", "s", $post_path)->fetch_assoc(); if(!isset($post_settings)) { $this->_dbg("-> gps: Returning because of no result\n"); @@ -192,9 +192,9 @@ class MySQLHandler $post_settings = []; $post_metadata = $this->_exec(" SELECT post_path, post_metadata - FROM posts - WHERE post_path = ? AND host = ? - ", "ss", $post_path, $this->hostname)->fetch_assoc(); + FROM {$this->db_prefix}_posts + WHERE post_path = ? + ", "s", $post_path)->fetch_assoc(); if(isset($post_metadata['post_metadata'])) { $post_metadata = json_decode($post_metadata['post_metadata'], true); @@ -209,9 +209,9 @@ class MySQLHandler $this->_dbg("-> gps: Merged post settings are " . json_encode($post_settings) . ", saving...\n"); $this->_exec(" - UPDATE posts SET post_settings_cache=? WHERE post_path=? AND host=? - ", "sss", - json_encode($post_settings), $post_path, $this->hostname); + UPDATE {$this->db_prefix}_posts SET post_settings_cache=? WHERE post_path=? + ", "ss", + json_encode($post_settings), $post_path); return $post_settings; } @@ -245,6 +245,7 @@ class MySQLHandler } $outdata = array_merge($post_settings, $post_metadata, $outdata); + $outdata['host'] ??= $this->hostname; return $outdata; } @@ -254,11 +255,11 @@ class MySQLHandler $qry = " SELECT * - FROM posts - WHERE post_path = ? AND host = ?; + FROM {$this->db_prefix}_posts + WHERE post_path = ?; "; - $data = $this->_exec($qry, "ss", $path, $this->hostname)->fetch_assoc(); + $data = $this->_exec($qry, "s", $path)->fetch_assoc(); return $this->process_postdata($data); } @@ -291,7 +292,7 @@ class MySQLHandler $qry = " SELECT * - FROM posts + FROM {$this->db_prefix}_posts WHERE post_path_depth BETWEEN ? AND ? AND post_path LIKE ? ORDER BY " . $order_by . @@ -314,17 +315,17 @@ class MySQLHandler public function get_post_markdown($id) { $qry = "SELECT post_markdown - FROM post_markdown + FROM {$this->db_prefix}_post_markdown WHERE post_id = ? "; $data = $this->_exec($qry, "i", $id)->fetch_assoc(); if(!isset($data)) { - return ""; + return ''; } - return $data['post_markdown']; + return $data['post_markdown'] ?? ''; } public function parse_search_query_string($text) { @@ -469,8 +470,9 @@ class MySQLHandler $qry = "SELECT " . implode(', ', $qry_selects) . " - FROM posts - LEFT JOIN post_markdown ON posts.post_id = post_markdown.post_id + FROM {$this->db_prefix}_posts AS posts + LEFT JOIN {$this->db_prefix}_post_markdown AS post_markdown + ON posts.post_id = post_markdown.post_id WHERE " . implode(' and ', $qry_wheres) . " ORDER BY post_search_score DESC LIMIT " . $options['limit'] . " diff --git a/www/src/setup/db.php b/www/src/setup/db.php index 30a7357..39c340f 100644 --- a/www/src/setup/db.php +++ b/www/src/setup/db.php @@ -25,7 +25,9 @@ try { die(); } -$sql_adapter = new MySQLHandler($db_connection, $SERVER_HOST); +$sql_adapter = new MySQLHandler($db_connection, + $SITE_CONFIG['site_defaults']['uri_prefix'], + $db_params['prefix']); $adapter = new PostHandler($sql_adapter); require_once 'dergdown.php';