Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Commit 07eb63d

Browse files
committed
Command: Rewrite make:migration, fix #1
1 parent 4b9d69e commit 07eb63d

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ Your commit is checked by several dev tools (like phpstan, php cs fixer...). The
5656

5757

5858
## Development notes
59-
For make new migration, don't use the command `php bin/console make:migration` (no compatible with DoctrineMigrationsMultipleDatabaseBundle), instead use: `php bin/console doctrine:migrations:diff`.
59+
For make new migrations in dev environnement, use the command `php bin/console make:migration` (this poc update the command with adding multiple databases support).
60+
61+
When you use the command `make:entity`, you should add the database namespace before the entity name. Example: _Second\Article_.

config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ services:
3131
tags:
3232
- { name: doctrine.event_subscriber, event: prePersist }
3333
- { name: doctrine.event_subscriber, event: preUpdate }
34+
35+
maker.maker.make_migration:
36+
class: App\Command\MakeMigrationCommand

src/Command/FixtureCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class FixtureCommand extends Command
1616
{
1717
// Entity Manager name (groups should be used the same value)
18-
private const LIST_DATABASE = ['main', 'second'];
18+
public const LIST_DATABASE = ['main', 'second'];
1919

2020
/** @var bool[] */
2121
private array $requirement;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace App\Command;
3+
4+
use Symfony\Component\Console\Attribute\AsCommand;
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\{ArrayInput, InputInterface};
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Style\SymfonyStyle;
9+
use Symfony\Component\HttpKernel\KernelInterface;
10+
11+
#[AsCommand(
12+
name: 'make:migration',
13+
description: 'Enhance the command for support multiple databases.',
14+
)]
15+
class MakeMigrationCommand extends Command
16+
{
17+
/** @var bool[] */
18+
private array $requirement;
19+
20+
public function __construct(KernelInterface $kernel, ?string $name = null)
21+
{
22+
parent::__construct($name);
23+
24+
$this->requirement = [
25+
'doctrine_migration' => array_key_exists('DoctrineMigrationsBundle', $kernel->getBundles()),
26+
];
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output): int
30+
{
31+
$io = new SymfonyStyle($input, $output);
32+
if (!$this->getApplication()) {
33+
return Command::INVALID;
34+
} elseif (!$this->requirement['doctrine_migration']) {
35+
$io->error('The bundle DoctrineMigrationsBundle is inactive or the APP_ENV value is not dev.');
36+
37+
return Command::INVALID;
38+
}
39+
40+
foreach (FixtureCommand::LIST_DATABASE as $database) {
41+
try {
42+
$io->title('Creation migration for database: '.$database);
43+
$symfonyCommand = $this->getApplication()->find('doctrine:migrations:diff');
44+
$symfonyCommand->run(new ArrayInput(['--em' => $database]), $output);
45+
} catch (\Throwable $exception) {
46+
$io->writeln($exception->getMessage());
47+
}
48+
}
49+
50+
return Command::INVALID;
51+
}
52+
}

0 commit comments

Comments
 (0)