szurubooru/src/Upgrades/Upgrade06.php
2014-10-18 18:48:27 +02:00

38 lines
1.1 KiB
PHP

<?php
namespace Szurubooru\Upgrades;
class Upgrade06 implements IUpgrade
{
public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
$pdo->exec('ALTER TABLE posts ADD COLUMN tagCount INTEGER NOT NULL DEFAULT 0');
$pdo->exec('
CREATE TRIGGER postTagsDelete BEFORE 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 postTagsInsert AFTER INSERT ON postTags
FOR EACH ROW
BEGIN
UPDATE posts SET tagCount = tagCount + 1 WHERE posts.id = NEW.postId;
UPDATE tags SET usages = usages + 1 WHERE tags.id = NEW.tagId;
END');
$pdo->exec('
CREATE TRIGGER postTagsUpdate AFTER UPDATE ON postTags
FOR EACH ROW
BEGIN
UPDATE posts SET tagCount = tagCount + 1 WHERE posts.id = NEW.postId;
UPDATE posts SET tagCount = tagCount - 1 WHERE posts.id = OLD.postId;
UPDATE tags SET usages = usages + 1 WHERE tags.id = NEW.tagId;
UPDATE tags SET usages = usages - 1 WHERE tags.id = OLD.tagId;
END');
}
}