public function test_execute()
 {
     $this->service->expects($this->once())->method('get_migration_contents')->will($this->returnValue('the query'));
     $this->service->expects($this->once())->method('run_query')->with($this->equalTo('the query'))->will($this->returnValue(true));
     $this->service->execute();
     $this->assertEquals(true, SchemaMigration::find_by_version('20140319-new_migration')->exists());
 }
 /**
  * Get all available migrations for a scope from file system.
  * If direction equals "down" then additionally check against migrated versions in schema_migration
  * to only migrate existing ones down.
  * This makes it possible to insert new migrations in between already existing and migrated ones.
  *
  * @param string $scope The scope (app or plugin name) that you want to get migrations from.
  * @param string $direction The migration direction for which to order the available migrations array.
  * @return array all available migrations for this scope
  */
 protected function _getAvailableMigrations($scope, $direction = 'up')
 {
     if (!isset($this->_migrations[$scope]) || empty($this->_migrations[$scope])) {
         $folder = $this->_getMigrationsFolder($scope);
         if (!$folder) {
             return array();
         }
         $this->_migrations[$scope] = array();
         foreach ($folder->find("^\\d+_.*\\.php\$", true) as $mf) {
             if (preg_match("/^(\\d+)_(.*)\\.php\$/", $mf, $matches) === 1) {
                 list($fileName, $version, $name) = $matches;
                 $this->_migrations[$scope][$version] = array('path' => $folder->path, 'fileName' => $fileName, 'className' => Inflector::camelize($name));
             }
         }
     }
     $migrations = $this->_migrations[$scope];
     if ($direction === 'down') {
         $migrations = array_reverse($migrations);
         $existingMigrations = $this->SchemaMigration->getMigrationsForDown($scope);
         foreach (array_keys($migrations) as $key) {
             if (!array_key_exists($key, $existingMigrations)) {
                 unset($migrations[$key]);
             }
         }
     }
     return $migrations;
 }
 function execute()
 {
     $migrations = array();
     foreach ($this->available_migrations as $migration) {
         if (!in_array($migration, $this->schema_migrations)) {
             $migrations[] = $migration;
         }
     }
     sort($migrations);
     foreach ($migrations as $migration) {
         $query = $this->get_migration_contents($migration);
         if ($query === false) {
             throw new Exception("Could not read migration {$migration}.");
         }
         if ($this->run_query($query) === false) {
             throw new Exception("Failed to execute migration {$migration}. " . pg_last_error());
         }
         $schema_migration = new SchemaMigration();
         $schema_migration->version = $migration;
         $schema_migration->save();
     }
 }