aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.173_biest5982_fix_resources_objects_level.php
blob: 1f7a2bc04ce309b44d13f2f2c9e951af9e3eb635 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
 * Due a bug introduced in changeset 23768, the level column of the table
 * resouces_objects has not been set correctly. This migration will fix that.
 *
 * @author  Jan-Hendrik Willms
 * @license GPL2 or any later version
 * @since   Stud.IP 3.4
 * @see     https://develop.studip.de/trac/ticket/5982
 */
class Biest5982FixResourcesObjectsLevel extends Migration
{
    public function description()
    {
        return 'Fix incorrect level values of "resouces_objects"; rebuilds correct hierarchy.';
    }
    
    public function up()
    {
        // Get all resources on a specific level. These will be used
        // as parent ids to find the children on the next level.
        $query = "SELECT `resource_id`
                  FROM `resources_objects`
                  WHERE `level` = :level";
        $parent_statement = DBManager::get()->prepare($query);

        // Update the level for all children of a set of parent ids.
        $query = "UPDATE `resources_objects`
                  SET `level` = :level
                  WHERE `parent_id` IN (:ids)";
        $child_statement = DBManager::get()->prepare($query);

        // Loop until the hierarchy has been built.
        $level = 0;
        do {
            // Read parent ids
            $parent_statement->bindValue(':level', $level);
            $parent_statement->execute();
            $parent_ids = $parent_statement->fetchAll(PDO::FETCH_COLUMN);

            // No parents, no children -> we're done.
            if (count($parent_ids) === 0) {
                break;
            }

            // Increase level
            $level = $level + 1;

            // Update level on all children, exit if no children have been
            // found/updated.
            $child_statement->bindValue(':level', $level);
            $child_statement->bindValue(':ids', $parent_ids, StudipPDO::PARAM_ARRAY);
            $updated_rows = $child_statement->execute();
        } while ($updated_rows > 0);
    }
    
    public function down()
    {
        // No, we don't need this.
    }
}