ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
MoveTestSettingsMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Test\Setup;
22
23use ILIAS\Setup;
26
28{
30
31 private \ilDBInterface $db;
32
33 public function getLabel(): string
34 {
35 return 'Migrate test settings from tst_tests to tst_test_settings';
36 }
37
39 {
40 return 100;
41 }
42
43 public function getPreconditions(Environment $environment): array
44 {
45 return [new \ilDatabaseInitializedObjective()];
46 }
47
48 public function prepare(Environment $environment): void
49 {
50 $this->db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
51 }
52
53 public function step(Environment $environment): void
54 {
55 $columns = implode(',', array_keys(self::SETTINGS_COLUMNS));
56 $row = $this->db->fetchAssoc(
57 $this->db->query("SELECT test_id, {$columns} FROM tst_tests WHERE settings_id IS NULL LIMIT 1")
58 );
59
60 $settings_id = $this->db->nextId('tst_test_settings');
61 $setting_data = ['id' => [\ilDBConstants::T_INTEGER, $settings_id]];
62
63 foreach ($row as $column_name => $value) {
64 if (isset(self::SETTINGS_COLUMNS[$column_name])) {
65 [$column_def] = self::SETTINGS_COLUMNS[$column_name];
66
67 if ($column_name === 'reporting_date') {
68 $value = $this->convertLegacyDate($value);
69 }
70
71 // Convert legacy null values to 0
72 if ($column_def['type'] === \ilDBConstants::T_INTEGER && !$this->columnIsNullable($column_def)) {
73 $value = (int) $value;
74 }
75
76 $setting_data[$column_name] = [$column_def['type'], $value];
77 }
78 }
79
80 $this->db->insert('tst_test_settings', $setting_data);
81 $this->db->update(
82 'tst_tests',
83 ['settings_id' => [\ilDBConstants::T_INTEGER, $settings_id]],
84 ['test_id' => [\ilDBConstants::T_INTEGER, $row['test_id']]]
85 );
86 }
87
88 public function getRemainingAmountOfSteps(): int
89 {
90 $result = $this->db->query("SELECT COUNT(test_id) AS cnt FROM tst_tests WHERE settings_id IS NULL");
91 return (int) $this->db->fetchObject($result)->cnt;
92 }
93}
prepare(Environment $environment)
Prepare the migration by means of some environment.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
getPreconditions(Environment $environment)
Objectives the migration depend on.
step(Environment $environment)
Run one step of the migration.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
columnIsNullable(array $column_def)
convertLegacyDate(string|\DateTimeImmutable|null $date)