MicroFrameWork/kernel/console/controllers/MigrationController.php

107 lines
3.5 KiB
PHP
Raw Permalink Normal View History

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;
use kernel\services\ModuleService;
2024-07-29 15:57:20 +03:00
class MigrationController extends ConsoleController
{
protected ModuleService $moduleService;
2024-09-23 17:03:42 +03:00
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');
2024-09-23 17:03:42 +03:00
} catch (\Exception $e) {
2024-07-29 15:57:20 +03:00
$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) {
2024-09-23 17:03:42 +03:00
$this->out->r('Message: ' . $e->getMessage(), 'red');
2024-07-29 15:57:20 +03:00
}
}
//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 17:03:42 +03:00
if (\kernel\App::$db->schema->hasTable('option')) {
$migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']);
} else {
$migrationPaths = [ROOT_DIR . '/migrations'];
}
2024-07-29 15:57:20 +03:00
$res = $m->run($migrationPaths);
2024-09-23 17:03:42 +03:00
foreach ($res as $re) {
2024-07-29 15:57:20 +03:00
$this->out->r(basename($re), 'green');
}
2024-09-23 17:03:42 +03:00
} catch (\Exception $e) {
$this->out->r('Message: ' . $e->getMessage(), 'red');
2024-07-29 15:57:20 +03:00
}
}
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-09-23 17:03:42 +03:00
foreach ($res as $re) {
2024-07-29 15:57:20 +03:00
$this->out->r(basename($re), 'green');
}
2024-09-23 17:03:42 +03:00
} catch (\Exception $e) {
$this->out->r('Message: ' . $e->getMessage(), 'red');
2024-07-29 15:57:20 +03:00
}
}
}