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'); } }
public function testTest() { $migration = new Doctrine_Migration(dirname(__FILE__) . '/DC292/migrations'); $diff = new Doctrine_Migration_Diff(dirname(__FILE__) . '/DC292/from.yml', dirname(__FILE__) . '/DC292/to.yml', $migration); $changes = $diff->generateChanges(); $this->assertEqual(2, count($changes['created_columns']['article'])); $this->assertTrue(isset($changes['created_columns']['article']['created_at'])); $this->assertTrue(isset($changes['created_columns']['article']['updated_at'])); }
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'); } }
public function testYAMLYAMLMigrationsWithPrefix() { $manager = Doctrine_Manager::getInstance(); $oldPrefix = $manager->getAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_Test'); $dir = dirname(__FILE__) . '/DC755/migrations'; if (!is_dir($dir)) { mkdir($dir, 0777, true); } $migration = new Doctrine_Migration($dir); $diff = new Doctrine_Migration_Diff(dirname(__FILE__) . '/DC755/dc755_from.yml', dirname(__FILE__) . '/DC755/dc755_to.yml', $migration); $changes = $diff->generateChanges(); $this->assertTrue(isset($changes['changed_columns']['755_test']['test_col'])); $this->assertFalse(isset($changes['created_tables']['755_test'])); $this->assertFalse(isset($changes['dropped_tables']['755_test'])); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, $oldPrefix); }
public function testMigrationWithBehavioursNotAddingUnexpectedChanges() { $manager = Doctrine_Manager::getInstance(); $oldPrefix = $manager->getAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_Test_DC880'); $dir = dirname(__FILE__) . '/DC880/migrations'; if (!is_dir($dir)) { mkdir($dir, 0777, true); } $migration = new Doctrine_Migration($dir); $diff = new Doctrine_Migration_Diff(dirname(__FILE__) . '/DC880/dc880_schema.yml', dirname(__FILE__) . '/DC880/dc880_schema.yml', $migration); $changes = $diff->generateChanges(); $foundChanges = array(); foreach ($changes as $kind => $models) { if (count($models)) { $foundChanges[$kind] = implode(',', array_keys($models)); } } $this->assertEqual($foundChanges, array()); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, $oldPrefix); }
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); }
/** * 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(); }
/** * Generate migrations from a Doctrine_Migration_Diff instance * * @param Doctrine_Migration_Diff $diff Instance to generate changes from * @return array $changes Array of changes produced from the diff */ public function generateMigrationsFromDiff(Doctrine_Migration_Diff $diff) { $changes = $diff->generateChanges(); $up = array(); $down = array(); if (!empty($changes['dropped_tables'])) { foreach ($changes['dropped_tables'] as $tableName => $table) { $up[] = $this->buildDropTable($table); $down[] = $this->buildCreateTable($table); } } if (!empty($changes['created_tables'])) { foreach ($changes['created_tables'] as $tableName => $table) { $up[] = $this->buildCreateTable($table); $down[] = $this->buildDropTable($table); } } if (!empty($changes['dropped_columns'])) { foreach ($changes['dropped_columns'] as $tableName => $removedColumns) { foreach ($removedColumns as $name => $column) { $up[] = $this->buildRemoveColumn($tableName, $name, $column); $down[] = $this->buildAddColumn($tableName, $name, $column); } } } if (!empty($changes['created_columns'])) { foreach ($changes['created_columns'] as $tableName => $addedColumns) { foreach ($addedColumns as $name => $column) { $up[] = $this->buildAddColumn($tableName, $name, $column); $down[] = $this->buildRemoveColumn($tableName, $name, $column); } } } if (!empty($changes['changed_columns'])) { foreach ($changes['changed_columns'] as $tableName => $changedColumns) { foreach ($changedColumns as $name => $column) { $up[] = $this->buildChangeColumn($tableName, $name, $column); } } } if (!empty($up) || !empty($down)) { $up = implode("\n", $up); $down = implode("\n", $down); $className = 'Version' . $this->migration->getNextMigrationClassVersion(); $this->generateMigrationClass($className, array(), $up, $down); } $up = array(); $down = array(); if (!empty($changes['dropped_foreign_keys'])) { foreach ($changes['dropped_foreign_keys'] as $tableName => $droppedFks) { if (!empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) { continue; } foreach ($droppedFks as $name => $foreignKey) { $up[] = $this->buildDropForeignKey($tableName, $foreignKey); $down[] = $this->buildCreateForeignKey($tableName, $foreignKey); } } } if (!empty($changes['dropped_indexes'])) { foreach ($changes['dropped_indexes'] as $tableName => $removedIndexes) { if (!empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) { continue; } foreach ($removedIndexes as $name => $index) { $up[] = $this->buildRemoveIndex($tableName, $name, $index); $down[] = $this->buildAddIndex($tableName, $name, $index); } } } if (!empty($changes['created_foreign_keys'])) { foreach ($changes['created_foreign_keys'] as $tableName => $createdFks) { if (!empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) { continue; } foreach ($createdFks as $name => $foreignKey) { $up[] = $this->buildCreateForeignKey($tableName, $foreignKey); $down[] = $this->buildDropForeignKey($tableName, $foreignKey); } } } if (!empty($changes['created_indexes'])) { foreach ($changes['created_indexes'] as $tableName => $addedIndexes) { if (!empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) { continue; } foreach ($addedIndexes as $name => $index) { if (isset($changes['created_tables'][$tableName]['options']['indexes'][$name])) { continue; } $up[] = $this->buildAddIndex($tableName, $name, $index); $down[] = $this->buildRemoveIndex($tableName, $name, $index); } } } if (!empty($up) || !empty($down)) { $up = implode("\n", $up); $down = implode("\n", $down); $className = 'Version' . $this->migration->getNextMigrationClassVersion(); $this->generateMigrationClass($className, array(), $up, $down); } return $changes; }
/** * 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(); } }