1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Biurad opensource projects. |
5
|
|
|
* |
6
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Flange\Commands\CycleORM; |
14
|
|
|
|
15
|
|
|
use Cycle\Database\DatabaseProviderInterface; |
16
|
|
|
use Cycle\Migrations\Migrator; |
17
|
|
|
use Cycle\Migrations\State; |
18
|
|
|
use Cycle\Schema\Compiler; |
19
|
|
|
use Cycle\Schema\Generator\Migrations\GenerateMigrations; |
20
|
|
|
use Cycle\Schema\Generator\SyncTables; |
21
|
|
|
use Cycle\Schema\GeneratorInterface; |
22
|
|
|
use Cycle\Schema\Registry; |
23
|
|
|
use Flange\Database\Cycle\Generator\ShowChanges; |
24
|
|
|
use Symfony\Component\Console\Command\Command; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cycle ORM synchronizer command. |
31
|
|
|
* |
32
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class DatabaseOrmCommand extends Command |
35
|
|
|
{ |
36
|
|
|
protected static $defaultName = 'cycle:database:orm'; |
37
|
|
|
protected static $defaultDescription = 'Generate ORM schema migrations and run if possible'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array<int,GeneratorInterface> $generators |
41
|
|
|
*/ |
42
|
|
|
public function __construct(private Migrator $migrator, private DatabaseProviderInterface $provider, private array $generators) |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
51
|
|
|
{ |
52
|
|
|
$this->migrator->configure(); |
53
|
|
|
$io = new SymfonyStyle($input, $output); |
54
|
|
|
|
55
|
|
|
foreach ($this->migrator->getMigrations() as $migration) { |
56
|
|
|
if (State::STATUS_EXECUTED !== $migration->getState()->getStatus()) { |
57
|
|
|
$io->error('Outstanding migrations found, run `cycle:database:migrate` first.'); |
58
|
|
|
|
59
|
|
|
return self::FAILURE; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->generators[] = $show = new ShowChanges($output); |
64
|
|
|
|
65
|
|
|
if (!\class_exists(GenerateMigrations::class)) { |
66
|
|
|
$io->warning('Synchronizing ORM schema into database is a ricky operation, instead "composer require cycle/schema-migrations-generator".'); |
67
|
|
|
|
68
|
|
|
if (!$io->confirm('Do you want to proceed now?', false)) { |
69
|
|
|
return self::SUCCESS; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->generators[] = new SyncTables(); |
73
|
|
|
$autoMigrate = false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
(new Compiler())->compile($registry = new Registry($this->provider), $this->generators); |
77
|
|
|
|
78
|
|
|
if ($show->hasChanges()) { |
79
|
|
|
if (!isset($autoMigrate)) { |
80
|
|
|
(new GenerateMigrations($this->migrator->getRepository(), $this->migrator->getConfig()))->run($registry); |
81
|
|
|
|
82
|
|
|
if ($io->confirm('Do you want to run migrations now?', false)) { |
83
|
|
|
return $this->getApplication()->find('cycle:database:migrate')->run($input, $output); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$io->writeln("\n<info>ORM Schema has been synchronized</info>"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::SUCCESS; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|