Passed
Push — master ( 127cbd...104cf6 )
by Luiz Kim
17:15 queued 15:03
created

MigrateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 11
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ControleOnline\Command;
4
5
use ControleOnline\Service\DatabaseSwitchService;
6
use Symfony\Component\Console\Attribute\AsCommand;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Attribute\AsCommand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Console\Input\ArrayInput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\ArrayInput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
#[AsCommand(
15
    name: 'tenant:migrations:migrate',
16
    description: 'Proxy para migrar um novo banco de dados de tenant.',
17
)]
18
final class MigrateCommand extends Command
19
{
20
    private DatabaseSwitchService $databaseSwitchService;
21
22
    public function __construct(DatabaseSwitchService $databaseSwitchService)
23
    {
24
        $this->databaseSwitchService = $databaseSwitchService;
25
        parent::__construct();
26
    }
27
28
    protected function configure(): void
29
    {
30
        $this
31
            ->setName('tenant:migrations:migrate')
32
            ->setAliases(['t:m:m'])
33
            ->setDescription('Proxy para executar doctrine:migrations:migrate para um banco de dados específico.')
34
            ->addArgument('domain', InputArgument::OPTIONAL, 'Identificador do domínio do banco de dados')
35
            ->addArgument('version', InputArgument::OPTIONAL, 'O número da versão (AAAAMMDDHHMMSS) ou alias (first, prev, next, latest) para migrar.', 'latest')
36
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Executar a migração como uma simulação.')
37
            ->addOption('query-time', null, InputOption::VALUE_NONE, 'Medir o tempo de todas as consultas individualmente.')
38
            ->addOption('allow-no-migration', null, InputOption::VALUE_NONE, 'Não lançar uma exceção quando nenhuma alteração for detectada.');
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        $domain = $input->getArgument('domain');
44
45
        if ($domain) {
46
            return $this->migrateByDomain($domain, $input, $output);
47
        }
48
49
        $domains = $this->databaseSwitchService->getAllDomains();
50
        $exitCode = Command::SUCCESS;
51
52
        foreach ($domains as $domain) {
53
            $result = $this->migrateByDomain($domain, $input, $output);
54
            if ($result !== Command::SUCCESS) {
55
                $exitCode = Command::FAILURE;
56
            }
57
        }
58
59
        return $exitCode;
60
    }
61
62
    protected function migrateByDomain(string $domain, InputInterface $input, OutputInterface $output): int
63
    {
64
        $this->databaseSwitchService->switchDatabaseByDomain($domain);
65
66
        $newInput = new ArrayInput([
67
            'version' => $input->getArgument('version'),
68
            '--dry-run' => $input->getOption('dry-run'),
69
            '--query-time' => $input->getOption('query-time'),
70
            '--allow-no-migration' => $input->getOption('allow-no-migration'),
71
        ]);
72
        $newInput->setInteractive(false);
73
74
        $output->writeln(sprintf('Executando migrações para o domínio: %s', $domain));
75
76
        $command = $this->getApplication()->find('doctrine:migrations:migrate');
77
        return $command->run($newInput, $output);
78
    }
79
}
80