1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Migrations; |
6
|
|
|
|
7
|
|
|
use Suricate\Interfaces\IMigration; |
8
|
|
|
use Suricate\Service; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* DB Migration extension for Suricate |
12
|
|
|
* |
13
|
|
|
* @package Suricate |
14
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class MigrationService extends Service |
19
|
|
|
{ |
20
|
|
|
protected array $registeredMigrations = []; |
21
|
|
|
|
22
|
|
|
public function registerMigration(IMigration $migration) |
23
|
|
|
{ |
24
|
|
|
// FIXME: test with migration outside of migration folder (eg: media module ) |
25
|
|
|
$this->registeredMigrations[$migration->getName()] = $migration->getSQL(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function scanForMigrations() |
29
|
|
|
{ |
30
|
|
|
$files = glob(app_path('migrations/*.php')); |
31
|
|
|
foreach ($files as $file) { |
32
|
|
|
$migrationClassName = str_replace('.php', '', basename($file)); |
33
|
|
|
include($file); |
34
|
|
|
// Class is defined inside the file |
35
|
|
|
if (class_exists($migrationClassName)) { |
36
|
|
|
$migration = new $migrationClassName(); |
37
|
|
|
if ($migration instanceof IMigration) { |
38
|
|
|
$this->registerMigration($migration); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function initMigrationTable(): int |
46
|
|
|
{ |
47
|
|
|
$this->scanForMigrations(); |
48
|
|
|
$migrationModel = new MigrationModel(); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
return $migrationModel->createMigrationTable(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function listMigrations() |
55
|
|
|
{ |
56
|
|
|
$this->scanForMigrations(); |
57
|
|
|
|
58
|
|
|
$migrations = MigrationModelList::loadAll(); |
59
|
|
|
$alreadyMigrated = []; |
60
|
|
|
$result = []; |
61
|
|
|
foreach ($migrations as $migration) { |
62
|
|
|
$alreadyMigrated[$migration->name] = true; |
63
|
|
|
$result[$migration->name] = $migration->date_added; |
64
|
|
|
} |
65
|
|
|
foreach (array_keys($this->registeredMigrations) as $regMigrationName) { |
66
|
|
|
if (isset($alreadyMigrated[$regMigrationName])) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
$result[$regMigrationName] = false; |
70
|
|
|
} |
71
|
|
|
ksort($result); |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function doMigrations() |
76
|
|
|
{ |
77
|
|
|
echo "[Migration] Starting migrations\n"; |
78
|
|
|
|
79
|
|
|
$migrations = $this->listMigrations(); |
80
|
|
|
$migrationsToDo = array_filter($migrations, function ($item) { |
81
|
|
|
return $item === false; |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
if (count($migrationsToDo) === 0) { |
85
|
|
|
echo "[Migration] Nothing to migrate\n"; |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
foreach (array_keys($migrationsToDo) as $migrationName) { |
89
|
|
|
$migration = new $migrationName(); |
90
|
|
|
// FIXME: do migration |
91
|
|
|
// FIXME: connection configuration in migration |
92
|
|
|
_p($migration); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function createMigration(): string|bool |
97
|
|
|
{ |
98
|
|
|
$migrationName = 'v' . date('Ymdhis'); |
99
|
|
|
|
100
|
|
|
$template = <<<EOD |
101
|
|
|
<?php |
102
|
|
|
|
103
|
|
|
use Suricate\Interfaces\IMigration; |
104
|
|
|
|
105
|
|
|
class {$migrationName} implements IMigration |
106
|
|
|
{ |
107
|
|
|
public function getName(): string |
108
|
|
|
{ |
109
|
|
|
return __CLASS__; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getSQL(): string |
113
|
|
|
{ |
114
|
|
|
return ''; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Leave empty string if you want to use default config name |
118
|
|
|
public function getConfigName(): string |
119
|
|
|
{ |
120
|
|
|
return ''; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
EOD; |
124
|
|
|
$filename = app_path('migrations/' . $migrationName . '.php'); |
125
|
|
|
$directory = pathinfo($filename, PATHINFO_DIRNAME); |
126
|
|
|
if (!is_dir($directory)) { |
|
|
|
|
127
|
|
|
$ret = mkdir($directory, 0755, true); |
|
|
|
|
128
|
|
|
if (!$ret) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
$fp = fopen($filename, 'w'); |
133
|
|
|
if ($fp === false) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
$ret = fputs($fp, $template); |
137
|
|
|
fclose($fp); |
138
|
|
|
if ($ret !== false) { |
139
|
|
|
return $migrationName; |
140
|
|
|
} |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|