Skip to content

Commit

Permalink
FIXUP making row copier
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Wirt authored and Steve Wirt committed Dec 10, 2024
1 parent 3995551 commit 3eaba64
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions modules/harvest/harvest.install
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\harvest\Entity\HarvestRunRepository;
use Drupal\harvest\HarvestUtility;

Expand Down Expand Up @@ -42,8 +43,52 @@ function harvest_requirements($phase): array {
* The ids of all the harvest runs in the table sorted oldest to newest.
*/
function harvest_get_temp_run_ids($table_name_temp) : array {
// @todo get the ids from the temp table sorted oldest to newwest.
return [];
$connection = Database::getConnection();
//- Step one get all referenced nids
$query = $connection->select($table_name_temp, 'hrt')
->fields('hrt', ['id']);
$query->orderBy('id', 'ASC');
$result = $query->execute()->fetchCol(0);
// Can't do orderBy as the sort is natural, not numeric.
asort($result, SORT_NUMERIC);

return $result ?? [];
}

/**
* Reads a single harvest row from the temp table.
*
* @param string $table_name_temp
* Name of the table to read from.
*
* @param string $time_id
* The id to read from, which was also the timestamp.
*
* @return array
* Elements from the row ['id', 'harvest_plan_id', 'data', 'extract_status'].
*/
function harvest_read_harvest_run(string $table_name_temp, string $time_id): array {
$connection = Database::getConnection();
$query = $connection->select($table_name_temp, 'hrt')
->fields('hrt', ['id', 'harvest_plan_id', 'data', 'extract_status'])
->condition('id', $time_id, '=');
// ->where($time_id, 'id', '=');
$query->orderBy('id', 'ASC');
$result = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
return reset($result);
}

function harvest_write_harvest_run(string $id, $harvest_plan_id, string $data, string $extract_status) {
/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
$result = $connection->insert('harvest_runs')
->fields([
'timestamp' => $id,
'harvest_plan_id' => $harvest_plan_id,
'data' => $data,
'extract_status' => $extract_status,
])
->execute();
}

/**
Expand Down Expand Up @@ -163,7 +208,7 @@ function harvest_update_8010(&$sandbox) {
$harvest_run_repository = \Drupal::service('dkan.harvest.storage.harvest_run_repository');
$schema = \Drupal::database()->schema();

if (!is_set($sandbox['total'])) {
if (!isset($sandbox['total'])) {
// Sandbox has not been initiated, so initiate it.
$sandbox['items_to_process'] = harvest_get_temp_run_ids($table_name_temp);
$sandbox['total'] = count($sandbox['items_to_process']);
Expand All @@ -172,10 +217,13 @@ function harvest_update_8010(&$sandbox) {
// Process them in batches of 25.
$harvest_runs_batch = array_slice($sandbox['items_to_process'], 0, 25, TRUE);
// Loop through all the entries in temp table and save them new.
foreach ($harvest_runs_batch as $harvest_run => $fields) {
$harvest_run_repository->storeRun($fields['data'], $fields['harvest_plan_id'], $fields['id']);
foreach ($harvest_runs_batch as $key => $time_id) {
// Load the old row.
$row = harvest_read_harvest_run($table_name_temp, $time_id);

harvest_write_harvest_run($row['id'], $row['harvest_plan_id'], $row['data'], $row['extract_status']);
// The item has been processed, remove it from the array.
unset($sandbox['items_to_process'][$fields['id']]);
unset($sandbox['items_to_process'][$key]);
}

// Determine when to stop batching.
Expand All @@ -187,15 +235,14 @@ function harvest_update_8010(&$sandbox) {
'@total' => $sandbox['total'],
];

$message = t('Processed @element. @completed/@total.', $vars) . PHP_EOL;
$messages = t('Processed @element. @completed/@total.', $vars) . PHP_EOL;
// Log the all finished notice.
if ($sandbox['#finished'] === 1) {
// The update of the harvest_runs is complete.
\Drupal::logger('script_library')->log(LogLevel::INFO, $completed_message, $vars);
$logged_message = new FormattableMarkup($completed_message, $vars);
$messages .= t('Data in harvest_runs updated to new schema:') . " {$logged_message}" . PHP_EOL;
// @todo uncomment this when done testing.
//$dropped = $schema->dropTable($table_name_temp);
$dropped = $schema->dropTable($table_name_temp);
if ($dropped) {
$messages .= t('Temporary table dropped.') . PHP_EOL;
}
Expand Down

0 comments on commit 3eaba64

Please sign in to comment.