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

sarin
  • 35
  • 8
  • 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 – Mcsky Aug 21 '18 at 08:49
  • 4
    `$this->container->getParameter('migration_version')` will not get changed in loop. . try some other method – hanish singla Aug 21 '18 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 – sarin Aug 21 '18 at 12:28
  • $this->container->getParameter('migration_version') is same – sarin Aug 21 '18 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 – hanish singla Aug 21 '18 at 15:01
  • Same migration but different database.need to run migration for all the databases – sarin Aug 21 '18 at 16:52
  • Yes for all databases. . you can run `doctrine:migrations:migrate`. . You don't need specific version to pass. DB has a table "migration_versions" to keep record of migrations. If it gives you error then it means your database is not correct and current DB already have these changes. try this one https://stackoverflow.com/questions/6844153/migrating-multiple-databases-using-doctrine2-with-symfony2 – hanish singla Aug 22 '18 at 07:27
  • doctrine:migrations:migrate 20180605025653 will migrate single databse. I am switching the database in the loop. I am not ruuning in terminal – sarin Aug 22 '18 at 09:05
  • Migration is not working after first loop – sarin Aug 28 '18 at 17:44

0 Answers0