refactor: extract shared code from bin/ scripts and remove legacy scheduler runner
- Extract ModuleManifestLoader for standalone manifest loading (used by module-deactivate and ValidateCommand) - Extract SqlStatementParser, ModuleMigrationRepository, and ModuleMigrationService from bin/module-migrate.php to enforce strict layering (SQL in repository, orchestration in service) - Delete bin/scheduler-run.php (fully superseded by bin/console scheduler:run) - Update docs and test fixtures to reference bin/console Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
75
lib/Repository/Module/ModuleMigrationRepository.php
Normal file
75
lib/Repository/Module/ModuleMigrationRepository.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Module;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
/**
|
||||
* Manages the module_migrations tracking table.
|
||||
*
|
||||
* All SQL for module migration tracking lives here (strict layering).
|
||||
*/
|
||||
final class ModuleMigrationRepository implements ModuleMigrationRepositoryInterface
|
||||
{
|
||||
public function ensureTrackingTable(): void
|
||||
{
|
||||
DB::query(
|
||||
'CREATE TABLE IF NOT EXISTS `module_migrations` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`module_id` VARCHAR(64) NOT NULL,
|
||||
`filename` VARCHAR(255) NOT NULL,
|
||||
`applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uq_module_migration` (`module_id`, `filename`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, true> Filenames already applied, keyed by filename
|
||||
*/
|
||||
public function getAppliedFilenames(string $moduleId): array
|
||||
{
|
||||
$applied = [];
|
||||
$rows = DB::select('SELECT filename FROM module_migrations WHERE module_id = ?', $moduleId);
|
||||
foreach ((array) $rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$filename = trim((string) (($row['module_migrations']['filename'] ?? $row['filename'] ?? '')));
|
||||
if ($filename !== '') {
|
||||
$applied[$filename] = true;
|
||||
}
|
||||
}
|
||||
return $applied;
|
||||
}
|
||||
|
||||
public function recordApplied(string $moduleId, string $filename): void
|
||||
{
|
||||
DB::query(
|
||||
'INSERT INTO module_migrations (module_id, filename) VALUES (?, ?)',
|
||||
$moduleId,
|
||||
$filename
|
||||
);
|
||||
}
|
||||
|
||||
public function beginTransaction(): void
|
||||
{
|
||||
DB::query('START TRANSACTION');
|
||||
}
|
||||
|
||||
public function commit(): void
|
||||
{
|
||||
DB::query('COMMIT');
|
||||
}
|
||||
|
||||
public function rollback(): void
|
||||
{
|
||||
DB::query('ROLLBACK');
|
||||
}
|
||||
|
||||
public function executeStatement(string $sql): void
|
||||
{
|
||||
DB::query($sql);
|
||||
}
|
||||
}
|
||||
24
lib/Repository/Module/ModuleMigrationRepositoryInterface.php
Normal file
24
lib/Repository/Module/ModuleMigrationRepositoryInterface.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Module;
|
||||
|
||||
/** Contract for module migration tracking: table creation, applied-file queries, and statement execution. */
|
||||
interface ModuleMigrationRepositoryInterface
|
||||
{
|
||||
public function ensureTrackingTable(): void;
|
||||
|
||||
/**
|
||||
* @return array<string, true>
|
||||
*/
|
||||
public function getAppliedFilenames(string $moduleId): array;
|
||||
|
||||
public function recordApplied(string $moduleId, string $filename): void;
|
||||
|
||||
public function beginTransaction(): void;
|
||||
|
||||
public function commit(): void;
|
||||
|
||||
public function rollback(): void;
|
||||
|
||||
public function executeStatement(string $sql): void;
|
||||
}
|
||||
Reference in New Issue
Block a user