Skip to content

Commit

Permalink
Fix command line printer and verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Nov 7, 2025
1 parent e2bd227 commit 1390066
Show file tree
Hide file tree
Showing 9 changed files with 1,107 additions and 593 deletions.
12 changes: 6 additions & 6 deletions app/plugins/Transmogrify/config/schema/tables.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"servers": {
"source": "cm_servers",
"displayField": "description",
"addChangelog": false,
"addChangelog": true,
"cache": ["co_id", "status"],
"fieldMap": {
"plugin": "&mapServerTypeToPlugin",
Expand All @@ -111,7 +111,7 @@
"http_servers": {
"source": "cm_http_servers",
"displayField": "serverurl",
"addChangelog": false,
"addChangelog": true,
"booleans": [
"ssl_verify_host",
"ssl_verify_peer"
Expand All @@ -126,7 +126,7 @@
"oauth2_servers": {
"source": "cm_oauth2_servers",
"displayField": "serverurl",
"addChangelog": false,
"addChangelog": true,
"cache": ["server_id"],
"booleans": ["access_token_exp"],
"fieldMap": {
Expand All @@ -138,15 +138,15 @@
"source": "cm_sql_servers",
"displayField": "hostname",
"cache": ["server_id"],
"addChangelog": false,
"addChangelog": true,
"fieldMap": {
"dbport": "port"
}
},
"match_servers": {
"source": "cm_match_servers",
"displayField": "username",
"addChangelog": false,
"addChangelog": true,
"booleans": [
"ssl_verify_peer",
"ssl_verify_host"
Expand All @@ -162,7 +162,7 @@
"match_server_attributes": {
"source": "cm_match_server_attributes",
"displayField": "attribute",
"addChangelog": false,
"addChangelog": true,
"booleans": [
"required"
],
Expand Down
66 changes: 35 additions & 31 deletions app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class TransmogrifyCommand extends BaseCommand {
protected ?Arguments $args = null;
protected ?ConsoleIo $io = null;

protected ?CommandLinePrinter $cmdPrinter = null;

/** @var string Absolute path to plugin root directory */
private string $pluginRoot;

Expand Down Expand Up @@ -189,8 +191,6 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
'boolean' => true
]);



$parser->setEpilog(__d('command', 'tm.epilog'));

return $parser;
Expand All @@ -210,6 +210,12 @@ public function execute(Arguments $args, ConsoleIo $io): int
$this->args = $args;
$this->io = $io;

// Info is forced to be white
$io->setStyle('info', ['text' => '0;39']);

// Now that BaseCommand set verbosity, construct the printer so it can detect it correctly
$this->cmdPrinter = new CommandLinePrinter($io, 'green', 50, true);

// Validate "info" option combinations and handle errors
$code = $this->validateInfoOptions($io);
if ($code !== null) {
Expand Down Expand Up @@ -274,7 +280,7 @@ public function execute(Arguments $args, ConsoleIo $io): int
$outboundQualifiedTableName = $this->outconn->qualifyTableName($t);
$Model = TableRegistry::getTableLocator()->get($t);

$io->info(message: sprintf("Transmogrifying table %s(%s)", Inflector::classify($t), $t));
$this->cmdPrinter->info(message: sprintf("Transmogrifying table: %s(%s)", Inflector::classify($t), $t));


/*
Expand All @@ -293,7 +299,7 @@ public function execute(Arguments $args, ConsoleIo $io): int
// Skip a table if already contains data
if($Model->find()->count() > 0) {
$outboundTableEmpty = false;
$io->warning("Table (" . $t . ") is not empty. We will not overwrite existing data.");
$this->cmdPrinter->warning("Table (" . $t . ") is not empty. We will not overwrite existing data.");
}

// Skip tables not in the selected subset if specified
Expand All @@ -304,7 +310,7 @@ public function execute(Arguments $args, ConsoleIo $io): int
&& !in_array($t, $selected)
) {
$skipTableTransmogrification = true;
$io->warning("Skipping Transmogrification. Table ($t) is not in the selected subset.");
$this->cmdPrinter->warning("Skipping Transmogrification. Table ($t) is not in the selected subset.");
}

// Mark the table as skipped if it is not empty and not selected
Expand All @@ -321,9 +327,9 @@ public function execute(Arguments $args, ConsoleIo $io): int
$this->outconn,
$this->tables[$t]['source'],
$t,
$this->io
$this->cmdPrinter
)) {
$io->warning("Skipping Transmogrification. Can not properly configure the Sequence for the primary key for the Table (\"$t\"");
$this->cmdPrinter->warning("Skipping Transmogrification. Can not properly configure the Sequence for the primary key for the Table (\"$t\"");
return BaseCommand::CODE_ERROR;
}

Expand All @@ -337,29 +343,28 @@ public function execute(Arguments $args, ConsoleIo $io): int
};

// Verbose message to show the SQL query being executed
$io->verbose(sprintf('[Inbound SQL] Table=%s | %s', $t, $insql));
$this->cmdPrinter->verbose(sprintf('[Inbound SQL] Table=%s | %s', $t, $insql));
// Fetch the inbound data.
$stmt = $this->inconn->executeQuery($insql);

/*
* PROGRESS STARTING
**/
$progress = new CommandLinePrinter($io, 'green', 50, true);
// If a custom SELECT is used, count the exact result set; otherwise count the whole table
if (!empty($this->tables[$t]['sqlSelect'])) {
$countSql = RawSqlQueries::buildCountFromSelect($insql);
$count = (int)$this->inconn->fetchOne($countSql);
} else {
$count = (int)$this->inconn->fetchOne(RawSqlQueries::buildCountAll($inboundQualifiedTableName));
}
$progress->start($count);
$this->cmdPrinter->start($count);
$tally = 0;
$warns = 0;
$err = 0;

while ($row = $stmt->fetchAssociative()) {
if(!empty($row[ $this->tables[$t]['displayField'] ])) {
$io->verbose("$t " . $row[ $this->tables[$t]['displayField'] ]);
$this->cmdPrinter->verbose("$t " . $row[ $this->tables[$t]['displayField'] ]);
}

try {
Expand Down Expand Up @@ -388,7 +393,7 @@ public function execute(Arguments $args, ConsoleIo $io): int
// Insert the transformed row into the target database
if($this->cache['skipInsert'][$outboundQualifiedTableName] === false) {
// Check if a parent record for this row was previously rejected; if so, skip this insert
if ($this->skipIfRejectedParent(currentTable: $t, row: $row, progress: $progress)) {
if ($this->skipIfRejectedParent(currentTable: $t, row: $row)) {
continue;
}

Expand All @@ -406,50 +411,50 @@ public function execute(Arguments $args, ConsoleIo $io): int
// not linked to a CO Person that was not migrated.
$warns++;
$this->cache['rejected'][$outboundQualifiedTableName][$row['id']] = $row;
$progress->warn("Skipping $t record " . $row['id'] . " due to invalid foreign key: " . $e->getMessage());
$progress->ask('Press <enter> to continue...');
$this->cmdPrinter->warn("Skipping $t record " . $row['id'] . " due to invalid foreign key: " . $e->getMessage());
// $this->cmdPrinter->pause();
}
catch(\InvalidArgumentException $e) {
// If we can't find a value for mapping we skip the record
// (ie: mapLegacyFieldNames basically requires a successful mapping)
$warns++;
$this->cache['rejected'][$outboundQualifiedTableName][$row['id']] = $row;
$progress->warn("Skipping $t record " . $row['id'] . ": " . $e->getMessage());
$progress->ask('Press <enter> to continue...');
$this->cmdPrinter->warn("Skipping $t record " . $row['id'] . ": " . $e->getMessage());
// $this->cmdPrinter->pause();
}
catch(\Exception $e) {
$err++;
$progress->error("$t record " . $row['id'] . ": " . $e->getMessage());
$progress->ask('Press <enter> to continue...');
$this->cmdPrinter->error("$t record " . $row['id'] . ": " . $e->getMessage());
// $this->cmdPrinter->pause();
}

$tally++;

if (!$this->args->getOption('quiet') && !$this->args->getOption('verbose')) {
$progress->update($tally);
$this->cmdPrinter->update($tally);
}
}

$progress->finish();
$this->cmdPrinter->finish();
/**
* FINISH PROGRESS
*/

// Output final warning and error counts for the table
$io->warning(sprintf('Warnings: %d', $warns));
$io->error(sprintf('Errors: %d', $err));
$this->cmdPrinter->warning(sprintf('Warnings: %d', $warns));
$this->cmdPrinter->error(sprintf('Errors: %d', $err));

// Execute any post-processing hooks for the table
if($this->cache['skipInsert'][$outboundQualifiedTableName] === false) {
$io->info('Running post-table hook for ' . $t);
$this->cmdPrinter->info('Running post-table hook for ' . $t);
$this->runPostTableHook($t);
}

// If user selected a subset, exit as soon as all selected tables are processed
if (!empty($pendingSelected) && isset($pendingSelected[$t])) {
unset($pendingSelected[$t]);
if (empty($pendingSelected)) {
$io->info('All selected tables have been processed. Exiting.');
$this->cmdPrinter->info('All selected tables have been processed. Exiting.');
return BaseCommand::CODE_SUCCESS;
}
}
Expand All @@ -458,12 +463,12 @@ public function execute(Arguments $args, ConsoleIo $io): int
$tables = array_keys($this->tables);
$currentIndex = array_search($t, $tables);
if (isset($tables[$currentIndex + 1])) {
$io->info("Next table to process: " . $tables[$currentIndex + 1]);
$this->cmdPrinter->info("Next table to process: " . $tables[$currentIndex + 1]);
} else {
$io->info("This is the last table to process.");
$this->cmdPrinter->info("This is the last table to process.");
}

$io->ask('Press <enter> to continue...');
$this->cmdPrinter->pause();
}

return BaseCommand::CODE_SUCCESS;
Expand Down Expand Up @@ -654,10 +659,9 @@ protected function tableExists(string $tableName): bool
*
* @param string $currentTable Logical target table name (eg, 'job_history_records')
* @param array $row Row to insert
* @param \Transmogrify\Lib\Util\CommandLinePrinter $progress Progress printer for warnings
* @return bool True if the row should be skipped, false otherwise
*/
private function skipIfRejectedParent(string $currentTable, array $row, \Transmogrify\Lib\Util\CommandLinePrinter $progress): bool
private function skipIfRejectedParent(string $currentTable, array $row): bool
{
if (!isset($this->cache['rejected'])) {
return false;
Expand All @@ -678,7 +682,7 @@ private function skipIfRejectedParent(string $currentTable, array $row, \Transmo
if ($childId !== null) {
$this->cache['rejected'][$qualifiedCurrent][$childId] = $row;
}
$progress->warn(sprintf(
$this->cmdPrinter->warn(sprintf(
'Skipping record %d in table %s - parent %s(%d) was rejected (self-reference)',
(int)($childId ?? 0),
$currentTable,
Expand Down Expand Up @@ -711,7 +715,7 @@ private function skipIfRejectedParent(string $currentTable, array $row, \Transmo
if ($childId !== null) {
$this->cache['rejected'][$qualifiedCurrent][$childId] = $row;
}
$progress->warn(sprintf(
$this->cmdPrinter->warn(sprintf(
'Skipping record %d in table %s - parent %s(%d) was rejected',
(int)($childId ?? 0),
$currentTable,
Expand Down
Loading

0 comments on commit 1390066

Please sign in to comment.