/** * Executes DB changes based in the classes defined in * src/Chamilo/CoreBundle/Migrations/Schema/* * * @param string $chamiloVersion * @param EntityManager $manager * @throws \Doctrine\DBAL\DBALException */ function migrate($chamiloVersion, EntityManager $manager) { $debug = true; $connection = $manager->getConnection(); $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection); // Table name that will store migrations log (will be created automatically, // default name is: doctrine_migration_versions) $config->setMigrationsTableName('version'); // Namespace of your migration classes, do not forget escape slashes, do not add last slash $config->setMigrationsNamespace('Application\\Migrations\\Schema\\V' . $chamiloVersion); // Directory where your migrations are located $config->setMigrationsDirectory(api_get_path(SYS_PATH) . 'app/Migrations/Schema/V' . $chamiloVersion); // Load your migrations $config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); $migration = new \Doctrine\DBAL\Migrations\Migration($config); $versions = $config->getMigrations(); /** @var Doctrine\DBAL\Migrations\Version $migrationItem */ foreach ($versions as $version) { $version->getMigration()->setEntityManager($manager); } $to = null; // Retrieve SQL queries that should be run to migrate you schema to $to version, // if $to == null - schema will be migrated to latest version $versions = $migration->getSql($to); if ($debug) { $nl = '<br>'; foreach ($versions as $version => $queries) { echo 'VERSION: ' . $version . $nl; echo '----------------------------------------------' . $nl . $nl; foreach ($queries as $query) { echo $query . $nl; } echo $nl . $nl; } } try { // Execute migration! $migration->migrate($to); if ($debug) { echo 'DONE' . $nl; } } catch (Exception $ex) { if ($debug) { echo 'ERROR: ' . $ex->getMessage() . $nl; } } }
/** * Executes DB changes based in the classes defined in * src/Chamilo/CoreBundle/Migrations/Schema/* * * @param string $chamiloVersion * @param EntityManager $manager * @throws \Doctrine\DBAL\DBALException */ function migrate($chamiloVersion, EntityManager $manager) { $debug = true; $connection = $manager->getConnection(); $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection); // Table name that will store migrations log (will be created automatically, // default name is: doctrine_migration_versions) $config->setMigrationsTableName('version'); // Namespace of your migration classes, do not forget escape slashes, do not add last slash $config->setMigrationsNamespace('Application\\Migrations\\Schema\\V' . $chamiloVersion); // Directory where your migrations are located $config->setMigrationsDirectory(api_get_path(SYS_PATH) . 'app/Migrations/Schema/V' . $chamiloVersion); // Load your migrations $config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); $migration = new \Doctrine\DBAL\Migrations\Migration($config); $versions = $config->getMigrations(); /** @var Doctrine\DBAL\Migrations\Version $migrationItem */ foreach ($versions as $version) { $version->getMigration()->setEntityManager($manager); } $to = null; // if $to == null then schema will be migrated to latest version echo "<pre>"; try { // Execute migration! $migratedSQL = $migration->migrate($to); if ($debug) { foreach ($migratedSQL as $version => $sqlList) { echo "VERSION: {$version}<br>"; echo "----------------------------------------------<br>"; foreach ($sqlList as $sql) { echo "<code>{$sql}</code><br>"; } } echo "<br>DONE!<br>"; } return true; } catch (Exception $ex) { if ($debug) { echo "ERROR: {$ex->getMessage()}<br>"; return false; } } echo "</pre>"; return false; }
protected function getDoctrineMigrationConfig() { $config = Registry::getConfig(); // Doctrine Migration $entityManager = Registry::getEntityManager(); $configuration = new \Doctrine\DBAL\Migrations\Configuration\Configuration($entityManager->getConnection()); $configuration->setName($config->migration->doctrine->name); $configuration->setMigrationsNamespace($config->migration->doctrine->migrations_namespace); $configuration->setMigrationsTableName($config->migration->doctrine->table_name); $configuration->setMigrationsDirectory($config->migration->doctrine->migrations_directory); $configuration->registerMigrationsFromDirectory($configuration->getMigrationsDirectory()); return $configuration; }
#!/usr/bin/env php <?php use Symfony\Component\Console\Application; require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../app/bootstrap.php'; $application = new Application(); // Migrations Commands $configure = new \Doctrine\DBAL\Migrations\Configuration\Configuration($c['db.eng']); $configure->setMigrationsNamespace('EngMigration'); $configure->setMigrationsDirectory(DOCROOT . '/database-migration/versions'); $configure->registerMigrationsFromDirectory($configure->getMigrationsDirectory()); $configure->setMigrationsTableName('english_migration_versions'); $symfony_output = new \Symfony\Component\Console\Output\ConsoleOutput(); $symfony_output->setDecorated(true); $configure->setOutputWriter(new \Doctrine\DBAL\Migrations\OutputWriter(function ($msg) use($symfony_output) { $symfony_output->writeln($msg); })); $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array('db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($c['db.eng']), 'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(), 'configuration' => new \Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper($c['db.eng'], $configure))); $application->setHelperSet($helperSet); $application->addCommands(array(new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(), new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(), new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(), new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(), new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(), new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand())); $application->add(new \Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand()); $application->add(new \Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand()); $application->add(new \Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand()); $application->add(new \Doctrine\Bundle\DoctrineBundle\Command\ImportMappingDoctrineCommand()); $application->add(new \Eng\Core\Command\Util\VoiceManager($c)); $application->run();