Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mgolshan-talentnet opened this issue Jul 26, 2018 · 6 comments
Labels

Comments

@mgolshan-talentnet
Copy link

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
Copy link
Member

caciobanu commented Jul 26, 2018

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

@mgolshan-talentnet
Copy link
Author

@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
Copy link
Author

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

Thanks

@mgolshan-talentnet
Copy link
Author

@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
Copy link
Member

caciobanu commented Jul 27, 2018

  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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants