LazyRecord\Migration\MigrationRunner::runUpgrade PHP Method

runUpgrade() public method

Run upgrade scripts.
public runUpgrade ( Connection $conn, BaseDriver $driver, array $scripts = null )
$conn LazyRecord\Connection
$driver SQLBuilder\Driver\BaseDriver
$scripts array
    public function runUpgrade(Connection $conn, BaseDriver $driver, array $scripts = null)
    {
        if (!$scripts) {
            $scripts = $this->getUpgradeScripts($conn, $driver);
            if (count($scripts) == 0) {
                $this->logger->info('No migration script found.');
                return;
            }
        }
        $this->logger->info('Found ' . count($scripts) . ' migration scripts to run upgrade!');
        try {
            $this->logger->info('Begining transaction...');
            $conn->beginTransaction();
            foreach ($scripts as $script) {
                $migration = new $script($conn, $driver, $this->logger);
                $migration->upgrade();
                $this->updateLastMigrationId($conn, $driver, $script::getId());
            }
            $this->logger->info('Committing...');
            $conn->commit();
        } catch (Exception $e) {
            $this->logger->error(get_class($e) . ' was thrown: ' . $e->getMessage());
            $this->logger->error('Rolling back ...');
            $conn->rollback();
            $this->logger->error('Recovered, escaping...');
            throw $e;
        }
    }

Usage Example

 public function testMigrationByDiff()
 {
     $this->conn->query('DROP TABLE IF EXISTS users');
     $this->conn->query('DROP TABLE IF EXISTS test');
     $this->conn->query('CREATE TABLE users (account VARCHAR(128) UNIQUE)');
     if (!file_exists('tests/migrations_testing')) {
         mkdir('tests/migrations_testing');
     }
     $generator = new MigrationGenerator(Console::getInstance()->getLogger(), 'tests/migrations_testing');
     ok(class_exists('TestApp\\Model\\UserSchema', true));
     $finder = new SchemaFinder();
     $finder->find();
     list($class, $path) = $generator->generateWithDiff('DiffMigration', $this->getDriverType(), ["users" => new TestApp\Model\UserSchema()], '20120101');
     require_once $path;
     ok($class::getId());
     /*
     $userSchema = new TestApp\Model\UserSchema;
     $column = $userSchema->getColumn('account');
     */
     // run migration
     $runner = new MigrationRunner($this->logger, $this->getDriverType());
     $runner->resetMigrationId($this->conn, $this->queryDriver);
     $runner->load('tests/migrations_testing');
     // XXX: PHPUnit can't run this test in separated unit test since
     // there is a bug of serializing the global array, this assertion will get 5 instead of the expected 1.
     $scripts = $runner->loadMigrationScripts();
     $this->assertNotEmpty($scripts);
     // $this->assertCount(1, $scripts);
     // $this->expectOutputRegex('#DiffMigration_1325347200#');
     $runner->runUpgrade($this->conn, $this->queryDriver, [$class]);
     # echo file_get_contents($path);
     unlink($path);
     $this->conn->query('DROP TABLE IF EXISTS users');
 }
All Usage Examples Of LazyRecord\Migration\MigrationRunner::runUpgrade