refactor: Extract hardcoded migrations to class property

This commit is contained in:
Roman Kelesidis 2025-06-20 09:23:43 +03:00
commit 30c1038b3e
No known key found for this signature in database
GPG key ID: D8157C4D4C4C6DB4

View file

@ -19,6 +19,10 @@ class MigrationStatus
{ {
private string $migrationTable; private string $migrationTable;
private string $migrationPath; private string $migrationPath;
private array $initialMigrations = [
'20250619000001',
'20250619000002'
];
public function __construct() public function __construct()
{ {
@ -37,7 +41,7 @@ class MigrationStatus
// Check if migration table exists using Nette Database Explorer // Check if migration table exists using Nette Database Explorer
$tableExists = $this->checkMigrationTableExists(); $tableExists = $this->checkMigrationTableExists();
$setupStatus = $this->getSetupStatus(); $setupStatus = $this->getSetupStatus();
if (!$tableExists) { if (!$tableExists) {
return [ return [
'table_exists' => false, 'table_exists' => false,
@ -105,7 +109,7 @@ class MigrationStatus
// Check if core TorrentPier tables exist (indicates existing installation) // Check if core TorrentPier tables exist (indicates existing installation)
$coreTablesExist = $this->checkCoreTablesExist(); $coreTablesExist = $this->checkCoreTablesExist();
$migrationTableExists = $this->checkMigrationTableExists(); $migrationTableExists = $this->checkMigrationTableExists();
if (!$coreTablesExist) { if (!$coreTablesExist) {
// Fresh installation // Fresh installation
return [ return [
@ -115,7 +119,7 @@ class MigrationStatus
'action_required' => false 'action_required' => false
]; ];
} }
if (!$migrationTableExists) { if (!$migrationTableExists) {
// Existing installation without migration system // Existing installation without migration system
return [ return [
@ -126,10 +130,10 @@ class MigrationStatus
'instructions' => 'Mark initial migrations as applied using --fake flag' 'instructions' => 'Mark initial migrations as applied using --fake flag'
]; ];
} }
// Check if initial migrations are marked as applied // Check if initial migrations are marked as applied
$initialMigrationsApplied = $this->checkInitialMigrationsApplied(); $initialMigrationsApplied = $this->checkInitialMigrationsApplied();
if (!$initialMigrationsApplied) { if (!$initialMigrationsApplied) {
return [ return [
'type' => 'existing_partial_setup', 'type' => 'existing_partial_setup',
@ -139,7 +143,7 @@ class MigrationStatus
'instructions' => 'Run: php vendor/bin/phinx migrate --fake --target=20250619000002' 'instructions' => 'Run: php vendor/bin/phinx migrate --fake --target=20250619000002'
]; ];
} }
// Fully set up // Fully set up
return [ return [
'type' => 'fully_setup', 'type' => 'fully_setup',
@ -147,7 +151,7 @@ class MigrationStatus
'message' => 'Migration system fully configured', 'message' => 'Migration system fully configured',
'action_required' => false 'action_required' => false
]; ];
} catch (\Exception $e) { } catch (\Exception $e) {
return [ return [
'type' => 'error', 'type' => 'error',
@ -189,10 +193,11 @@ class MigrationStatus
private function checkInitialMigrationsApplied(): bool private function checkInitialMigrationsApplied(): bool
{ {
try { try {
$initialMigrationsCSV = implode(',', $this->initialMigrations);
$result = DB()->query(" $result = DB()->query("
SELECT COUNT(*) as migration_count SELECT COUNT(*) as migration_count
FROM {$this->migrationTable} FROM {$this->migrationTable}
WHERE version IN ('20250619000001', '20250619000002') WHERE version IN ($initialMigrationsCSV)
")->fetch(); ")->fetch();
return $result && $result->migration_count >= 2; return $result && $result->migration_count >= 2;