ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
MoveSettingsTemplatesMigration.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 personal test template settings from tst_test_defaults 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 $row = $this->db->fetchAssoc(
56 $this->db->query("SELECT * FROM tst_test_defaults WHERE settings_id IS NULL LIMIT 1")
57 );
58
59 $settings_id = $this->db->nextId('tst_test_settings');
60 $setting_data = ['id' => [\ilDBConstants::T_INTEGER, $settings_id]];
61
62 // Migrate the legacy serialized column to a row in 'tst_test_settings'
63 $raw_settings = unserialize($row['defaults'], ['allowed_classes' => [\DateTimeImmutable::class]]);
64 foreach (self::SETTINGS_COLUMNS as $column_name => $column) {
65 [$column_def, $raw_name] = $column;
66 if (isset($raw_settings[$raw_name])) {
67 $value = $raw_settings[$raw_name];
68
69 if ($column_name === 'reporting_date') {
70 $value = $this->convertLegacyDate($value);
71 }
72
73 $setting_data[$column_name] = [$column_def['type'], $value];
74 }
75 }
76
77 // Insert the new row
78 $this->db->insert('tst_test_settings', $setting_data);
79 $this->db->update(
80 'tst_test_defaults',
81 ['settings_id' => [\ilDBConstants::T_INTEGER, $settings_id]],
82 ['test_defaults_id' => [\ilDBConstants::T_INTEGER, $row['test_defaults_id']]]
83 );
84
85 // Migrate the legacy json decoded to a row in 'tst_mark'
86 $raw_marks = json_decode($row['marks'], true);
87 foreach ($raw_marks as $mark_data) {
88 $mark_id = $this->db->nextId('tst_mark');
89 $this->db->insert(
90 'tst_mark',
91 [
92 'mark_id' => [\ilDBConstants::T_INTEGER, $mark_id],
93 'test_fi' => [\ilDBConstants::T_INTEGER, 0],
94 'short_name' => [\ilDBConstants::T_TEXT, $mark_data['short_name']],
95 'official_name' => [\ilDBConstants::T_TEXT, $mark_data['official_name']],
96 'minimum_level' => [\ilDBConstants::T_FLOAT, $mark_data['minimum_level']],
97 'passed' => [\ilDBConstants::T_FLOAT, (int) $mark_data['passed']],
98 'tstamp' => [\ilDBConstants::T_INTEGER, $row['tstamp']],
99 ]
100 );
101
102 $this->db->insert(
103 'tst_defaults_marks',
104 [
105 'defaults_id' => [\ilDBConstants::T_INTEGER, $row['test_defaults_id']],
106 'mark_id' => [\ilDBConstants::T_INTEGER, $mark_id],
107 ]
108 );
109 }
110 }
111
113 {
114 $result = $this->db->query("SELECT COUNT(test_defaults_id) AS cnt FROM tst_test_defaults WHERE settings_id IS NULL");
115 return (int) $this->db->fetchObject($result)->cnt;
116 }
117}
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.
prepare(Environment $environment)
Prepare the migration by means of some environment.
step(Environment $environment)
Run one step 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...
convertLegacyDate(string|\DateTimeImmutable|null $date)