Skip to content

Commit 3cc6c11

Browse files
authored
Merge pull request #591 from wayofdev/feat/updates
2 parents d03489c + 62b0075 commit 3cc6c11

File tree

11 files changed

+76
-200
lines changed

11 files changed

+76
-200
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# With custom namespace provided, it will be used to prefix all services
33
# in Docker network for current project
44
COMPOSE_PROJECT_NAME=laravel-cycle-orm-adapter
5+
APP_ENV=production
56

67
DB_CONNECTION=pgsql
78
DB_HOST=pgsql

src/Bridge/Laravel/Console/Commands/Migrations/AbstractCommand.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function verifyEnvironment(?string $confirmationQuestion = null): bool
4040
{
4141
$confirmationQuestion = $confirmationQuestion ?? self::DEFAULT_CONFIRMATION;
4242

43-
if ($this->option('force') || $this->config->isSafe()) {
43+
if ($this->isForced() || $this->config->isSafe()) {
4444
return true;
4545
}
4646

@@ -69,11 +69,31 @@ protected function defineOptions(): array
6969
return array_merge(
7070
static::OPTIONS,
7171
[
72-
['force', 's', InputOption::VALUE_NONE, 'Skip safe environment check'],
72+
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
73+
['no-interaction', 'n', InputOption::VALUE_NONE, 'Do not ask any interactive question'],
7374
]
7475
);
7576
}
7677

78+
/**
79+
* Check if the operation is being forced.
80+
*/
81+
protected function isForced(): bool
82+
{
83+
return $this->option('force');
84+
}
85+
86+
/**
87+
* Check if the command is running in interactive mode.
88+
*/
89+
protected function isInteractive(): bool
90+
{
91+
return ! $this->option('no-interaction');
92+
}
93+
94+
/**
95+
* Configure the command options.
96+
*/
7797
protected function configure(): void
7898
{
7999
foreach ($this->defineOptions() as $option) {

src/Bridge/Laravel/Console/Commands/Migrations/InitCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class InitCommand extends AbstractCommand
1313
{
1414
protected $signature = 'cycle:migrate:init';
1515

16-
protected $description = 'Create the cycle orm migrations table';
16+
protected $description = 'Create the cycle orm migrations table if it does not exist.';
1717

1818
public function handle(): int
1919
{

src/Bridge/Laravel/Console/Commands/Migrations/MigrateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class MigrateCommand extends AbstractCommand
1717
{--o|one : Execute only one (first) migration. }
1818
{--seed : Indicates if the seed task should be re-run }';
1919

20-
protected $description = 'Perform one or all outstanding migrations';
20+
protected $description = 'Execute one or multiple migrations.';
2121

2222
public function handle(): int
2323
{
@@ -49,7 +49,7 @@ public function handle(): int
4949
}
5050

5151
if ($this->option('seed')) {
52-
$this->call('db:seed', ['--force' => $this->option('force')]);
52+
$this->call('db:seed', ['--force' => $this->isForced()]);
5353
}
5454

5555
return self::SUCCESS;

src/Bridge/Laravel/Console/Commands/Migrations/ReplayCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public function handle(): int
2222
return self::FAILURE;
2323
}
2424

25-
$rollback = ['--force' => $this->option('force')];
26-
$migrate = ['--force' => $this->option('force')];
25+
$rollback = ['--force' => $this->isForced()];
26+
$migrate = ['--force' => $this->isForced()];
2727

2828
if ($this->option('all')) {
2929
$rollback['--all'] = true;
3030
} else {
3131
$migrate['--one'] = true;
3232
}
3333

34-
$this->info('Rolling back executed migration(s)...');
34+
$this->warn('Rolling back executed migration(s)...');
3535
$this->call(RollbackCommand::class, $rollback);
3636

3737
$this->info('');

src/Bridge/Laravel/Console/Commands/ORM/Generators/ShowChanges.php

Lines changed: 0 additions & 170 deletions
This file was deleted.

src/Bridge/Laravel/Console/Commands/ORM/MigrateCommand.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
use Cycle\Migrations\State;
88
use Cycle\Schema\Compiler as CycleSchemaCompiler;
99
use Cycle\Schema\Generator\Migrations\GenerateMigrations;
10+
use Cycle\Schema\Generator\Migrations\Strategy\GeneratorStrategyInterface;
11+
use Cycle\Schema\Generator\Migrations\Strategy\MultipleFilesStrategy;
12+
use Cycle\Schema\Generator\PrintChanges;
1013
use Cycle\Schema\Registry;
14+
use Psr\Container\ContainerExceptionInterface;
15+
use Psr\Container\NotFoundExceptionInterface;
1116
use Symfony\Component\Console\Command\Command;
1217
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\Migrations\AbstractCommand;
1318
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\Migrations\MigrateCommand as DatabaseMigrateCommand;
14-
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\ORM\Generators\ShowChanges;
1519
use WayOfDev\Cycle\Contracts\CacheManager as CacheManagerContract;
1620
use WayOfDev\Cycle\Contracts\GeneratorLoader;
1721
use WayOfDev\Cycle\Schema\Compiler;
@@ -24,14 +28,18 @@
2428
final class MigrateCommand extends AbstractCommand
2529
{
2630
protected $signature = 'cycle:orm:migrate
27-
{--r|run : Automatically run generated migration }';
31+
{--r|run : Automatically run generated migration }
32+
{--s|split : Split generated migration into multiple files }';
2833

2934
protected $description = 'Generate ORM schema migrations.';
3035

36+
/**
37+
* @throws ContainerExceptionInterface
38+
* @throws NotFoundExceptionInterface
39+
*/
3140
public function handle(
3241
GeneratorLoader $generators,
3342
Registry $registry,
34-
GenerateMigrations $migrations,
3543
CacheManagerContract $cache
3644
): int {
3745
if (! $this->migrator->isConfigured()) {
@@ -40,19 +48,32 @@ public function handle(
4048

4149
foreach ($this->migrator->getMigrations() as $migration) {
4250
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) {
43-
$this->warn('Outstanding migrations found, run `cycle:orm:migrate` first!');
51+
if ($this->isInteractive() && $this->output->confirm('Outstanding migrations found. Do you want to run `cycle:migrate` now?')) {
52+
$this->call(DatabaseMigrateCommand::class, ['--force' => true]);
53+
} else {
54+
$this->warn('Outstanding migrations found, run `cycle:migrate` first.');
4455

45-
return self::FAILURE;
56+
return self::FAILURE;
57+
}
4658
}
4759
}
4860

49-
$diff = new ShowChanges($this->output);
61+
$this->comment('Detecting schema changes...');
62+
63+
$diff = new PrintChanges($this->output);
5064
$queue = $generators->add(GeneratorLoader::GROUP_RENDER, $diff);
5165

5266
$schemaCompiler = Compiler::compile($registry, $queue);
5367
$schemaCompiler->toMemory($cache);
5468

5569
if ($diff->hasChanges()) {
70+
if ($this->option('split')) {
71+
$this->info('Splitting generated migration into multiple files.');
72+
$this->laravel->singleton(GeneratorStrategyInterface::class, MultipleFilesStrategy::class);
73+
}
74+
75+
$migrations = $this->laravel->get(GenerateMigrations::class);
76+
5677
// Creates migration files in database/migrations directory.
5778
(new CycleSchemaCompiler())->compile($registry, [$migrations]);
5879

src/Bridge/Laravel/Console/Commands/ORM/SyncCommand.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace WayOfDev\Cycle\Bridge\Laravel\Console\Commands\ORM;
66

7+
use Cycle\Schema\Generator\PrintChanges;
78
use Cycle\Schema\Generator\SyncTables;
89
use Cycle\Schema\Registry;
9-
use Illuminate\Console\Command;
10-
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\ORM\Generators\ShowChanges;
10+
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\Migrations\AbstractCommand;
1111
use WayOfDev\Cycle\Contracts\CacheManager as CacheManagerContract;
1212
use WayOfDev\Cycle\Contracts\GeneratorLoader;
1313
use WayOfDev\Cycle\Schema\Compiler;
@@ -17,7 +17,7 @@
1717
*
1818
* @see https://github.com/spiral/cycle-bridge/blob/master/src/Console/Command/CycleOrm/SyncCommand.php
1919
*/
20-
final class SyncCommand extends Command
20+
final class SyncCommand extends AbstractCommand
2121
{
2222
protected $signature = 'cycle:orm:sync';
2323

@@ -28,7 +28,11 @@ public function handle(
2828
Registry $registry,
2929
CacheManagerContract $cache
3030
): int {
31-
$diff = new ShowChanges($this->output);
31+
if (! $this->verifyEnvironment('This operation is not recommended for production environment.')) {
32+
return self::FAILURE;
33+
}
34+
35+
$diff = new PrintChanges($this->output);
3236
$queue = $generators
3337
->add(GeneratorLoader::GROUP_RENDER, $diff)
3438
->add(GeneratorLoader::GROUP_POSTPROCESS, new SyncTables());
@@ -37,7 +41,7 @@ public function handle(
3741
$schemaCompiler->toMemory($cache);
3842

3943
if ($diff->hasChanges()) {
40-
$this->info('ORM Schema has been synchronized!');
44+
$this->info('ORM Schema has been synchronized with database.');
4145
}
4246

4347
return self::SUCCESS;

src/Bridge/Laravel/Console/Commands/ORM/UpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function handle(
3030

3131
Compiler::compile($registry, $generators)->toMemory($cache);
3232

33-
$this->info('Done');
33+
$this->info('Schema has been updated.');
3434

3535
return self::SUCCESS;
3636
}

0 commit comments

Comments
 (0)