ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilManScoringSettingsToOwnDbTableMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Setup;
22 
23 use ILIAS\Setup;
27 use ilDBInterface;
28 use Exception;
30 
37 {
38  private const TABLE_NAME = 'manscoring_done';
39  private const TESTS_PER_STEP = 10000;
40 
41  private ilDBInterface $db;
42 
46  private mixed $io;
47 
48  private function manScoringDoneEntryExists(int $activeId): bool
49  {
50  $result = $this->db->queryF(
51  "SELECT active_id FROM manscoring_done WHERE active_id = %s",
52  ["integer"],
53  [$activeId]
54  );
55 
56  return $result->numRows() === 1;
57  }
58 
59  public function getLabel(): string
60  {
61  return "Migrate manual scoring done setting from ilSettings db table to own table for improved performance";
62  }
63 
65  {
66  return 10;
67  }
68 
69  public function getPreconditions(Environment $environment): array
70  {
71  return [
73  ];
74  }
75 
76  public function prepare(Environment $environment): void
77  {
78  $this->db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
79  $this->io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
80  }
81 
85  public function step(Environment $environment): void
86  {
90  $failed = [];
91 
95  $success = [];
96 
97  $totalCount = 0;
98  $this->db->setLimit(self::TESTS_PER_STEP);
99  $result = $this->db->query(
100  "SELECT keyword, value FROM settings WHERE " . $this->db->like('keyword', 'text', 'manscoring_done_%')
101  );
102  while ($row = $this->db->fetchAssoc($result)) {
103  $totalCount++;
104 
105  $keyword = $row["keyword"];
106  $match = [];
107  if (!preg_match('/manscoring_done_(\d+)$/', $keyword, $match)) {
108  continue;
109  }
110  $activeId = $match[1];
111  if (!is_numeric($activeId)) {
112  continue;
113  }
114 
115  $activeId = (int) $activeId;
116 
117  if ($this->manScoringDoneEntryExists($activeId)) {
118  $failed[$activeId] = "Entry with active_id '$activeId' already exists in table '" . self::TABLE_NAME . "'.";
119  continue;
120  }
121 
122  if ((int) $this->db->manipulateF(
123  "INSERT INTO " . self::TABLE_NAME . " (active_id, done) VALUES (%s, %s)",
124  ["integer", "integer"],
125  [$activeId, (int) $row["value"]]
126  ) !== 1) {
127  $failed[$activeId] = "Error occurred while trying to insert manscoring done status into new table ' " . self::TABLE_NAME . "'.";
128  continue;
129  }
130 
131  if ((int) $this->db->manipulateF(
132  "DELETE FROM settings WHERE keyword = %s",
133  ["text"],
134  [$keyword]
135  ) !== 1) {
136  $failed[$activeId] = "Error occurred while trying to delete manscoring done status '$keyword' from old table 'settings'.";
137  continue;
138  }
139 
140  $success[] = $activeId;
141  }
142 
143  //To get into new line for cleaner error reporting.
144  $this->io->text("");
145  foreach ($failed as $reason) {
146  $this->io->error($reason);
147  }
148 
149  $successCount = count($success);
150  $failedCount = count($failed);
151 
152  $this->io->success(
153  "Successfully migrated $successCount of $totalCount ($failedCount failed) entries " .
154  "from table 'settings' to table '" . self::TABLE_NAME . "'."
155  );
156  }
157 
158  public function getRemainingAmountOfSteps(): int
159  {
160  $result = $this->db->query(
161  "SELECT COUNT(*) AS cnt FROM settings WHERE " . $this->db->like('keyword', 'text', 'manscoring_done_%')
162  );
163  $row = $this->db->fetchAssoc($result);
164 
165  $num_legacy_tests = (int) ($row['cnt'] ?? 0);
166 
167  return (int) ceil($num_legacy_tests / self::TESTS_PER_STEP);
168  }
169 }
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
step(Environment $environment)
Run one step of the migration.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
prepare(Environment $environment)
Prepare the migration by means of some environment.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
getPreconditions(Environment $environment)
Objectives the migration depend on.
string $reason
Error message for last request processed.
Definition: System.php:102