ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ilCourseCleanupActivationMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
26 private \ilDBInterface $db;
27
28 public function getLabel(): string
29 {
30 return "Remove Duplicate Rows In Activation Table";
31 }
32
34 {
35 return 10000;
36 }
37
38 public function getPreconditions(Environment $environment): array
39 {
40 return \ilResourceStorageMigrationHelper::getPreconditions();
41 }
42
43 public function prepare(Environment $environment): void
44 {
45 $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
46 }
47
48 public function step(Environment $environment): void
49 {
50 $activation_data = $this->db->fetchObject(
51 $this->db->query(
52 'SELECT obj_id FROM crs_items' . PHP_EOL
53 . 'GROUP BY obj_id HAVING COUNT(*) > 1 LIMIT 1'
54 )
55 );
56
57 if ($activation_data === null) {
58 return;
59 }
60
61 $parent_data = $this->db->fetchObject(
62 $this->db->queryF(
63 'SELECT parent FROM tree WHERE child=%s',
65 [$activation_data->obj_id]
66 )
67 );
68
69 if ($parent_data === null) {
70 $this->db->manipulateF(
71 'DELETE FROM crs_items WHERE obj_id = %s',
73 [$activation_data->obj_id]
74 );
75 return;
76 }
77
78 $this->db->manipulateF(
79 'DELETE FROM crs_items WHERE obj_id = %s AND parent_id != %s',
81 [$activation_data->obj_id, $parent_data->parent]
82 );
83 }
84
85 public function getRemainingAmountOfSteps(): int
86 {
87 return $this->db->numRows(
88 $this->db->query(
89 'SELECT obj_id, parent_id FROM crs_items GROUP BY obj_id HAVING COUNT(*) > 1'
90 )
91 );
92 }
93}
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
step(Environment $environment)
Run one step of the migration.
prepare(Environment $environment)
Prepare the migration by means of some environment.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
A migration is a potentially long lasting operation that can be broken into discrete steps.
Definition: Migration.php:29