Exemple #1
0
/**
 * 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;
}