doesntmattr / mongodb-migrations-bundle

Symfony MongoDBMigrationsBundle
MIT License
23 stars 27 forks source link

The interaction is not being off when I call the migration command within another command #23

Closed mgolshan-talentnet closed 6 years ago

mgolshan-talentnet commented 6 years ago

Hi.

I am trying to run the command inside another command using a loop for all DBs.

Unfortunately, the process doesn't go through because it keeps asks for the confirmation on each call. I tried different ways to pass the option --no-interaction. But, no luck. When I run the command mongodb:migrations:migrate by itself and passing the option, it works.

Here is my code:

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @throws \App\Exception\InvalidTenantException
     */
    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        // try to off the interaction
        $input->setInteractive(false);
        $io = new SymfonyStyle($input, $output);

        // Get the list of DB names which could be handles by tenant registry
        $tenants = \array_column(
            $this->tenantRegistry->getTenants(),
            'name'
        );

        if (empty($tenants)) {
            $io->warning('No tenants found ... aborting');

            return;
        }

        $plainCommandName = 'mongodb:migrations:migrate';
        $command = $this->getApplication()->find($plainCommandName);
        $command->interact($input, $output);

        $io->writeln(sprintf('[Environment: %s]', $this->environment));

        foreach ($tenants as $tenant) {
            $io->writeln(sprintf('Migrating the tenant <info>%s</info> ...', $tenant));

            $this->tenantRegistry->setTenant($tenant);

            $inputArguments = new ArrayInput(
                [
                    'command'  => $plainCommandName,
                    '--env'    => $this->environment,
                    '--no-interaction' => true
                ]
            );

            $command->run($inputArguments, $output);

            $io->writeln(str_repeat('=', 100));
        }
    }

Could you advise a solution?

caciobanu commented 6 years ago

Try removing the call $command->interact($input, $output); and adding $inputArguments->setInteractive(false);

mgolshan-talentnet commented 6 years ago

@caciobanu Thanks for quick response. I just tried and it still the same. Actually, I added that line of code after trying different solutions.

mgolshan-talentnet commented 6 years ago

Wait. Looks like you updated your comment $inputArguments->setInteractive(false); worked 👏

Thanks

mgolshan-talentnet commented 6 years ago

@caciobanu I am running to another issue now. As you see, I am trying to run the command in a loop. Looks like, on each round, it tries to register the migration versions. So, in the second round of the loop, I get this error:

Migration version 20180626192820 already registered with class AntiMattr\MongoDB\Migrations\Version

caciobanu commented 6 years ago
  1. I've hit the comment button by mistake before finishing to write the whole solution.
  2. I'm not sure how you can fix that with your current solution of running the commands in a loop in the same process. I suggest that you run the commands in a loop with the help of the symfony/process component. It will isolate your commands one from another, and you can even run them in parallel.
mgolshan-talentnet commented 6 years ago

Thanks @caciobanu Looks like Symfony process is a working solution. Cheers