misuzu/database/2018_07_25_194106_add_global_comments_stuff.php

111 lines
4.5 KiB
PHP
Raw Normal View History

2018-07-26 18:24:16 +00:00
<?php
namespace Misuzu\DatabaseMigrations\AddGlobalCommentsStuff;
use PDO;
function migrate_up(PDO $conn): void
{
$conn->exec('
CREATE TABLE `msz_comments_categories` (
`category_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`category_name` VARCHAR(255) NOT NULL,
`category_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`category_locked` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`category_id`),
UNIQUE INDEX `comments_categories_name_unique` (`category_name`)
);
');
$conn->exec('
CREATE TABLE `msz_comments_posts` (
`comment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`category_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`comment_reply_to` INT(10) UNSIGNED NULL DEFAULT NULL,
`comment_text` TEXT NOT NULL,
`comment_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
2018-08-06 22:19:35 +00:00
`comment_pinned` TIMESTAMP NULL DEFAULT NULL,
2018-07-26 18:24:16 +00:00
`comment_edited` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`comment_deleted` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`comment_id`),
INDEX `comments_posts_category_foreign` (`category_id`),
INDEX `comments_posts_user_foreign` (`user_id`),
INDEX `comments_posts_reply_id` (`comment_reply_to`),
INDEX `comments_posts_dates` (
`comment_created`,
2018-08-06 22:19:35 +00:00
`comment_pinned`,
2018-07-26 18:24:16 +00:00
`comment_edited`,
`comment_deleted`
),
CONSTRAINT `comments_posts_category_foreign`
FOREIGN KEY (`category_id`)
REFERENCES `msz_comments_categories` (`category_id`)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT `comments_posts_user_foreign`
FOREIGN KEY (`user_id`)
REFERENCES `msz_users` (`user_id`)
ON UPDATE CASCADE
ON DELETE SET NULL
);
');
2018-08-06 22:19:35 +00:00
$conn->exec("
CREATE TABLE `msz_comments_votes` (
`comment_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`comment_vote` ENUM('Like','Dislike') NULL,
UNIQUE INDEX `comments_vote_unique` (`comment_id`, `user_id`),
INDEX `comments_vote_user_foreign` (`user_id`),
INDEX `comments_vote_index` (`comment_vote`),
CONSTRAINT `comment_vote_id`
FOREIGN KEY (`comment_id`)
REFERENCES `msz_comments_posts` (`comment_id`)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT `comment_vote_user`
FOREIGN KEY (`user_id`)
REFERENCES `msz_users` (`user_id`)
ON UPDATE CASCADE
ON DELETE CASCADE
);
");
$conn->exec("
ALTER TABLE `msz_news_posts`
ADD COLUMN `comment_section_id` INT UNSIGNED NULL DEFAULT NULL AFTER `deleted_at`,
ADD INDEX `news_posts_comment_section` (`comment_section_id`),
ADD CONSTRAINT `news_posts_comment_section`
FOREIGN KEY (`comment_section_id`)
REFERENCES `msz_comments_categories` (`category_id`)
ON UPDATE CASCADE
ON DELETE SET NULL;
");
// create a comment section for all news posts
$getNews = $conn->query('SELECT `post_id` FROM `msz_news_posts` WHERE `comment_section_id` IS NULL')
->fetchAll(PDO::FETCH_ASSOC);
$setNews = $conn->prepare('UPDATE `msz_news_posts` SET `comment_section_id` = :c WHERE `post_id` = :p');
foreach ($getNews as $post) {
$info = comments_category_create("news-{$post['post_id']}");
$setNews->execute([
'p' => $post['post_id'],
'c' => $info['category_id'],
]);
}
2018-07-26 18:24:16 +00:00
}
function migrate_down(PDO $conn): void
{
$conn->exec('
ALTER TABLE `msz_news_posts`
DROP COLUMN `comment_section_id`,
DROP INDEX `news_posts_comment_section`,
DROP FOREIGN KEY `news_posts_comment_section`;
');
2018-08-06 22:19:35 +00:00
$conn->exec('DROP TABLE `msz_comments_votes`');
2018-07-26 18:24:16 +00:00
$conn->exec('DROP TABLE `msz_comments_posts`');
$conn->exec('DROP TABLE `msz_comments_categories`');
}