From 58a689235a13e422395384eb8ae00c2eaa0afce4 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 4 Oct 2014 12:02:58 +0200 Subject: [PATCH] Fixed triggers --- src/Upgrades/Upgrade13.php | 54 ++++++++++++++++++++++++++++++++++++++ src/di.php | 1 + 2 files changed, 55 insertions(+) create mode 100644 src/Upgrades/Upgrade13.php diff --git a/src/Upgrades/Upgrade13.php b/src/Upgrades/Upgrade13.php new file mode 100644 index 00000000..17f740f6 --- /dev/null +++ b/src/Upgrades/Upgrade13.php @@ -0,0 +1,54 @@ +getPDO(); + + $pdo->exec('DROP TRIGGER postTagsDelete'); + $pdo->exec('DROP TRIGGER favoritesDelete'); + $pdo->exec('DROP TRIGGER favoritesInsert'); + $pdo->exec('DROP TRIGGER favoritesUpdate'); + + $pdo->exec(' + CREATE TRIGGER postTagsDelete AFTER DELETE ON postTags + FOR EACH ROW + BEGIN + UPDATE posts SET tagCount = tagCount - 1 WHERE posts.id = OLD.postId; + UPDATE tags SET usages = usages - 1 WHERE tags.id = OLD.tagId; + END'); + + $pdo->exec(' + CREATE TRIGGER favoritesDelete AFTER DELETE ON favorites + FOR EACH ROW + BEGIN + UPDATE posts SET + favCount = (SELECT COUNT(1) FROM favorites WHERE favorites.postId = posts.id), + lastFavTime = (SELECT MAX(time) FROM favorites WHERE favorites.postId = posts.id) + WHERE posts.id = OLD.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER favoritesInsert AFTER INSERT ON favorites + FOR EACH ROW + BEGIN + UPDATE posts SET + favCount = (SELECT COUNT(1) FROM favorites WHERE favorites.postId = posts.id), + lastFavTime = (SELECT MAX(time) FROM favorites WHERE favorites.postId = posts.id) + WHERE posts.id = NEW.postId; + END'); + + $pdo->exec(' + CREATE TRIGGER favoritesUpdate AFTER UPDATE ON favorites + FOR EACH ROW + BEGIN + UPDATE posts SET + favCount = (SELECT COUNT(1) FROM favorites WHERE favorites.postId = posts.id), + lastFavTime = (SELECT MAX(time) FROM favorites WHERE favorites.postId = posts.id) + WHERE posts.id IN (OLD.postId, NEW.postId); + END'); + + } +} diff --git a/src/di.php b/src/di.php index bb835e0c..b898a0c9 100644 --- a/src/di.php +++ b/src/di.php @@ -28,6 +28,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade10::class), $container->get(\Szurubooru\Upgrades\Upgrade11::class), $container->get(\Szurubooru\Upgrades\Upgrade12::class), + $container->get(\Szurubooru\Upgrades\Upgrade13::class), ]; }),