2

Tried this in while loop:

 $command = array(
                'command' => 'doctrine:migrations:execute',
                '--em' => "dynamic",
                'version' => $this->container->getParameter('migration_version')
 );
 $kernel = $this->getContainer()->get('kernel');
 $application = new Application($kernel);
 $application->setAutoExit(false);
 $input = new ArrayInput($command);
 $output = new BufferedOutput();
 $result = $application->run($input, $output);
 $s=$output->fetch();

The error getting is:

Migration version 20171024105242 already registered with class Doctrine\DBAL\Migrations\Version

Please help me

public function switchDatabase($host, $user, $dbname)
    {
        //check the company database exist or not
        $password = getenv('DEFAULT_DB_PASSWORD');
        if (gettype($host) == 'resource') {
            $host = long2ip((int) stream_get_contents($host, -1, 0));
        }
        try {
            $conn = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $password);

            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $conn = null;
        } catch (\PDOException $e) {
            return $e->getMessage();
        }
        // dynamic DB switch
        $connection = $this->getContainer()->get(sprintf('doctrine.dbal.%s_connection', 'dynamic'));
        $connection->close();
        $refConn = new \ReflectionObject($connection);
        $refParams = $refConn->getProperty('_params');
        $refParams->setAccessible('public'); //we have to change it for a moment
        $params = $refParams->getValue($connection);
        $params['dbname'] = $dbname;
        $params['host'] = $host;
        $params['user'] = $user;
        $refParams->setAccessible('private');
        $refParams->setValue($connection, $params);
        $this->getContainer()->get('doctrine')->resetEntityManager('dynamic');

        return true;
    }

This is the code used for db switching db switching is working fine. but the above error comes after 2nd migration in loop

9
  • 2
    Tried this in while loop: What are you trying to achieve in a while loop ? The error message is clear, you already run the migration 20171024105242 on your current database Commented Aug 21, 2018 at 8:49
  • 4
    $this->container->getParameter('migration_version') will not get changed in loop. . try some other method Commented Aug 21, 2018 at 10:40
  • @Mcsky switching to different dbs in while loop and run the migration in my case. First migration was successful the next ones was showing like this Commented Aug 21, 2018 at 12:28
  • $this->container->getParameter('migration_version') is same Commented Aug 21, 2018 at 12:29
  • @sarin.. But it should be dynamic.. If you try to run same migration twice it will throw the error. Why don't you run command doctrine:migrations:migrate.. It will execute all migrations automatically. You don't need a loop to do that Commented Aug 21, 2018 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.