/** * @return boolean * @param string $onlyVersion */ public function run($onlyVersion = null) { try { $this->logAllStart(); $transaction = false; foreach ($this->getSteps() as $name => $step) { /* @var $step Nano_Migrate_Step */ if (Nano_Migrate_Version::exists($this->getDb(), $name)) { continue; } if (null !== $onlyVersion && $onlyVersion !== $name) { continue; } $this->logStepStart($name, $step); $this->getDb()->beginTransaction(); $transaction = true; $step->run($this->getDb()); $this->getDb()->commit(); Nano_Migrate_Version::add($this->getDb(), $name); $transaction = false; $this->logStepDone($name, $step); } $this->logAllDone(); } catch (Exception $e) { if (true === $transaction) { $this->getDb()->rollBack(); } throw $e; } return true; }
/** * @return string[] * @param Nano_Db $db * @param boolean $force */ public static function getAll(Nano_Db $db, $force = false) { if (null === self::$versions || true === $force) { self::$versions = $db->getAssoc('select version, id from ' . Nano_Migrate::VERSION_TABLE); } return self::$versions; }
public function testMigrateFromVersion() { $this->setVersion('04_0001'); $this->setVersion('04_0002'); Nano_Migrate_Version::getAll($this->db, true); $this->db->exec('create table migration_test(' . 'id integer primary key' . ', comment text' . ')'); $migrate = $this->createMigrate('from-version'); self::assertEquals(5, count($migrate->getSteps())); self::assertTrue($migrate->run()); Nano_Migrate_Version::getAll($this->db, true); self::assertTrue(Nano_Migrate_Version::exists($this->db, '04_0003')); self::assertTrue(Nano_Migrate_Version::exists($this->db, '04_0004')); self::assertTrue(Nano_Migrate_Version::exists($this->db, '04_0005')); self::assertEquals(6, $this->db->getCell('select count(*) from migration_test')); $rows = $this->db->query('select * from migration_test order by id', PDO::FETCH_OBJ)->fetchAll(); $expected = array('300' => '3rd migration', '301' => '3rd migration script', '400' => '4th migration', '401' => '4th migration script', '500' => '5th migration', '501' => '5th migration script'); foreach ($rows as $row) { $this->assertArrayHasKey($row->id, $expected); $this->assertEquals($expected[$row->id], $row->comment); } }