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
62
63
64
65
66
67
68
69
70
71
|
<?php
return new class extends Migration
{
public function description()
{
return 'Introduces numeric IDs and adds the path to the current node to all sem_tree and range_tree entries.';
}
public function up()
{
// Add the new database column for storing node ancestry path.
DBManager::get()->exec("ALTER TABLE `sem_tree`
ADD IF NOT EXISTS `ancestors` VARCHAR(255) NOT NULL AFTER `parent_id`,
ADD INDEX IF NOT EXISTS `ancestors` (`ancestors`)"
);
StudipStudyArea::expireTableScheme();
$this->buildStructure(StudipStudyArea::class, 'root', 0, 0, '');
DBManager::get()->exec("ALTER TABLE `range_tree`
ADD `ancestors` VARCHAR(255) NOT NULL AFTER `parent_id`,
ADD INDEX IF NOT EXISTS `ancestors` (`ancestors`)"
);
RangeTreeNode::expireTableScheme();
$this->buildStructure(RangeTreeNode::class, 'root', 0, 0, '');
DBManager::get()->exec("ALTER TABLE `sem_tree`
CHANGE `sem_tree_id` `sem_tree_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
CHANGE `parent_id` `parent_id` INT UNSIGNED NOT NULL");
DBManager::get()->exec("ALTER TABLE `seminar_sem_tree`
CHANGE `sem_tree_id` `sem_tree_id` INT UNSIGNED NOT NULL");
DBManager::get()->exec("ALTER TABLE `range_tree`
CHANGE `item_id` `item_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
CHANGE `parent_id` `parent_id` INT UNSIGNED NOT NULL");
}
public function down()
{
DBManager::get()->exec("ALTER TABLE `sem_tree` DROP `ancestors`");
DBManager::get()->exec("ALTER TABLE `range_tree` DROP `ancestors`");
}
private function buildStructure(string $classname, string $oldParentId, int $newParentId, int $currentId, string $ancestors)
{
foreach ($classname::findByParent_id($oldParentId, "ORDER BY `priority`") as $child) {
$currentId++;
$newAncestors = $ancestors;
if ($ancestors !== '') {
$newAncestors .= '|';
}
$newAncestors .= $newParentId;
$oldId = $child->id;
$child->id = $currentId;
$child->parent_id = $newParentId;
$child->ancestors = $newAncestors;
$child->store();
DBManager::get()->execute(
"UPDATE `seminar_sem_tree` SET `sem_tree_id` = :new WHERE `sem_tree_id` = :old",
['new' => $child->id, 'old' => $oldId]
);
$currentId = $this->buildStructure($classname, $oldId, $child->id, $currentId, $newAncestors);
}
return $currentId;
}
};
|