1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Command; |
4
|
|
|
|
5
|
|
|
use Cycle\Migrations\MigrationImage; |
6
|
|
|
use Spiral\Database\DatabaseManager; |
7
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
8
|
|
|
use Spiral\Migrations\Exception\RepositoryException; |
9
|
|
|
use Spiral\Migrations\MigrationInterface; |
10
|
|
|
use Spiral\Migrations\Migrator; |
11
|
|
|
use Spiral\Migrations\State; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Yiisoft\Yii\Cycle\Helper\CycleOrmHelper; |
15
|
|
|
|
16
|
|
|
abstract class BaseMigrationCommand extends Command |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var DatabaseManager */ |
20
|
|
|
protected $dbal; |
21
|
|
|
/** @var MigrationConfig */ |
22
|
|
|
protected $config; |
23
|
|
|
/** @var Migrator */ |
24
|
|
|
protected $migrator; |
25
|
|
|
/** @var CycleOrmHelper */ |
26
|
|
|
protected $cycleOrmHelper; |
27
|
|
|
|
28
|
|
|
protected static $migrationStatus = [ |
29
|
|
|
State::STATUS_UNDEFINED => 'undefined', |
30
|
|
|
State::STATUS_PENDING => 'pending', |
31
|
|
|
State::STATUS_EXECUTED => 'executed', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
DatabaseManager $dbal, |
36
|
|
|
MigrationConfig $conf, |
37
|
|
|
Migrator $migrator, |
38
|
|
|
CycleOrmHelper $cycleOrmHelper |
39
|
|
|
) { |
40
|
|
|
parent::__construct(); |
41
|
|
|
$this->dbal = $dbal; |
42
|
|
|
$this->config = $conf; |
43
|
|
|
$this->migrator = $migrator; |
44
|
|
|
$this->cycleOrmHelper = $cycleOrmHelper; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function createEmptyMigration(OutputInterface $output, string $name, ?string $database = null): ?MigrationImage |
48
|
|
|
{ |
49
|
|
|
if ($database === null) { |
50
|
|
|
// get default database |
51
|
|
|
$database = $this->dbal->database()->getName(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$migrationSkeleton = new MigrationImage($this->config, $database); |
55
|
|
|
$migrationSkeleton->setName($name); |
56
|
|
|
try { |
57
|
|
|
$migrationFile = $this->migrator->getRepository()->registerMigration( |
58
|
|
|
$migrationSkeleton->buildFileName(), |
59
|
|
|
$migrationSkeleton->getClass()->getName(), |
60
|
|
|
$migrationSkeleton->getFile()->render() |
61
|
|
|
); |
62
|
|
|
} catch (RepositoryException $e) { |
63
|
|
|
$output->writeln('<fg=yellow>Can not create migration</>'); |
64
|
|
|
$output->writeln('<fg=red>' . $e->getMessage() . '</>'); |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
$output->writeln('<info>New migration file has been created</info>'); |
68
|
|
|
$output->writeln("<fg=cyan>{$migrationFile}</>"); |
69
|
|
|
return $migrationSkeleton; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param OutputInterface $output |
74
|
|
|
* @return MigrationInterface[] |
75
|
|
|
*/ |
76
|
|
|
protected function findMigrations(OutputInterface $output): array |
77
|
|
|
{ |
78
|
|
|
$list = $this->migrator->getMigrations(); |
79
|
|
|
$output->writeln('<info>' . count($list) . ' migration(s) found in ' . $this->config->getDirectory() . '</info>'); |
80
|
|
|
return $list; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|