/** @internal */
 public function __testbench_database_setup($connection, \Nette\DI\Container $container, $persistent = FALSE)
 {
     $config = $container->parameters['testbench'];
     $this->__testbench_databaseName = $config['dbprefix'] . getenv(\Tester\Environment::THREAD);
     $this->__testbench_database_drop($connection, $container);
     $this->__testbench_database_create($connection, $container);
     foreach ($config['sqls'] as $file) {
         \Kdyby\Doctrine\Dbal\BatchImport\Helpers::loadFromFile($connection, $file);
     }
     if ($config['migrations'] === TRUE) {
         if (class_exists(\Zenify\DoctrineMigrations\Configuration\Configuration::class)) {
             /** @var \Zenify\DoctrineMigrations\Configuration\Configuration $migrationsConfig */
             $migrationsConfig = $container->getByType(\Zenify\DoctrineMigrations\Configuration\Configuration::class);
             $migrationsConfig->__construct($container, $connection);
             $migrationsConfig->registerMigrationsFromDirectory($migrationsConfig->getMigrationsDirectory());
             $migration = new \Doctrine\DBAL\Migrations\Migration($migrationsConfig);
             $migration->migrate($migrationsConfig->getLatestVersion());
         }
     }
     if ($persistent === FALSE) {
         register_shutdown_function(function () use($connection, $container) {
             $this->__testbench_database_drop($connection, $container);
         });
     }
 }
Example #2
0
 public function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
 {
     $configuration = $this->getMigrationConfiguration($input, $output);
     $migration = new \Doctrine\DBAL\Migrations\Migration($configuration);
     $this->outputHeader($configuration, $output);
     $noInteraction = $input->getOption('no-interaction') ? true : false;
     $executedMigrations = $configuration->getMigratedVersions();
     $availableMigrations = $configuration->getAvailableVersions();
     $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
     if ($executedUnavailableMigrations) {
         $output->writeln(sprintf('<error>WARNING! You have %s previously executed migrations in the database that are not registered migrations.  You will need to use the migrations tool directly to fix this.</error>', count($executedUnavailableMigrations)));
         return 1;
     }
     $noInteraction = $input->getOption('no-interaction') ? true : false;
     if (!$noInteraction) {
         $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>This migration may result in lost data, you should test it first.  Are you sure you want to continue? (y/n)</question>', false);
         if (!$confirmation) {
             $output->writeln('<error>Migration cancelled!</error>');
             return 1;
         }
     }
     $sql = $migration->migrate(null, false);
     if (!$sql) {
         $output->writeln('<comment>No migrations to execute.</comment>');
     }
 }
Example #3
0
 /**
  * executes the doctrine migrations
  *
  * @param bool $dryRun flag if only a dryrun should be executed
  *
  * @return string
  */
 public function executeMigrations($dryRun = false)
 {
     $configuration = $this->getMigrationConfiguration();
     $migration = new \Doctrine\DBAL\Migrations\Migration($configuration);
     $migration->migrate(null, $dryRun);
     $output = '';
     foreach ($this->output as $line) {
         $line = strip_tags($line);
         if (strpos($line, '  ++ migrating ') !== false || strpos($line, '  -- reverting ') !== false) {
             $output .= substr($line, -15);
         }
     }
     return $output;
 }
Example #4
0
 private function migrate()
 {
     $em = $this->getDoctrine()->getEntityManager();
     $configuration = $this->getMigrationsConfiguration();
     $migration = new \Doctrine\DBAL\Migrations\Migration($configuration);
     $from = $configuration->getCurrentVersion();
     $to = $configuration->getLatestVersion();
     $migrations = $configuration->getMigrations();
     foreach ($migrations as $version) {
         if ($version->getVersion() > $from) {
             $migration->migrate($version->getVersion());
             $this->loadFixtures($version->getVersion());
         }
     }
 }
 /**
  * Execute all new migrations, up to $version if given.
  *
  * If $outputPathAndFilename is given, the SQL statements will be written to the given file instead of executed.
  *
  * @param string $version The version to migrate to
  * @param string $outputPathAndFilename A file to write SQL to, instead of executing it
  * @param boolean $dryRun Whether to do a dry run or not
  * @param boolean $quiet Whether to do a quiet run or not
  * @return string
  */
 public function executeMigrations($version = null, $outputPathAndFilename = null, $dryRun = false, $quiet = false)
 {
     $configuration = $this->getMigrationConfiguration();
     $migration = new \Doctrine\DBAL\Migrations\Migration($configuration);
     if ($outputPathAndFilename !== null) {
         $migration->writeSqlFile($outputPathAndFilename, $version);
     } else {
         $migration->migrate($version, $dryRun);
     }
     if ($quiet === true) {
         $output = '';
         foreach ($this->output as $line) {
             $line = strip_tags($line);
             if (strpos($line, '  ++ migrating ') !== false || strpos($line, '  -- reverting ') !== false) {
                 $output .= substr($line, -15);
             }
         }
         return $output;
     } else {
         return strip_tags(implode(PHP_EOL, $this->output));
     }
 }
