From 3896b213453493f74401a3b8ef4f66e2ced74ff7 Mon Sep 17 00:00:00 2001 From: Ctrl-S Date: Sun, 24 Jul 2022 16:34:28 +0800 Subject: [PATCH] Add scripts to automate creation and modification of board tables, update readme to reflect API changes. --- readme.md | 16 +-- scripts/change-column-sizes.sh | 67 +++++++++++++ scripts/create-triggers.sh | 40 ++++++++ scripts/create_board_tables.sh | 40 ++++++++ scripts/create_tables.template.sql | 143 +++++++++++++++++++++++++++ scripts/create_triggers.template.sql | 106 ++++++++++++++++++++ scripts/modify_columns.template.sql | 25 +++++ scripts/root.my.cnf | 24 +++++ 8 files changed, 453 insertions(+), 8 deletions(-) create mode 100644 scripts/change-column-sizes.sh create mode 100644 scripts/create-triggers.sh create mode 100644 scripts/create_board_tables.sh create mode 100644 scripts/create_tables.template.sql create mode 100644 scripts/create_triggers.template.sql create mode 100644 scripts/modify_columns.template.sql create mode 100644 scripts/root.my.cnf diff --git a/readme.md b/readme.md index c866209..c267ecc 100644 --- a/readme.md +++ b/readme.md @@ -177,7 +177,7 @@ CREATE TABLE `%%BOARD%%` ( `op` tinyint(1) NOT NULL DEFAULT 0, `timestamp` int(10) unsigned NOT NULL, `timestamp_expired` int(10) unsigned NOT NULL, - `preview_orig` varchar(20) DEFAULT NULL, + `preview_orig` varchar(23) DEFAULT NULL, `preview_w` smallint(5) unsigned NOT NULL DEFAULT 0, `preview_h` smallint(5) unsigned NOT NULL DEFAULT 0, `media_filename` text DEFAULT NULL, @@ -185,7 +185,7 @@ CREATE TABLE `%%BOARD%%` ( `media_h` smallint(5) unsigned NOT NULL DEFAULT 0, `media_size` int(10) unsigned NOT NULL DEFAULT 0, `media_hash` varchar(25) DEFAULT NULL, - `media_orig` varchar(20) DEFAULT NULL, + `media_orig` varchar(23) DEFAULT NULL, `spoiler` tinyint(1) NOT NULL DEFAULT 0, `deleted` tinyint(1) NOT NULL DEFAULT 0, `capcode` varchar(1) NOT NULL DEFAULT 'N', @@ -225,7 +225,7 @@ CREATE TABLE `%%BOARD%%_deleted` ( `op` tinyint(1) NOT NULL DEFAULT 0, `timestamp` int(10) unsigned NOT NULL, `timestamp_expired` int(10) unsigned NOT NULL, - `preview_orig` varchar(20) DEFAULT NULL, + `preview_orig` varchar(23) DEFAULT NULL, `preview_w` smallint(5) unsigned NOT NULL DEFAULT 0, `preview_h` smallint(5) unsigned NOT NULL DEFAULT 0, `media_filename` text DEFAULT NULL, @@ -233,7 +233,7 @@ CREATE TABLE `%%BOARD%%_deleted` ( `media_h` smallint(5) unsigned NOT NULL DEFAULT 0, `media_size` int(10) unsigned NOT NULL DEFAULT 0, `media_hash` varchar(25) DEFAULT NULL, - `media_orig` varchar(20) DEFAULT NULL, + `media_orig` varchar(23) DEFAULT NULL, `spoiler` tinyint(1) NOT NULL DEFAULT 0, `deleted` tinyint(1) NOT NULL DEFAULT 0, `capcode` varchar(1) NOT NULL DEFAULT 'N', @@ -266,9 +266,9 @@ CREATE TABLE `%%BOARD%%_deleted` ( CREATE TABLE `%%BOARD%%_images` ( `media_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `media_hash` varchar(25) NOT NULL, - `media` varchar(20) DEFAULT NULL, - `preview_op` varchar(20) DEFAULT NULL, - `preview_reply` varchar(20) DEFAULT NULL, + `media` varchar(23) DEFAULT NULL, + `preview_op` varchar(23) DEFAULT NULL, + `preview_reply` varchar(23) DEFAULT NULL, `total` int(10) unsigned NOT NULL DEFAULT 0, `banned` smallint(5) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (`media_id`), @@ -347,7 +347,7 @@ BEGIN DELETE FROM `%%BOARD%%_threads` WHERE thread_num = tnum; END;; -CREATE PROCEDURE `insert_image_%%BOARD%%` (n_media_hash VARCHAR(25), n_media VARCHAR(20), n_preview VARCHAR(20), n_op INT) +CREATE PROCEDURE `insert_image_%%BOARD%%` (n_media_hash VARCHAR(25), n_media VARCHAR(23), n_preview VARCHAR(23), n_op INT) BEGIN IF n_op = 1 THEN INSERT INTO `%%BOARD%%_images` (media_hash, media, preview_op, total) diff --git a/scripts/change-column-sizes.sh b/scripts/change-column-sizes.sh new file mode 100644 index 0000000..bba2594 --- /dev/null +++ b/scripts/change-column-sizes.sh @@ -0,0 +1,67 @@ +#!/bin/bash +## change-column-sizes.sh +## Modify tables for Neofuuka to be compatable with net 4chan image timestamp values. +## (This change occured during 2022-07.) +## +set -v # Print lines as they are run (bash option). +echo "#[${0##*/}]" "Starting" +echo "#[${0##*/}]" "Running as: $(whoami)@$(hostname)" + + +##=====< Config >=====## +mariadb_db="fourchan" # Database name. +mariadb_conf="root.my.cnf" # Superuser .my.cnf file. +boards=( # Shortnames of boards (e.g. 'mlp' or 'g' ) + 'g' + 'a' + 'c' + 'k' +) +##=====< /Config >=====## + + +##=====< Modify tables and triggers >=====## +echo "#[${0##*/}]" "boards=$boards" +mkdir -vp tmp/ # Just a place to temporarily store the SQL for templating and execution. +for board in ${boards[@]}; do + echo "#[${0##*/}]" "Modifying columns and triggers for:" "board=$board" + + ## ----- < Modify columns > ----- ## + ## ALTER TABLE statements have to be used here, so it needs its own SQL template file. + echo "#[${0##*/}]" "Altering tables for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + + ## Template SQL: + cp -v "modify_columns.template.sql" tmp/"modify_columns.${board}.sql" + sed -i "s/%%BOARD%%/${board}/g" tmp/"modify_columns.${board}.sql" + + ## Run the SQL: + ## https://mariadb.com/kb/en/mysql-command-line-client/ + /bin/time/ -- mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" \ + < tmp/"modify_columns.${board}.sql" + + echo "#[${0##*/}]" "Altered tables for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + ## ----- < /Modify columns > ----- ## + + + ## ----- < /Modify triggers > ----- ## + ## The create triggers SQL drops triggers and recreates them already, so just copypaste it (with the appropriate column sizes fixed). + echo "#[${0##*/}]" "Creating triggers for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + + ## Template SQL: + cp -v "create_triggers.template.sql" tmp/"create_triggers.${board}.sql" + sed -i "s/%%BOARD%%/${board}/g" tmp/"create_triggers.${board}.sql" + + ## Run the SQL: + ## https://mariadb.com/kb/en/mysql-command-line-client/ + /bin/time/ -- mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" --progress-reports \ + < tmp/"create_triggers.${board}.sql" + + echo "#[${0##*/}]" "Created triggers for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + ## ----- < /Modify triggers > ----- ## + + echo "#[${0##*/}]" "Modified:" "board=$board" +done +##=====< /Modify tables and triggers >=====## + + +echo "#[${0##*/}]" "Finished" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" diff --git a/scripts/create-triggers.sh b/scripts/create-triggers.sh new file mode 100644 index 0000000..38324f6 --- /dev/null +++ b/scripts/create-triggers.sh @@ -0,0 +1,40 @@ +#!/bin/bash +## create-triggers.sh +## Create triggers for specified boards. +## +set -v # Print lines as they are run (bash option). +echo "#[${0##*/}]" "Starting" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" +echo "#[${0##*/}]" "Running as: $(whoami)@$(hostname) in $(pwd)" + + +##=====< Config >=====## +mariadb_db="fourchan" # Database name. +mariadb_conf="root.my.cnf" # Superuser .my.cnf file. +boards=( # Shortnames of boards (e.g. 'mlp' or 'g' ) + 'h' + 'i' +) +##=====< /Config >=====## + + +##=====< Create triggers >=====## +echo "#[${0##*/}]" "boards=$boards" +mkdir -vp tmp/ +for board in ${boards[@]}; do + echo "#[${0##*/}]" "Creating triggers for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + + ## Template SQL: + cp -v "create_triggers.template.sql" tmp/"create_triggers.${board}.sql" + sed -i "s/%%BOARD%%/${board}/g" tmp/"create_triggers.${board}.sql" + + ## Run the SQL: + ## https://mariadb.com/kb/en/mysql-command-line-client/ + /bin/time/ -- mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" --progress-reports \ + < tmp/"create_triggers.${board}.sql" + + echo "#[${0##*/}]" "Created triggers for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" +done +##=====< /Create triggers >=====## + + +echo "#[${0##*/}]" "Finished" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" diff --git a/scripts/create_board_tables.sh b/scripts/create_board_tables.sh new file mode 100644 index 0000000..c5e4fc3 --- /dev/null +++ b/scripts/create_board_tables.sh @@ -0,0 +1,40 @@ +#!/bin/bash +## create_tables.sh +## Create tables for specified boards. +## +set -v # Print lines as they are run (bash option). +echo "#[${0##*/}]" "Starting" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" +echo "#[${0##*/}]" "Running as: $(whoami)@$(hostname) in $(pwd)" + + +##=====< Config >=====## +mariadb_db="fourchan" # Database name. +mariadb_conf="root.my.cnf" # Superuser .my.cnf file. +boards=( # Shortnames of boards (e.g. 'mlp' or 'g' ) + 'h' + 'i' +) +##=====< /Config >=====## + + +##=====< Create tables >=====## +echo "#[${0##*/}]" "boards=$boards" +mkdir -vp tmp/ +for board in ${boards[@]}; do + echo "#[${0##*/}]" "Creating tables for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" + + ## Template SQL: + cp -v "create_tables.template.sql" tmp/"create_tables.${board}.sql" + sed -i "s/%%BOARD%%/${board}/g" tmp/"create_tables.${board}.sql" + + ## Run the SQL: + ## https://mariadb.com/kb/en/mysql-command-line-client/ + /bin/time/ -- mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" --progress-reports \ + < tmp/"create_tables.${board}.sql" + + echo "#[${0##*/}]" "Created tables for:" "board=$board" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" +done +##=====< /Create tables >=====## + + +echo "#[${0##*/}]" "Finished" "[at $(date +%Y-%m-%d_%H-%M%z=@%s)]" diff --git a/scripts/create_tables.template.sql b/scripts/create_tables.template.sql new file mode 100644 index 0000000..220b5a4 --- /dev/null +++ b/scripts/create_tables.template.sql @@ -0,0 +1,143 @@ +/* + * create_triggers.template.sql + * Create tables for Neofuuka. + * + * $ cp -v "modify_columns.template.sql" "modify_columns.${board}.sql" + * $ sed -i "s/%%BOARD%%/${board}/g" "modify_columns.${board}.sql" + * + * $ mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" \ + * < "modify_columns.${board}.sql" + */ +CREATE TABLE `%%BOARD%%` ( + `doc_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `media_id` int(10) unsigned NOT NULL DEFAULT 0, + `poster_ip` decimal(39,0) unsigned NOT NULL DEFAULT 0, + `num` int(10) unsigned NOT NULL, + `subnum` int(10) unsigned NOT NULL, + `thread_num` int(10) unsigned NOT NULL DEFAULT 0, + `op` tinyint(1) NOT NULL DEFAULT 0, + `timestamp` int(10) unsigned NOT NULL, + `timestamp_expired` int(10) unsigned NOT NULL, + `preview_orig` varchar(23) DEFAULT NULL, + `preview_w` smallint(5) unsigned NOT NULL DEFAULT 0, + `preview_h` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_filename` text DEFAULT NULL, + `media_w` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_h` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_size` int(10) unsigned NOT NULL DEFAULT 0, + `media_hash` varchar(25) DEFAULT NULL, + `media_orig` varchar(23) DEFAULT NULL, + `spoiler` tinyint(1) NOT NULL DEFAULT 0, + `deleted` tinyint(1) NOT NULL DEFAULT 0, + `capcode` varchar(1) NOT NULL DEFAULT 'N', + `email` varchar(100) DEFAULT NULL, + `name` varchar(100) DEFAULT NULL, + `trip` varchar(25) DEFAULT NULL, + `title` varchar(100) DEFAULT NULL, + `comment` text DEFAULT NULL, + `delpass` tinytext DEFAULT NULL, + `sticky` tinyint(1) NOT NULL DEFAULT 0, + `locked` tinyint(1) NOT NULL DEFAULT 0, + `poster_hash` varchar(8) DEFAULT NULL, + `poster_country` varchar(2) DEFAULT NULL, + `exif` text DEFAULT NULL, + PRIMARY KEY (`doc_id`), + UNIQUE KEY `num_subnum_index` (`num`,`subnum`), + KEY `thread_num_subnum_index` (`thread_num`,`num`,`subnum`), + KEY `subnum_index` (`subnum`), + KEY `op_index` (`op`), + KEY `media_id_index` (`media_id`), + KEY `media_hash_index` (`media_hash`), + KEY `media_orig_index` (`media_orig`), + KEY `name_trip_index` (`name`,`trip`), + KEY `trip_index` (`trip`), + KEY `email_index` (`email`), + KEY `poster_ip_index` (`poster_ip`), + KEY `timestamp_index` (`timestamp`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `%%BOARD%%_deleted` ( + `doc_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `media_id` int(10) unsigned NOT NULL DEFAULT 0, + `poster_ip` decimal(39,0) unsigned NOT NULL DEFAULT 0, + `num` int(10) unsigned NOT NULL, + `subnum` int(10) unsigned NOT NULL, + `thread_num` int(10) unsigned NOT NULL DEFAULT 0, + `op` tinyint(1) NOT NULL DEFAULT 0, + `timestamp` int(10) unsigned NOT NULL, + `timestamp_expired` int(10) unsigned NOT NULL, + `preview_orig` varchar(23) DEFAULT NULL, + `preview_w` smallint(5) unsigned NOT NULL DEFAULT 0, + `preview_h` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_filename` text DEFAULT NULL, + `media_w` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_h` smallint(5) unsigned NOT NULL DEFAULT 0, + `media_size` int(10) unsigned NOT NULL DEFAULT 0, + `media_hash` varchar(25) DEFAULT NULL, + `media_orig` varchar(23) DEFAULT NULL, + `spoiler` tinyint(1) NOT NULL DEFAULT 0, + `deleted` tinyint(1) NOT NULL DEFAULT 0, + `capcode` varchar(1) NOT NULL DEFAULT 'N', + `email` varchar(100) DEFAULT NULL, + `name` varchar(100) DEFAULT NULL, + `trip` varchar(25) DEFAULT NULL, + `title` varchar(100) DEFAULT NULL, + `comment` text DEFAULT NULL, + `delpass` tinytext DEFAULT NULL, + `sticky` tinyint(1) NOT NULL DEFAULT 0, + `locked` tinyint(1) NOT NULL DEFAULT 0, + `poster_hash` varchar(8) DEFAULT NULL, + `poster_country` varchar(2) DEFAULT NULL, + `exif` text DEFAULT NULL, + PRIMARY KEY (`doc_id`), + UNIQUE KEY `num_subnum_index` (`num`,`subnum`), + KEY `thread_num_subnum_index` (`thread_num`,`num`,`subnum`), + KEY `subnum_index` (`subnum`), + KEY `op_index` (`op`), + KEY `media_id_index` (`media_id`), + KEY `media_hash_index` (`media_hash`), + KEY `media_orig_index` (`media_orig`), + KEY `name_trip_index` (`name`,`trip`), + KEY `trip_index` (`trip`), + KEY `email_index` (`email`), + KEY `poster_ip_index` (`poster_ip`), + KEY `timestamp_index` (`timestamp`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `%%BOARD%%_images` ( + `media_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `media_hash` varchar(25) NOT NULL, + `media` varchar(23) DEFAULT NULL, + `preview_op` varchar(23) DEFAULT NULL, + `preview_reply` varchar(23) DEFAULT NULL, + `total` int(10) unsigned NOT NULL DEFAULT 0, + `banned` smallint(5) unsigned NOT NULL DEFAULT 0, + PRIMARY KEY (`media_id`), + UNIQUE KEY `media_hash_index` (`media_hash`), + KEY `total_index` (`total`), + KEY `banned_index` (`banned`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `%%BOARD%%_threads` ( + `thread_num` int(10) unsigned NOT NULL, + `time_op` int(10) unsigned NOT NULL, + `time_last` int(10) unsigned NOT NULL, + `time_bump` int(10) unsigned NOT NULL, + `time_ghost` int(10) unsigned DEFAULT NULL, + `time_ghost_bump` int(10) unsigned DEFAULT NULL, + `time_last_modified` int(10) unsigned NOT NULL, + `nreplies` int(10) unsigned NOT NULL DEFAULT 0, + `nimages` int(10) unsigned NOT NULL DEFAULT 0, + `sticky` tinyint(1) NOT NULL DEFAULT 0, + `locked` tinyint(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`thread_num`), + KEY `time_op_index` (`time_op`), + KEY `time_bump_index` (`time_bump`), + KEY `time_ghost_bump_index` (`time_ghost_bump`), + KEY `time_last_modified_index` (`time_last_modified`), + KEY `sticky_index` (`sticky`), + KEY `locked_index` (`locked`), + KEY `sticky_time_bump_index` (`sticky`,`time_bump`), + KEY `sticky_time_ghost_bump_index` (`sticky`,`time_ghost_bump`), + KEY `sticky_thread_num_index` (`sticky`,`thread_num`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/scripts/create_triggers.template.sql b/scripts/create_triggers.template.sql new file mode 100644 index 0000000..6615b51 --- /dev/null +++ b/scripts/create_triggers.template.sql @@ -0,0 +1,106 @@ +/* + * create_triggers.template.sql + * Create triggers for Neofuuka. + * + * $ cp -v "modify_columns.template.sql" "modify_columns.${board}.sql" + * $ sed -i "s/%%BOARD%%/${board}/g" "modify_columns.${board}.sql" + * + * $ mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" \ + * < "modify_columns.${board}.sql" + */ +DELIMITER ;; + +DROP PROCEDURE IF EXISTS `update_thread_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `create_thread_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `delete_thread_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `insert_image_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `delete_image_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `insert_post_%%BOARD%%`;; +DROP PROCEDURE IF EXISTS `delete_post_%%BOARD%%`;; + +DROP TRIGGER IF EXISTS `before_ins_%%BOARD%%`;; +DROP TRIGGER IF EXISTS `after_ins_%%BOARD%%`;; +DROP TRIGGER IF EXISTS `after_del_%%BOARD%%`;; + +CREATE PROCEDURE `update_thread_%%BOARD%%` (ins INT, tnum INT, subnum INT, timestamp INT, media INT, email VARCHAR(100)) +BEGIN + UPDATE + `%%BOARD%%_threads` op + SET + op.time_last = IF((ins AND subnum = 0), GREATEST(timestamp, op.time_last), op.time_last), + op.time_bump = IF((ins AND subnum = 0), GREATEST(timestamp, op.time_bump), op.time_bump), + op.time_ghost = IF((ins AND subnum != 0), GREATEST(timestamp, COALESCE(op.time_ghost, 0)), op.time_ghost), + op.time_ghost_bump = IF((ins AND subnum != 0 AND (email IS NULL OR email != 'sage')), GREATEST(timestamp, COALESCE(op.time_ghost_bump, 0)), op.time_ghost_bump), + op.time_last_modified = GREATEST(timestamp, op.time_last_modified), + op.nreplies = IF(ins, (op.nreplies + 1), (op.nreplies - 1)), + op.nimages = IF(media, IF(ins, (op.nimages + 1), (op.nimages - 1)), op.nimages) + WHERE op.thread_num = tnum; +END;; + +CREATE PROCEDURE `create_thread_%%BOARD%%` (num INT, timestamp INT) +BEGIN + INSERT IGNORE INTO `%%BOARD%%_threads` VALUES (num, timestamp, timestamp, timestamp, NULL, NULL, timestamp, 0, 0, 0, 0); +END;; + +CREATE PROCEDURE `delete_thread_%%BOARD%%` (tnum INT) +BEGIN + DELETE FROM `%%BOARD%%_threads` WHERE thread_num = tnum; +END;; + +CREATE PROCEDURE `insert_image_%%BOARD%%` (n_media_hash VARCHAR(25), n_media varchar(23), n_preview varchar(23), n_op INT) +BEGIN + IF n_op = 1 THEN + INSERT INTO `%%BOARD%%_images` (media_hash, media, preview_op, total) + VALUES (n_media_hash, n_media, n_preview, 1) + ON DUPLICATE KEY UPDATE + media_id = LAST_INSERT_ID(media_id), + total = (total + 1), + preview_op = COALESCE(preview_op, VALUES(preview_op)), + media = COALESCE(media, VALUES(media)); + ELSE + INSERT INTO `%%BOARD%%_images` (media_hash, media, preview_reply, total) + VALUES (n_media_hash, n_media, n_preview, 1) + ON DUPLICATE KEY UPDATE + media_id = LAST_INSERT_ID(media_id), + total = (total + 1), + preview_reply = COALESCE(preview_reply, VALUES(preview_reply)), + media = COALESCE(media, VALUES(media)); + END IF; +END;; + +CREATE PROCEDURE `delete_image_%%BOARD%%` (n_media_id INT) +BEGIN + UPDATE `%%BOARD%%_images` SET total = (total - 1) WHERE media_id = n_media_id; +END;; + +CREATE TRIGGER `before_ins_%%BOARD%%` BEFORE INSERT ON `%%BOARD%%` +FOR EACH ROW +BEGIN + IF NEW.media_hash IS NOT NULL THEN + CALL insert_image_%%BOARD%%(NEW.media_hash, NEW.media_orig, NEW.preview_orig, NEW.op); + SET NEW.media_id = LAST_INSERT_ID(); + END IF; +END;; + +CREATE TRIGGER `after_ins_%%BOARD%%` AFTER INSERT ON `%%BOARD%%` +FOR EACH ROW +BEGIN + IF NEW.op = 1 THEN + CALL create_thread_%%BOARD%%(NEW.num, NEW.timestamp); + END IF; + CALL update_thread_%%BOARD%%(1, NEW.thread_num, NEW.subnum, NEW.timestamp, NEW.media_id, NEW.email); +END;; + +CREATE TRIGGER `after_del_%%BOARD%%` AFTER DELETE ON `%%BOARD%%` +FOR EACH ROW +BEGIN + CALL update_thread_%%BOARD%%(0, OLD.thread_num, OLD.subnum, OLD.timestamp, OLD.media_id, OLD.email); + IF OLD.op = 1 THEN + CALL delete_thread_%%BOARD%%(OLD.num); + END IF; + IF OLD.media_hash IS NOT NULL THEN + CALL delete_image_%%BOARD%%(OLD.media_id); + END IF; +END;; + +DELIMITER ; diff --git a/scripts/modify_columns.template.sql b/scripts/modify_columns.template.sql new file mode 100644 index 0000000..816a58e --- /dev/null +++ b/scripts/modify_columns.template.sql @@ -0,0 +1,25 @@ +/* modify_columns.template.sql + * Modify tables for Neofuuka to be compatable with net 4chan image timestamp values. + * (This change occured during 2022-07.) + * + * $ cp -v "modify_columns.template.sql" "modify_columns.${board}.sql" + * $ sed -i "s/%%BOARD%%/${board}/g" "modify_columns.${board}.sql" + * + * $ mysql --defaults-extra-file="${mariadb_conf}" --database="${mariadb_db}" \ + * < "modify_columns.${board}.sql" + */ + + /* BOARD */ +ALTER TABLE `%%BOARD%%` MODIFY `preview_orig` varchar(23) DEFAULT NULL ; +ALTER TABLE `%%BOARD%%` MODIFY `media_orig` varchar(23) DEFAULT NULL ; + +/* BOARD_deleted */ +ALTER TABLE `%%BOARD%%` MODIFY `preview_orig` varchar(23) DEFAULT NULL ; +ALTER TABLE `%%BOARD%%` MODIFY `media_orig` varchar(23) DEFAULT NULL ; + +/* BOARD_images */ +ALTER TABLE `%%BOARD%%_images` MODIFY `media` VARCHAR(23) DEFAULT NULL ; +ALTER TABLE `%%BOARD%%_images` MODIFY `preview_op` VARCHAR(23) DEFAULT NULL ; +ALTER TABLE `%%BOARD%%_images` MODIFY `preview_reply` VARCHAR(23) DEFAULT NULL ; + +COMMIT ; diff --git a/scripts/root.my.cnf b/scripts/root.my.cnf new file mode 100644 index 0000000..64ba10f --- /dev/null +++ b/scripts/root.my.cnf @@ -0,0 +1,24 @@ +## root.my.cnf +## mariadb config file +## This is just used to safely store connection details including password. + +[client-server] +# Uncomment these if you want to use a nonstandard connection to MariaDB +#socket=/tmp/mysql.sock +#port=3306 + + +## This will be passed to all MariaDB clients +[client] +#host=localhost +user=root +password=PASSWORD_REMOVED + + +# https://mariadb.com/kb/en/mysqldump/ +[mysqldump] +comments=TRUE +tz-utc=TRUE +single-transaction=TRUE +skip-lock-tables=TRUE +