added routine engine

This commit is contained in:
2026-09-05 13:41:26 +02:00
parent 34f7490e94
commit 8f4ac3d53e
9 changed files with 2197 additions and 4 deletions
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
/**
* Aggiunge il flag `engine` alla tabella `routine` esistente (0 = legacy su file,
* 1 = motore generico basato su routine_step) e crea la tabella `routine_step`.
*
* Migrazione puramente additiva: le routine legacy esistenti (id 1..4) restano
* intatte e con engine = 0 continuano a funzionare come prima.
*/
final class AddEngineAndRoutineStep extends AbstractMigration
{
public function change(): void
{
// 1) Flag engine sulla tabella esistente `routine`
$routine = $this->table('routine');
if (!$routine->hasColumn('engine')) {
$routine
->addColumn('engine', 'boolean', [
'default' => 0,
'null' => false,
'comment' => '0 = routine legacy su file, 1 = motore generico (routine_step)',
'after' => 'filename',
])
->update();
}
// 2) Nuova tabella routine_step
if (!$this->hasTable('routine_step')) {
$table = $this->table('routine_step', [
'id' => 'idstep',
'signed' => false,
'engine' => 'InnoDB',
'collation' => 'utf8mb4_general_ci',
]);
$table
->addColumn('idroutine', 'integer', [
'null' => false,
'comment' => 'FK verso routine.idroutine',
])
->addColumn('step_order', 'integer', [
'default' => 1,
'null' => false,
'comment' => 'Ordine di esecuzione dello step nella routine',
])
->addColumn('operation', 'string', [
'limit' => 50,
'null' => false,
'comment' => 'Verbo operazione: merge, replace, trim, copy, clear, uppercase, lowercase',
])
->addColumn('params', 'text', [
'null' => true,
'comment' => 'Parametri operazione in formato JSON',
])
->addColumn('active', 'boolean', [
'default' => 1,
'null' => false,
'comment' => 'Se 0 lo step viene saltato',
])
->addIndex(['idroutine', 'step_order'], ['name' => 'idx_routine_order'])
->create();
}
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
use Phinx\Seed\AbstractSeed;
/**
* Seed dimostrativo del motore generico.
*
* - Assicura che le routine legacy esistenti abbiano engine = 0 (nessun impatto).
* - Crea una routine di esempio "merge_D_F_H_into_J" con engine = 1
* e il relativo step: unisce le colonne D, F, H nella colonna J.
*
* Nota: filename per le routine engine punta al file motore condiviso _engine.php,
* così apply_routine.php lo risolve correttamente anche se il flusso invia il filename.
*/
final class RoutineEngineSeed extends AbstractSeed
{
public function run(): void
{
$pdo = $this->getAdapter()->getConnection();
// 1) Le legacy restano engine = 0 (idempotente).
$pdo->exec("UPDATE routine SET engine = 0 WHERE engine IS NULL");
// 2) Crea/aggiorna la routine engine di esempio.
$name = 'merge_D_F_H_into_J';
$description = 'Unisce le colonne D, F, H nella colonna J durante l\'importazione';
$filename = '_engine.php';
$check = $pdo->prepare("SELECT idroutine FROM routine WHERE name = :name LIMIT 1");
$check->execute([':name' => $name]);
$existing = $check->fetch(PDO::FETCH_ASSOC);
if ($existing) {
$idroutine = (int)$existing['idroutine'];
$upd = $pdo->prepare(
"UPDATE routine
SET description = :descr, filename = :fn, engine = 1, instruction = :instr
WHERE idroutine = :id"
);
$upd->execute([
':descr' => $description,
':fn' => $filename,
':instr' => 'Routine gestita dal motore generico (routine_step).',
':id' => $idroutine,
]);
} else {
$ins = $pdo->prepare(
"INSERT INTO routine (name, description, instruction, filename, engine, headerrow)
VALUES (:name, :descr, :instr, :fn, 1, 1)"
);
$ins->execute([
':name' => $name,
':descr' => $description,
':instr' => 'Routine gestita dal motore generico (routine_step).',
':fn' => $filename,
]);
$idroutine = (int)$pdo->lastInsertId();
}
// 3) Ripulisci eventuali step precedenti di questa routine e reinserisci.
$del = $pdo->prepare("DELETE FROM routine_step WHERE idroutine = :id");
$del->execute([':id' => $idroutine]);
// Step unico: merge D,F,H -> J. Colonne espresse in lettere per leggibilità.
$params = json_encode([
'source_columns' => ['D', 'F', 'H'],
'target_column' => 'J',
'separator' => ' ',
'skip_empty' => true,
], JSON_UNESCAPED_UNICODE);
$insStep = $pdo->prepare(
"INSERT INTO routine_step (idroutine, step_order, operation, params, active)
VALUES (:id, 1, 'merge', :params, 1)"
);
$insStep->execute([':id' => $idroutine, ':params' => $params]);
}
}