Example #6
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;
        }
    }
}
Example #7
0
 /**
  * @see https://github.com/doctrine/migrations/issues/61
  * @group regresion
  * @dataProvider provideTestMigrationNames
  */
 public function testMigrateExecutesOlderVersionsThatHaveNetYetBeenMigrated(array $migrations)
 {
     foreach ($migrations as $key => $class) {
         $migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
         $this->config->registerMigration($key, $class);
         $sql = $migration->migrate();
         $this->assertCount(1, $sql, 'should have executed one migration');
     }
 }
Example #8
0
function initializeDatabase(\Eccube\Application $app)
{
    // Get an instance of your entity manager
    $entityManager = $app['orm.em'];
    $pdo = $entityManager->getConnection()->getWrappedConnection();
    // Clear Doctrine to be safe
    $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
    $entityManager->clear();
    gc_collect_cycles();
    // Schema Tool to process our entities
    $tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
    $classes = $entityManager->getMetaDataFactory()->getAllMetaData();
    // Drop all classes and re-build them for each test case
    out('Dropping database schema...', 'info');
    $tool->dropSchema($classes);
    out('Creating database schema...', 'info');
    $tool->createSchema($classes);
    out('Database schema created successfully!', 'success');
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($app['db']);
    $config->setMigrationsNamespace('DoctrineMigrations');
    $migrationDir = __DIR__ . '/src/Eccube/Resource/doctrine/migration';
    $config->setMigrationsDirectory($migrationDir);
    $config->registerMigrationsFromDirectory($migrationDir);
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
    $migration->migrate();
    out('Database migration successfully!', 'success');
    $login_id = getenv('ADMIN_USER');
    $login_password = getenv('ADMIN_PASS');
    $passwordEncoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']);
    $salt = \Eccube\Util\Str::random(32);
    $encodedPassword = $passwordEncoder->encodePassword($login_password, $salt);
    out('Creating admin accounts...', 'info');
    $sql = "INSERT INTO dtb_member (member_id, login_id, password, salt, work, del_flg, authority, creator_id, rank, update_date, create_date,name,department) VALUES (2, :login_id, :admin_pass , :salt , '1', '0', '0', '1', '1', current_timestamp, current_timestamp,'管理者', 'EC-CUBE SHOP');";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(':login_id' => $login_id, ':admin_pass' => $encodedPassword, ':salt' => $salt));
    $stmt->closeCursor();
    $shop_name = getenv('SHOP_NAME');
    $admin_mail = getenv('ADMIN_MAIL');
    $sql = "INSERT INTO dtb_base_info (id, shop_name, email01, email02, email03, email04, update_date, option_product_tax_rule) VALUES (1, :shop_name, :admin_mail1, :admin_mail2, :admin_mail3, :admin_mail4, current_timestamp, 0)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(':shop_name' => $shop_name, ':admin_mail1' => $admin_mail, ':admin_mail2' => $admin_mail, ':admin_mail3' => $admin_mail, ':admin_mail4' => $admin_mail));
    $stmt->closeCursor();
}
Example #9
0
 /**
  * @param string|null $toVersion
  *
  * @return array
  * @throws \Doctrine\DBAL\Migrations\MigrationException
  */
 protected function migrateDb($toVersion = null)
 {
     // the order is important
     $updateInfo = array();
     // 1. reset/clear opcode cache
     $this->resetOpCodeCache();
     // 2. migrate database
     $configuration = $this->getDoctrineMigrationConfig();
     // Information ermitteln
     $currentVersion = $configuration->getCurrentVersion();
     $latestVersion = $configuration->getLatestVersion();
     if ($toVersion === null) {
         $toVersion = $latestVersion;
     }
     // DB-Migration durchfuehren
     $migration = new \Doctrine\DBAL\Migrations\Migration($configuration);
     $migration->migrate($toVersion, false);
     $versionInfo = array('current' => (string) $currentVersion, 'latest' => (string) $latestVersion, 'migrateto' => (string) $toVersion);
     // log migrate
     Registry::getActionLogger()->logAction(self::SPACE_MIGRATED_ACTION, $versionInfo);
     return array('version' => $versionInfo);
 }
 public function testAddSql()
 {
     $this->config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrateAddSqlTest');
     $migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
     $migration->migrate(1);
     $migrations = $this->config->getMigrations();
     $this->assertTrue($migrations[1]->isMigrated());
     $schema = $this->config->getConnection()->getSchemaManager()->createSchema();
     $this->assertTrue($schema->hasTable('test_add_sql_table'));
     $check = $this->config->getConnection()->fetchAll('select * from test_add_sql_table');
     $this->assertNotEmpty($check);
     $this->assertEquals('test', $check[0]['test']);
     $migration->migrate(0);
     $this->assertFalse($migrations[1]->isMigrated());
     $schema = $this->config->getConnection()->getSchemaManager()->createSchema();
     $this->assertFalse($schema->hasTable('test_add_sql_table'));
 }
Example #11
0
 public function testMigrateToCurrentVersionReturnsEmpty()
 {
     $this->config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrateAddSqlTest');
     $this->config->registerMigration(2, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateFurther');
     $migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
     $migration->migrate();
     $sql = $migration->migrate();
     $this->assertEquals(array(), $sql);
 }
Example #12
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;
    // 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;
}