aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.107_step00247_forum_performance.php
blob: 99d5031edba56fa66e9a75217e427ec978888ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

class Step00247ForumPerformance extends Migration
{
    function description()
    {
        return 'some performance improvements for the new forum';
    }

    function up()
    {
        DBManager::get()->exec("ALTER TABLE forum_entries ADD latest_chdate INT(11) AFTER mkdate");

        $db = DBManager::get()->query("SELECT * FROM forum_entries");
        $stmt = DBManager::get()->prepare("SELECT chdate FROM forum_entries
            WHERE lft > ? AND rgt < ? AND seminar_id = ?
            ORDER BY chdate DESC LIMIT 1");
        $stmt_update = DBManager::get()->prepare("UPDATE forum_entries 
            SET latest_chdate = ? WHERE topic_id = ?");

        while ($data = $db->fetch(PDO::FETCH_ASSOC)) {
            $stmt->execute([$data['lft'], $data['rgt'], $data['seminar_id']]);
            $chdate = $stmt->fetchColumn();

            if ($chdate) {
                $stmt_update->execute([$chdate, $data['topic_id']]);
            } else {
                $stmt_update->execute([$data['chdate'], $data['topic_id']]);
            }
        }
    }

    function down()
    {
        DBManager::get()->exec("ALTER TABLE forum_entries DROP latest_chdate");
    }
}