2024-07-29 15:57:20 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace kernel\console\controllers;
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
|
use kernel\App;
|
|
|
|
use kernel\console\CgMigrationCreator;
|
|
|
|
use kernel\console\ConsoleController;
|
|
|
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
|
|
|
|
use Illuminate\Database\Migrations\MigrationCreator;
|
|
|
|
use Illuminate\Database\Migrations\Migrator;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Database\Schema\Builder;
|
2024-09-23 12:50:50 +03:00
|
|
|
use kernel\services\ModuleService;
|
2024-07-29 15:57:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MigrationController extends ConsoleController
|
|
|
|
{
|
2024-09-23 12:50:50 +03:00
|
|
|
protected ModuleService $moduleService;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->moduleService = new ModuleService();
|
|
|
|
}
|
|
|
|
|
2024-07-29 15:57:20 +03:00
|
|
|
//create migrations table
|
|
|
|
public function actionCreateMigrationTable(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
App::$db->schema->create('migration', function (Blueprint $table) {
|
|
|
|
$table->bigIncrements('id');
|
|
|
|
$table->string('migration', 255);
|
|
|
|
$table->integer('batch');
|
|
|
|
});
|
|
|
|
$this->out->r("Success", 'green');
|
|
|
|
}
|
|
|
|
catch (\Exception $e){
|
|
|
|
$this->out->r($e->getMessage(), 'red');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create migrations
|
|
|
|
public function actionCreate(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (!isset($this->argv['name'])) {
|
|
|
|
throw new \Exception('Missing migration "--name" specified');
|
|
|
|
}
|
|
|
|
$m = new CgMigrationCreator(new Filesystem(), CgMigrationCreator::getCustomStubPath());
|
|
|
|
|
|
|
|
$path = $this->argv['path'] ?? 'migrations';
|
|
|
|
|
|
|
|
$res = $m->create(
|
|
|
|
$this->argv['name'],
|
|
|
|
ROOT_DIR . '/' . $path,
|
|
|
|
$this->argv['table'] ?? null,
|
|
|
|
!isset($this->argv['update'])
|
|
|
|
);
|
|
|
|
$this->out->r(basename($res) . " created", 'green');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->out->r('Message: ' .$e->getMessage(), 'red');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//execute migrations
|
|
|
|
public function actionRun(): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
|
|
|
|
|
|
|
|
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem());
|
2024-09-23 12:50:50 +03:00
|
|
|
$migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']);
|
|
|
|
//$migrationPaths = [ROOT_DIR . '/migrations'];
|
2024-07-29 15:57:20 +03:00
|
|
|
$res = $m->run($migrationPaths);
|
|
|
|
foreach ($res as $re){
|
|
|
|
$this->out->r(basename($re), 'green');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e){
|
|
|
|
$this->out->r('Message: ' .$e->getMessage(), 'red');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionRollback(): void
|
|
|
|
{
|
|
|
|
try {
|
2024-09-03 16:29:44 +03:00
|
|
|
$step = $this->argv['step'] ?? 1;
|
2024-07-29 15:57:20 +03:00
|
|
|
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
|
|
|
|
|
|
|
|
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem());
|
|
|
|
//$migrationPaths = array_merge(App::$migrationsPaths, [WORKSPACE_DIR . '/console/migrations']);
|
2024-09-03 16:29:44 +03:00
|
|
|
$migrationPaths = [ROOT_DIR . '/migrations'];
|
|
|
|
$res = $m->rollback($migrationPaths, ['step' => $step]);
|
|
|
|
print_r($step);
|
2024-07-29 15:57:20 +03:00
|
|
|
foreach ($res as $re){
|
|
|
|
$this->out->r(basename($re), 'green');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e){
|
|
|
|
$this->out->r('Message: ' .$e->getMessage(), 'red');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|