Newer
Older
use Breyta\Migration\CreateMigrationTable;
const INTERNAL_PREFIX = '@breyta/';
/** @var array|Model\Statement[] */
protected $statements = [];
/** @var AdapterInterface */
protected $adapter;
/** @var callable */
protected $resolver;
public function __construct(\PDO $db, string $path, callable $resolver = null)
{
if (!file_exists($path) || !is_dir($path)) {
throw new \InvalidArgumentException('The path to migrations is not valid');
}
// force the error mode to exception
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
/** @codeCoverageIgnore the default resolver is a) trivial and b) not testable */
$this->resolver = $resolver ?? function ($class, ...$args) {
if ($class === AdapterInterface::class) {
return new BasicAdapter(...$args);
}
return new $class(...$args);
};
}
public function getStatus(): \stdClass
{
$this->loadMigrations();
$status = (object)[
'migrations' => $this->migrations,
'count' => count(array_filter($this->migrations, function ($migration) {
return $migration->status !== 'done';
})),
if (count($this->missingMigrations)) {
$status->missing = $this->missingMigrations;
}
return $status;
public function migrate(): bool
/** @var Model\Migration[] $migrations */
$migrations = array_filter($this->getStatus()->migrations, function (Model\Migration $migration) {
return $migration->status !== 'done';
return $this->up(...$migrations);
}
// public function migrateTo(string $file)
// {
// $found = false;
// $migrations = [];
// foreach ($this->getStatus()->migrations as $migration) {
// $migrations[] = $migration;
// if (strpos($migration, $file) !== false) {
// $found = true;
// break;
// }
// }
//
// if (!$found) {
// throw new \LogicException('No migration found matching ' . $file);
// }
//
// /** @var Model\Migration[] $migrations */
// $migrations = array_filter($migrations, function (Model\Migration $migration) {
// return $migration->status !== 'done';
// });
//
// return $this->up(...$migrations);
// }
public function up(Model\Migration ...$migrations)
{
foreach ($migrations as $migration) {
$this->statements = [];
$start = microtime(true);
try {
$this->db->beginTransaction();
$class = self::internalClass($migration->file) ??
FileHelper::getClassFromFile($this->path . DIRECTORY_SEPARATOR . $migration->file);
/** @var AbstractMigration $migrationInstance */
$migrationInstance = call_user_func($this->resolver, $class, $this->getAdapter());
$migrationInstance->up();
$this->saveMigration($migration, 'done', microtime(true) - $start);
$this->db->commit();
$this->saveMigration($migration, 'failed', microtime(true) - $start);
throw $exception;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public function revert()
{
/** @var Model\Migration[] $migrations */
$migrations = array_filter($this->getStatus()->migrations, function (Model\Migration $migration) {
return $migration->status === 'done' && !self::isInternal($migration->file);
});
return $this->down(...array_reverse($migrations));
}
// public function revertTo(string $file)
// {
// $status = $this->getStatus();
// $found = false;
// $migrations = [];
// foreach (array_reverse($status->migrations) as $migration) {
// $migrations[] = $migration;
// if (strpos($migration, $file) !== false) {
// $found = true;
// break;
// }
// }
//
// if (!$found) {
// throw new \LogicException('No migration found matching ' . $file);
// }
//
// /** @var Model\Migration[] $toExecute */
// $toExecute = array_filter($status->migrations, function (Model\Migration $migration) {
// return $migration->status === 'done' && !self::isInternal($migration);
// });
//
// foreach ($toExecute as $migration) {
// $this->down($migration);
// }
//
// return true;
// }
public function down(Model\Migration ...$migrations)
{
foreach ($migrations as $migration) {
$this->statements = $migration->statements;
$start = microtime(true) - $migration->executionTime;
try {
$this->db->beginTransaction();
$class = self::internalClass($migration->file) ??
FileHelper::getClassFromFile($this->path . DIRECTORY_SEPARATOR . $migration->file);
/** @var AbstractMigration $migrationInstance */
$migrationInstance = call_user_func($this->resolver, $class, $this->getAdapter());
$migrationInstance->down();
$this->saveMigration($migration, 'reverted', microtime(true) - $start);
$this->db->commit();
} catch (\PDOException $exception) {
$this->db->rollBack();
throw $exception;
}
}
return true;
}
protected function saveMigration(Model\Migration $migration, $status, $executionTime)
{
$exists = (bool)$migration->executed;
$now = new \DateTime('now', new \DateTimeZone('UTC'));
$status === 'reverted' ? $migration->reverted = $now : $migration->executed = $now;
$migration->statements = $this->statements;
$migration->status = $status;
$migration->executionTime = $executionTime;
if (!$exists) {
$this->db->prepare("INSERT INTO migrations
(file, executed, status, statements, executionTime) VALUES
(?, ?, ?, ?, ?)
")->execute([
$migration->file,
$migration->executed->format('c'),
$migration->status,
json_encode($migration->statements),
$migration->executionTime
]);
} else {
$this->db->prepare("UPDATE migrations SET
executed = ?, reverted = ?, status = ?, statements = ?, executionTime = ?
WHERE file = ?
")->execute([
$migration->executed->format('c'),
$migration->reverted ? $migration->reverted->format('c') : null,
$migration->status,
json_encode($migration->statements),
$migration->executionTime,
$migration->file
]);
}
$migrations = $this->findMigrations();
// get the status of migrations from database
try {
$statement = $this->db->query('SELECT * FROM migrations');
if ($statement) {
$statement->setFetchMode(\PDO::FETCH_CLASS, Model\Migration::class);
while ($migration = $statement->fetch()) {
if (!isset($migrations[$migration->file])) {
$this->missingMigrations[] = $migration;
continue;
}
$migrations[$migration->file] = $migration;
}
}
} catch (\PDOException $exception) {
// the table does not exist - so nothing to do here
}
$this->migrations = array_values($migrations);
}
}
protected function findMigrations(): array
{
$migrations = [Model\Migration::createInstance([
'file' => self::INTERNAL_PREFIX . 'CreateMigrationTable.php',
/** @var \SplFileInfo $fileInfo */
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path)) as $fileInfo) {
if (is_dir($fileInfo->getPathname()) ||
$fileInfo->getFilename()[0] === '.' ||
substr($fileInfo->getFilename(), -4) !== '.php'
) {
continue;
}
$className = FileHelper::getClassFromFile($fileInfo->getPathname());
if (!$className) {
continue;
}
require_once $fileInfo->getPathname();
if (!is_subclass_of($className, AbstractMigration::class)) {
continue;
}
$file = substr($fileInfo->getPathname(), strlen($this->path) + 1);
$migrations[] = Model\Migration::createInstance([
'file' => $file,
'status' => 'new'
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
}
usort($migrations, function ($left, $right) {
// sort criteria 1: is from breyta
$leftIsFromBreyta = substr($left->file, 0, 8) === '@breyta/';
$rightIsFromBreyta = substr($right->file, 0, 8) === '@breyta/';
if ($leftIsFromBreyta !== $rightIsFromBreyta) {
return $rightIsFromBreyta - $leftIsFromBreyta;
}
$leftBaseName = basename($left->file);
$rightBaseName = basename($right->file);
// sort criteria 2: has creation date
$leftHasCreationDate = (int)preg_match(
'/^(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z)_/',
$leftBaseName,
$leftCreationDate
);
$rightHasCreationDate = (int)preg_match(
'/^(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z)_/',
$rightBaseName,
$rightCreationDate
);
if ($leftHasCreationDate !== $rightHasCreationDate) {
return $leftHasCreationDate - $rightHasCreationDate;
}
// sort criteria 3: by creation date
if (@$leftCreationDate[1] !== @$rightCreationDate[1]) {
list($leftDate, $leftTime) = explode('T', $leftCreationDate[1]);
list($rightDate, $rightTime) = explode('T', $rightCreationDate[1]);
list($leftTime, $rightTime) = str_replace('-', ':', [$leftTime, $rightTime]);
return strtotime($leftDate . 'T' . $leftTime) - strtotime($rightDate . 'T' . $rightTime);
}
// sort criteria 4: alphabetically
return strcmp($leftBaseName, $rightBaseName);
});
// key by identifier...
$migrations = array_combine(array_map(function ($migration) {
return $migration->file;
}, $migrations), $migrations);
protected function executeStatement(Model\Statement $statement)
{
$start = microtime(true);
try {
$statement->result = $this->db->exec($statement->raw);
$statement->exception = null;
} catch (\PDOException $exception) {
$statement->exception = $exception;
throw $exception;
} finally {
$statement->executionTime = microtime(true) - $start;
}
}
protected function getAdapter(): AdapterInterface
{
if (!$this->adapter) {
$this->adapter = call_user_func(
$this->resolver,
AdapterInterface::class,
function (Model\Statement $statement) {
$this->statements[] = $statement;
$this->executeStatement($statement);
protected static function internalClass(string $file): ?string
{
return self::isInternal($file) ? 'Breyta\\Migration\\' . substr($file, 8, -4) : null;
}
protected static function isInternal(string $file): bool
{
return strncmp($file, self::INTERNAL_PREFIX, strlen(self::INTERNAL_PREFIX)) === 0;
}