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