diff --git a/src/Search/Filters/PostFilter.php b/src/Search/Filters/PostFilter.php index 52d9b626..b583ab61 100644 --- a/src/Search/Filters/PostFilter.php +++ b/src/Search/Filters/PostFilter.php @@ -11,7 +11,7 @@ class PostFilter extends BasicFilter implements IFilter const ORDER_SCORE = 'score'; const ORDER_LAST_EDIT_TIME = 'lastEditTime'; const ORDER_FILE_SIZE = 'originalFileSize'; - const ORDER_LAST_COMMENT_TIME = 'lastCommentTime'; + const ORDER_LAST_COMMENT_TIME = 'lastCommentCreationTime'; const ORDER_LAST_FAV_TIME = 'lastFavTime'; const ORDER_LAST_FEATURE_TIME = 'lastFeatureTime'; diff --git a/src/Upgrades/Upgrade41.php b/src/Upgrades/Upgrade41.php index 26583679..91e8a0a8 100644 --- a/src/Upgrades/Upgrade41.php +++ b/src/Upgrades/Upgrade41.php @@ -7,7 +7,6 @@ class Upgrade41 implements IUpgrade public function run(DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $databaseConnection->getDriver(); $pdo->exec(' CREATE TRIGGER postNotesDelete AFTER DELETE ON postNotes diff --git a/src/Upgrades/Upgrade42.php b/src/Upgrades/Upgrade42.php new file mode 100644 index 00000000..7737c2b4 --- /dev/null +++ b/src/Upgrades/Upgrade42.php @@ -0,0 +1,53 @@ +getPDO(); + + $pdo->exec('ALTER TABLE posts CHANGE COLUMN lastCommentTime lastCommentEditTime TIMESTAMP NOT NULL DEFAULT 0'); + $pdo->exec('ALTER TABLE posts ADD COLUMN lastCommentCreationTime TIMESTAMP NOT NULL DEFAULT 0'); + + $pdo->exec('DROP TRIGGER IF EXISTS commentsDelete'); + $pdo->exec('DROP TRIGGER IF EXISTS commentsInsert'); + $pdo->exec('DROP TRIGGER IF EXISTS commentsUpdate'); + + $pdo->exec(' + CREATE TRIGGER commentsDelete AFTER DELETE ON comments + FOR EACH ROW + BEGIN + UPDATE posts SET + commentCount = (SELECT COUNT(1) FROM comments WHERE comments.postId = posts.id), + lastCommentCreationTime = (SELECT MAX(creationTime) FROM comments WHERE comments.postId = posts.id), + lastCommentEditTime = (SELECT MAX(lastEditTime) FROM comments WHERE comments.postId = posts.id) + WHERE posts.id = OLD.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER commentsInsert AFTER INSERT ON comments + FOR EACH ROW + BEGIN + UPDATE posts SET + commentCount = (SELECT COUNT(1) FROM comments WHERE comments.postId = posts.id), + lastCommentCreationTime = (SELECT MAX(creationTime) FROM comments WHERE comments.postId = posts.id), + lastCommentEditTime = (SELECT MAX(lastEditTime) FROM comments WHERE comments.postId = posts.id) + WHERE posts.id = NEW.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER commentsUpdate AFTER UPDATE ON comments + FOR EACH ROW + BEGIN + UPDATE posts SET + commentCount = (SELECT COUNT(1) FROM comments WHERE comments.postId = posts.id), + lastCommentCreationTime = (SELECT MAX(creationTime) FROM comments WHERE comments.postId = posts.id), + lastCommentEditTime = (SELECT MAX(lastEditTime) FROM comments WHERE comments.postId = posts.id) + WHERE posts.id IN (OLD.postId, NEW.postId); + END'); + + $pdo->exec('UPDATE posts SET lastCommentCreationTime = (SELECT MAX(creationTime) FROM comments WHERE comments.postId = posts.id)'); + } +} diff --git a/src/di.php b/src/di.php index ced90405..c367a806 100644 --- a/src/di.php +++ b/src/di.php @@ -58,6 +58,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade39::class), $container->get(\Szurubooru\Upgrades\Upgrade40::class), $container->get(\Szurubooru\Upgrades\Upgrade41::class), + $container->get(\Szurubooru\Upgrades\Upgrade42::class), ]; }),