public function execute()
 {
     try {
         $migrationsPath = $this->getArgument('migrations_path');
         $modelsPath = $this->getArgument('models_path');
         $yamlSchemaPath = $this->getArgument('yaml_schema_path');
         $migration = new Doctrine_Migration($migrationsPath);
         $result1 = false;
         if (!count($migration->getMigrationClasses())) {
             $builder = new Doctrine_Migration_Builder($migration);
             $result1 = $builder->generateMigrationsFromModels($modelsPath);
         }
         $diff = new Doctrine_Migration_Diff($modelsPath, $yamlSchemaPath, $migration);
         $changes = $diff->generateMigrationClasses();
         $numChanges = count($changes, true) - count($changes);
         $result = $result1 || $numChanges ? true : false;
     } catch (Exception $e) {
         $result = false;
     }
     if (!$result) {
         throw new Doctrine_Task_Exception('Could not generate migration classes from models' . (isset($e) ? ': ' . $e->getMessage() : null));
     } else {
         $this->notify('Generated migration classes successfully from models');
     }
 }
Ejemplo n.º 2
0
 public function execute()
 {
     $migrationsPath = $this->getArgument('migrations_path');
     $modelsPath = $this->getArgument('models_path');
     $yamlSchemaPath = $this->getArgument('yaml_schema_path');
     $migration = new Doctrine_Migration($migrationsPath);
     $diff = new Doctrine_Migration_Diff($modelsPath, $yamlSchemaPath, $migration);
     $changes = $diff->generateMigrationClasses();
     $numChanges = count($changes, true) - count($changes);
     if (!$numChanges) {
         throw new Doctrine_Task_Exception('Could not generate migration classes from difference');
     } else {
         $this->notify('Generated migration classes successfully from difference');
     }
 }
Ejemplo n.º 3
0
 public function testTest()
 {
     $from = dirname(__FILE__) . '/Diff/schema/from.yml';
     $to = dirname(__FILE__) . '/Diff/schema/to.yml';
     $migrationsPath = dirname(__FILE__) . '/Diff/migrations';
     Doctrine_Lib::makeDirectories($migrationsPath);
     $diff = new Doctrine_Migration_Diff($from, $to, $migrationsPath);
     $changes = $diff->generateChanges();
     $this->assertEqual($changes['dropped_tables']['homepage']['tableName'], 'homepage');
     $this->assertEqual($changes['created_tables']['blog_post']['tableName'], 'blog_post');
     $this->assertEqual($changes['created_columns']['profile']['user_id'], array('type' => 'integer', 'length' => 8));
     $this->assertEqual($changes['dropped_columns']['user']['homepage_id'], array('type' => 'integer', 'length' => 8));
     $this->assertEqual($changes['dropped_columns']['user']['profile_id'], array('type' => 'integer', 'length' => 8));
     $this->assertEqual($changes['changed_columns']['user']['username'], array('type' => 'string', 'length' => 255, 'unique' => true, 'notnull' => true));
     $this->assertEqual($changes['created_foreign_keys']['profile']['profile_user_id_user_id']['local'], 'user_id');
     $this->assertEqual($changes['created_foreign_keys']['blog_post']['blog_post_user_id_user_id']['local'], 'user_id');
     $this->assertEqual($changes['dropped_foreign_keys']['user']['user_profile_id_profile_id']['local'], 'profile_id');
     $this->assertEqual($changes['dropped_foreign_keys']['user']['user_homepage_id_homepage_id']['local'], 'homepage_id');
     $this->assertEqual($changes['created_indexes']['blog_post']['blog_post_user_id'], array('fields' => array('user_id')));
     $this->assertEqual($changes['created_indexes']['profile']['profile_user_id'], array('fields' => array('user_id')));
     $this->assertEqual($changes['dropped_indexes']['user']['is_active'], array('fields' => array('is_active')));
     $diff->generateMigrationClasses();
     $files = glob($migrationsPath . '/*.php');
     $this->assertEqual(count($files), 2);
     $this->assertTrue(strpos($files[0], '_version1.php'));
     $this->assertTrue(strpos($files[1], '_version2.php'));
     $code1 = file_get_contents($files[0]);
     $this->assertTrue(strpos($code1, 'this->dropTable'));
     $this->assertTrue(strpos($code1, 'this->createTable'));
     $this->assertTrue(strpos($code1, 'this->removeColumn'));
     $this->assertTrue(strpos($code1, 'this->addColumn'));
     $this->assertTrue(strpos($code1, 'this->changeColumn'));
     $code2 = file_get_contents($files[1]);
     $this->assertTrue(strpos($code2, 'this->dropForeignKey'));
     $this->assertTrue(strpos($code2, 'this->removeIndex'));
     $this->assertTrue(strpos($code2, 'this->addIndex'));
     $this->assertTrue(strpos($code2, 'this->createForeignKey'));
     Doctrine_Lib::removeDirectories($migrationsPath);
 }
Ejemplo n.º 4
0
 /**
  * Generate a set of migration classes by generating differences between two sets
  * of schema information
  *
  * @param  string $migrationsPath   Path to your Doctrine migration classes
  * @param  string $from             From schema information
  * @param  string $to               To schema information
  * @return array $changes
  */
 public static function generateMigrationsFromDiff($migrationsPath, $from, $to)
 {
     $diff = new Doctrine_Migration_Diff($from, $to, $migrationsPath);
     return $diff->generateMigrationClasses();
 }
Ejemplo n.º 5
0
 /**
  * Generates the migration classes
  * 
  * @param boolean $changesOnly
  * @return array
  */
 public function generateMigration($changesOnly = false)
 {
     $dm = Doctrine_Manager::getInstance();
     if (!$changesOnly && $dm->getAttribute(Doctrine_Core::ATTR_TBLNAME_FORMAT) != '%s') {
         throw new Exception("You can not create migrations from diff with a custom Doctrine attribute 'ATTR_TBLNAME_FORMAT'");
     }
     if (!$changesOnly && $dm->getAttribute(Doctrine_Core::ATTR_IDXNAME_FORMAT) != '%s_idx') {
         throw new Exception("You can not create migrations from diff with a custom Doctrine attribute 'ATTR_IDXNAME_FORMAT'");
     }
     if (!$changesOnly && $dm->getAttribute(Doctrine_Core::ATTR_SEQNAME_FORMAT) != '%s_seq') {
         throw new Exception("You can not create migrations from diff with a custom Doctrine attribute 'ATTR_SEQNAME_FORMAT'");
     }
     $this->_getModels($this->getFromPath());
     $this->_getSchema($this->getToPath());
     $changes = new Doctrine_Migration_Diff($this->getFromPath(), $this->getToPath(), $this->getMigrationPath());
     if ($changesOnly) {
         return $changes->generateChanges();
     } else {
         return $changes->generateMigrationClasses();
     }
 }