public function migrate()
{
// find migrations
$folders = $this->app->pathfinder->search('dbupdates', '', 'path');
// todo - sort files in folders
foreach ($folders as $dir) {
$files = scandir($dir);
sort($files);
foreach ($files as $name) {
if (strtolower(substr($name, -4)) != '.sql') {
continue;
}
$q = $this->db->dsql()->table('_db_update')->where('name', strtolower($name))->field('status');
if ($q->getOne() === 'ok') {
continue;
}
$migration = file_get_contents($dir . '/' . $name);
$q->set('name', strtolower($name));
try {
$this->db->dbh->exec($migration);
$q->set('status', 'ok')->replace();
} catch (Exception $e) {
$q->set('status', 'fail')->replace();
if (!$e instanceof BaseException) {
$e = $this->exception()->addMoreInfo('Original error', $e->getMessage());
}
throw $e->addMoreInfo('file', $name);
}
}
}
return $this;
}