MigrationMoodle: Don't use json file as log - refs BT#15992

pull/3127/head
Angel Fernando Quiroz Campos 6 years ago
parent d133e7ed96
commit a52b7dd5cf
  1. 33
      plugin/migrationmoodle/install.php
  2. 0
      plugin/migrationmoodle/map/.gitkeep
  3. 36
      plugin/migrationmoodle/src/Task/BaseTask.php
  4. 35
      plugin/migrationmoodle/src/Traits/MapTrait.php
  5. 33
      plugin/migrationmoodle/src/Transformer/Property/LoadedKeyLookup.php
  6. 14
      plugin/migrationmoodle/uninstall.php

@ -10,6 +10,8 @@ try {
$plugin->get_lang('MoodlePassword'),
''
);
createPluginTables();
} catch (Exception $exception) {
$message = sprintf(
$plugin->get_lang('InstallError'),
@ -18,3 +20,34 @@ try {
echo Display::return_message($message, 'error');
}
/**
* Create database tables for this plugin.
*/
function createPluginTables()
{
$queries = [];
$queries[] = "CREATE TABLE IF NOT EXISTS plugin_migrationmoodle_task (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB";
$queries[] = "CREATE TABLE IF NOT EXISTS plugin_migrationmoodle_item (
id INT AUTO_INCREMENT NOT NULL,
task_id INT NOT NULL,
hash VARCHAR(255) NOT NULL,
extracted_id INT NOT NULL,
loaded_id INT NOT NULL,
INDEX IDX_HASH (hash),
INDEX IDX_EXTRACTED_LOADED (extracted_id, loaded_id),
INDEX IDX_LOADED (loaded_id),
INDEX IDX_TASK (task_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB";
$queries[] = "ALTER TABLE plugin_migrationmoodle_item ADD CONSTRAINT FK_TASK FOREIGN KEY (task_id)
REFERENCES plugin_migrationmoodle_task (id) ON DELETE CASCADE";
foreach ($queries as $query) {
Database::query($query);
}
}

@ -16,6 +16,10 @@ abstract class BaseTask
{
use MapTrait;
/**
* @var int
*/
protected $taskId;
/**
* @var ExtractorInterface
*/
@ -104,12 +108,18 @@ abstract class BaseTask
*/
private function initMapLog()
{
$filePath = $this->getMapFilePath();
$dirPath = dirname($filePath);
$taskName = $this->getTaskName();
$fileSystem = new Filesystem();
$fileSystem->mkdir($dirPath);
$fileSystem->touch($filePath);
$id = \Database::insert(
'plugin_migrationmoodle_task',
['name' => $taskName]
);
if (empty($id)) {
throw new \Exception("Failed to save task ($taskName).");
}
$this->taskId = $id;
}
/**
@ -143,13 +153,15 @@ abstract class BaseTask
{
$hash = md5("$extractedId@@$loadedId");
$filePath = $this->getMapFilePath();
$mapLog = $this->parseMapFile();
$mapLog[] = ['hash' => $hash, 'extracted' => $extractedId, 'loaded' => $loadedId];
$fileSystem = new Filesystem();
$fileSystem->dumpFile($filePath, json_encode($mapLog));
\Database::insert(
'plugin_migrationmoodle_item',
[
'hash' => $hash,
'extracted_id' => $extractedId,
'loaded_id' => $loadedId,
'task_id' => $this->taskId,
]
);
return $hash;
}

@ -13,43 +13,10 @@ trait MapTrait
/**
* @return string
*/
private function getMapFileName()
private function getTaskName()
{
$name = substr(strrchr($this->calledClass, '\\'), 1);
return api_camel_case_to_underscore($name);
}
/**
* @return string
*/
private function getMapFilePath()
{
$name = $this->getMapFileName();
$dirPath = __DIR__.'/../../map';
return "$dirPath/$name.json";
}
/**
* @throws \Exception
*
* @return array
*/
private function parseMapFile()
{
$filePath = $this->getMapFilePath();
$contents = @file_get_contents($filePath);
if (false === $contents) {
throw new \Exception("Failed to read $filePath file.");
}
/** @var array $mapLog */
$mapLog = json_decode($contents, true);
return $mapLog ?: [];
}
}

@ -26,32 +26,37 @@ abstract class LoadedKeyLookup implements TransformPropertyInterface
{
$id = current($data);
$mapLog = $this->parseMapFile();
$migration = $this->search($id);
$migration = $this->search($mapLog, $id);
return $migration['loaded'];
return $migration['loaded_id'];
}
/**
* @param array $mapLog
* @param int $searchedId
* @param int $searchedId
*
* @throws \Exception
*
* @return array
*/
private function search(array $mapLog, $searchedId)
private function search($searchedId)
{
if (empty($searchedId) || empty($mapLog)) {
if (empty($searchedId)) {
return null;
}
$filtered = array_filter(
$mapLog,
function (array $item) use ($searchedId) {
return $item['extracted'] == $searchedId;
}
$taskName = $this->getTaskName();
$itemInfo = \Database::select(
'i.*',
'plugin_migrationmoodle_item i INNER JOIN plugin_migrationmoodle_task t ON i.task_id = t.id',
[
'where' => [
't.name = ? AND i.extracted_id = ?' => [$taskName, $searchedId],
],
],
'first'
);
return current($filtered) ?: null;
return $itemInfo ?: null;
}
}

@ -42,3 +42,17 @@ function removeExtraField()
$em->remove($extraField);
$em->flush();
}
/**
* Drop database table created by this plugin.
*/
function removePluginTables()
{
$queries = [];
$queries[] = "DROP TABLE IF EXISTS plugin_migrationmoodle_item";
$queries[] = "DROP TABLE IF EXISTS plugin_migrationmoodle_task";
foreach ($queries as $query) {
Database::query($query);
}
}

Loading…
Cancel
Save