diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 9d0b03aad..d78355649 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -826,13 +826,13 @@ final class Doctrine * * Migrate database to specified $to version. Migrates from current to latest if you do not specify. * - * @param string $directory Directory which contains your migration classes + * @param string $migrationsPath Path to migrations directory which contains your migration classes * @param string $to Version you wish to migrate to. * @return void */ - public static function migrate($directory, $to = null) + public static function migrate($migrationsPath, $to = null) { - $migration = new Doctrine_Migration($directory); + $migration = new Doctrine_Migration($migrationsPath); return $migration->migrate($to); } @@ -843,26 +843,64 @@ final class Doctrine * Generate new migration class skeleton * * @param string $className Name of the Migration class to generate - * @param string $directory Directory which contains your migration classes + * @param string $migrationsPath Path to directory which contains your migration classes * @package default */ - public static function generateMigrationClass($className, $directory) + public static function generateMigrationClass($className, $migrationsPath) { - $migration = new Doctrine_Migration($directory); - $next = (string) $migration->getNextVersion(); + $builder = new Doctrine_Migration_Builder($migrationsPath); - $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . self::tableize($className) . '.class.php'; - $path = $directory . DIRECTORY_SEPARATOR . $fileName; + return $builder->generateMigrationClass($className); + } + + /** + * generateMigrationsFromDb + * + * @param string $migrationsPath + * @return void + */ + public function generateMigrationsFromDb($migrationsPath) + { + $builder = new Doctrine_Migration_Builder($migrationsPath); - $code = 'generateMigrationsFromDb(); + } + + /** + * generateMigrationsFromModels + * + * @param string $migrationsPath + * @param string $modelsPath + * @return void + */ + public function generateMigrationsFromModels($migrationsPath, $modelsPath = null) + { + $builder = new Doctrine_Migration_Builder($migrationsPath); - file_put_contents($path, $code); + return $builder->generateMigrationsFromModels($modelsPath); + } + + /** + * getTable + * + * @param string $tableName + * @return void + */ + public function getTable($tableName) + { + return Doctrine_Manager::table($tableName); + } + + /** + * connection + * + * @param string $adapter + * @param string $name + * @return void + */ + public function connection($adapter, $name = null) + { + return Doctrine_Manager::connection($adapter, $name); } /** diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 4213b02bf..e3b5d1a46 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -114,6 +114,7 @@ class Doctrine_Export extends Doctrine_Connection_Module public function dropIndexSql($table, $name) { $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); + return 'DROP INDEX ' . $name; } /** @@ -128,6 +129,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { $table = $this->conn->quoteIdentifier($table); $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)); + return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name); } /** @@ -330,7 +332,9 @@ class Doctrine_Export extends Doctrine_Connection_Module */ public function createConstraint($table, $name, $definition) { - return $this->conn->exec($this->createConstraintSql($table, $name, $definition)); + $sql = $this->createConstraintSql($table, $name, $definition); + + return $this->conn->exec($sql); } /** * create a constraint on a table @@ -467,7 +471,9 @@ class Doctrine_Export extends Doctrine_Connection_Module */ public function createForeignKey($table, array $definition) { - return $this->conn->execute($this->createForeignKeySql($table, $definition)); + $sql = $this->createForeignKeySql($table, $definition); + + return $this->conn->execute($sql); } /** * alter an existing table diff --git a/lib/Doctrine/Import/Builder.php b/lib/Doctrine/Import/Builder.php index bfe17f390..1221cfd41 100644 --- a/lib/Doctrine/Import/Builder.php +++ b/lib/Doctrine/Import/Builder.php @@ -158,7 +158,6 @@ class Doctrine_Import_Builder %s } END; - } /* diff --git a/lib/Doctrine/Migration.php b/lib/Doctrine/Migration.php index 5fb126bb0..e9a3f0037 100644 --- a/lib/Doctrine/Migration.php +++ b/lib/Doctrine/Migration.php @@ -49,7 +49,8 @@ class Doctrine_Migration ), $migrationTableName = 'migration_version', $migrationClassesDirectory = array(), - $migrationClasses = array(); + $migrationClasses = array(), + $loadedMigrations = array(); /** * construct @@ -104,35 +105,34 @@ class Doctrine_Migration return $this->migrationClasses; } - $directory = $this->migrationClassesDirectory; - $classes = get_declared_classes(); - $loadedClasses = array(); - - if ($directory !== null) { - foreach ((array) $directory as $dir) { + + if ($this->migrationClassesDirectory !== null) { + foreach ((array) $this->migrationClassesDirectory as $dir) { $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($it as $file) { $e = explode('.', $file->getFileName()); if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { - require_once $file->getPathName(); - $requiredClass = array_diff(get_declared_classes(), $classes); - $requiredClass = end($requiredClass); - - $loadedClasses[$requiredClass] = $file->getFileName(); - + if (!in_array($file->getFileName(), $this->loadedMigrations)) { + require_once($file->getPathName()); + + $requiredClass = array_diff(get_declared_classes(), $classes); + $requiredClass = end($requiredClass); + + if ($requiredClass) { + $this->loadedMigrations[$requiredClass] = $file->getFileName(); + } + } } } } } - $classes = $loadedClasses; $parent = new ReflectionClass('Doctrine_Migration'); - $loadedClasses = array(); - foreach ($classes as $name => $fileName) { + foreach ($this->loadedMigrations as $name => $fileName) { $class = new ReflectionClass($name); while ($class->isSubclassOf($parent)) { @@ -150,11 +150,9 @@ class Doctrine_Migration $e = explode('_', $fileName); $classMigrationNum = (int) $e[0]; - $loadedClasses[$classMigrationNum] = array('className' => $name, 'fileName' => $fileName); + $this->migrationClasses[$classMigrationNum] = array('className' => $name, 'fileName' => $fileName); } - $this->migrationClasses = $loadedClasses; - return $this->migrationClasses; } @@ -280,6 +278,7 @@ class Doctrine_Migration protected function doMigrateStep($direction, $num) { $migrate = $this->getMigrationClass($num); + $migrate->doMigrate($direction); } @@ -327,7 +326,7 @@ class Doctrine_Migration } if ($from == $to) { - throw new Doctrine_Migration_Exception('Already up-to-date'); + throw new Doctrine_Migration_Exception('Already at version: ' . $to); } $direction = $from > $to ? 'down':'up'; diff --git a/lib/Doctrine/Migration/Builder.php b/lib/Doctrine/Migration/Builder.php new file mode 100644 index 000000000..4858475e0 --- /dev/null +++ b/lib/Doctrine/Migration/Builder.php @@ -0,0 +1,315 @@ +. + */ + +/** + * Doctrine_Migration_Builder + * + * @package Doctrine + * @subpackage Migration + * @author Konsta Vesterinen + * @author Jonathan H. Wage + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision: 2939 $ + */ +class Doctrine_Migration_Builder +{ + /** + * migrationsPath + * + * The path to your migration classes directory + * + * @var string + */ + private $migrationsPath = ''; + + /** + * suffix + * + * File suffix to use when writing class definitions + * + * @var string $suffix + */ + private $suffix = '.class.php'; + + /** + * tpl + * + * Class template used for writing classes + * + * @var $tpl + */ + private static $tpl; + + /** + * __construct + * + * @return void + */ + public function __construct($migrationsPath = null) + { + if ($migrationsPath) { + $this->setMigrationsPath($migrationsPath); + } + + $this->loadTemplate(); + } + + /** + * setMigrationsPath + * + * @param string path the path where migration classes are stored and being generated + * @return + */ + public function setMigrationsPath($path) + { + if ( ! file_exists($path)) { + mkdir($path, 0777); + } + + $this->migrationsPath = $path; + } + + /** + * getMigrationsPath + * + * @return string the path where migration classes are stored and being generated + */ + public function getMigrationsPath() + { + return $this->migrationsPath; + } + + /** + * loadTemplate + * + * Loads the class template used for generating classes + * + * @return void + */ + protected function loadTemplate() + { + if (isset(self::$tpl)) { + return; + } + + self::$tpl =<<generateMigrationsFromModels($directory); + + exec('rm -rf ' . $directory); + + return $result; + } + + /** + * generateMigrationsFromModels + * + * @param string $modelsPath + * @return void + */ + public function generateMigrationsFromModels($modelsPath = null) + { + if ($modelsPath) { + $models = Doctrine::loadModels($modelsPath); + } else { + $models = Doctrine::getLoadedModels(); + } + + $foreignKeys = array(); + + foreach ($models as $model) { + $export = Doctrine::getTable($model)->getExportableFormat(); + + $foreignKeys[$export['tableName']] = $export['options']['foreignKeys']; + + $up = $this->buildCreateTable($export); + $down = $this->buildDropTable($export); + + $className = 'Add'.Doctrine::classify($export['tableName']); + + $this->generateMigrationClass($className, array(), $up, $down); + } + + $className = 'ApplyForeignKeyConstraints'; + + $up = ''; + $down = ''; + foreach ($foreignKeys as $tableName => $definitions) { + $tableForeignKeyNames[$tableName] = array(); + + foreach ($definitions as $definition) { + $definition['name'] = $tableName . '_' . $definition['foreignTable'] . '_' . $definition['local'] . '_' . $definition['foreign']; + + $up .= $this->buildCreateForeignKey($tableName, $definition); + $down .= $this->buildDropForeignKey($tableName, $definition); + } + } + + $this->generateMigrationClass($className, array(), $up, $down); + } + + /** + * buildCreateForeignKey + * + * @param string $tableName + * @param string $definition + * @return void + */ + public function buildCreateForeignKey($tableName, $definition) + { + return "\t\t\$this->createForeignKey('" . $tableName . "', " . $this->dataToPhpCode($definition) . ");"; + } + + /** + * buildDropForeignKey + * + * @param string $tableName + * @param string $definition + * @return void + */ + public function buildDropForeignKey($tableName, $definition) + { + return "\t\t\$this->dropForeignKey('" . $tableName . "', '" . $definition['name'] . "');\n"; + } + + /** + * buildCreateTable + * + * @param string $tableData + * @return void + */ + public function buildCreateTable($tableData) + { + $code = "\t\t\$this->createTable('" . $tableData['tableName'] . "', "; + + $code .= $this->dataToPhpCode($tableData['columns']) . ", "; + + $code .= $this->dataToPhpCode(array('indexes' => $tableData['options']['indexes'], 'primary' => $tableData['options']['primary'])); + + $code .= ");"; + + return $code; + } + + /** + * buildDropTable + * + * @param string $tableData + * @return void + */ + public function buildDropTable($tableData) + { + return "\t\t\$this->dropTable('" . $tableData['tableName'] . "');"; + } + + /** + * dataToPhpCode + * + * @param string $data + * @return void + */ + public function dataToPhpCode($data) + { + ob_start(); + var_export($data); + $results = ob_get_contents(); + ob_end_clean(); + + return $results; + } + + /** + * generateMigrationClass + * + * @package default + */ + public function generateMigrationClass($className, $options = array(), $up = null, $down = null, $return = false) + { + if ($return || !$this->getMigrationsPath()) { + return $this->buildMigrationClass($className, null, $options, $up, $down); + } else { + if (!$this->getMigrationsPath()) { + throw new Doctrine_Migration_Exception('You must specify the path to your migrations.'); + } + + $migration = new Doctrine_Migration($this->getMigrationsPath()); + $next = (string) $migration->getNextVersion(); + + $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . Doctrine::tableize($className) . $this->suffix; + + $class = $this->buildMigrationClass($className, $fileName, $options, $up, $down); + + $path = $this->getMigrationsPath() . DIRECTORY_SEPARATOR . $fileName; + + file_put_contents($path, $class); + } + } + + /** + * buildMigrationClass + * + * @package default + */ + public function buildMigrationClass($className, $fileName = null, $options = array(), $up = null, $down = null) + { + $extends = isset($options['extends']) ? $options['extends']:'Doctrine_Migration'; + + $content = 'arguments[$name]) && $this->arguments[$name]) { + if (isset($this->arguments[$name]) && $this->arguments[$name] !== null) { return $this->arguments[$name]; } else { return $default; diff --git a/lib/Doctrine/Task/GenerateMigrationsFromDb.php b/lib/Doctrine/Task/GenerateMigrationsFromDb.php new file mode 100644 index 000000000..aa27789a2 --- /dev/null +++ b/lib/Doctrine/Task/GenerateMigrationsFromDb.php @@ -0,0 +1,43 @@ +. + */ + +/** + * Doctrine_Task_GenerateMigrationsFromDb + * + * @package Doctrine + * @subpackage Task + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision: 2761 $ + * @author Jonathan H. Wage + */ +class Doctrine_Task_GenerateMigrationsFromDb extends Doctrine_Task +{ + public $description = 'Generate migration classes for an existing database', + $requiredArguments = array('migrations_path' => 'Specify the complete path to your migration classes folder.'), + $optionalArguments = array(); + + public function execute() + { + Doctrine::generateMigrationsFromDb($this->getArgument('migrations_path')); + } +} \ No newline at end of file diff --git a/lib/Doctrine/Task/GenerateMigrationsFromModels.php b/lib/Doctrine/Task/GenerateMigrationsFromModels.php new file mode 100644 index 000000000..6824fc28b --- /dev/null +++ b/lib/Doctrine/Task/GenerateMigrationsFromModels.php @@ -0,0 +1,44 @@ +. + */ + +/** + * Doctrine_Task_GenerateMigrationsFromModels + * + * @package Doctrine + * @subpackage Task + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision: 2761 $ + * @author Jonathan H. Wage + */ +class Doctrine_Task_GenerateMigrationsFromModels extends Doctrine_Task +{ + public $description = 'Generate migration classes for an existing set of models', + $requiredArguments = array('migrations_path' => 'Specify the path to your migration classes folder.', + 'models_path' => 'Specify the path to your doctrine models folder.'), + $optionalArguments = array(); + + public function execute() + { + Doctrine::generateMigrationsFromModels($this->getArgument('migrations_path'), $this->getArgument('models_path')); + } +} \ No newline at end of file diff --git a/lib/Doctrine/Task/Migrate.php b/lib/Doctrine/Task/Migrate.php index d2fdaedd1..a5c8df308 100644 --- a/lib/Doctrine/Task/Migrate.php +++ b/lib/Doctrine/Task/Migrate.php @@ -33,11 +33,11 @@ class Doctrine_Task_Migrate extends Doctrine_Task { public $description = 'Migrate database to latest version or the specified version', - $requiredArguments = array(), + $requiredArguments = array('migrations_path' => 'Specify path to your migrations directory.'), $optionalArguments = array('version' => 'Version to migrate to. If you do not specify, the db will be migrated from the current version to the latest.'); public function execute() { - Doctrine::migrate($this->getArgument('version')); + Doctrine::migrate($this->getArgument('migrations_path'), $this->getArgument('version')); } } \ No newline at end of file diff --git a/sandbox/migrations/001_add_adult.class.php b/sandbox/migrations/001_add_adult.class.php new file mode 100644 index 000000000..3f52a4316 --- /dev/null +++ b/sandbox/migrations/001_add_adult.class.php @@ -0,0 +1,42 @@ +createTable('adult', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'contact_id' => + array ( + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('adult'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/002_add_car.class.php b/sandbox/migrations/002_add_car.class.php new file mode 100644 index 000000000..74a7de623 --- /dev/null +++ b/sandbox/migrations/002_add_car.class.php @@ -0,0 +1,37 @@ +createTable('car', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('car'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/003_add_child.class.php b/sandbox/migrations/003_add_child.class.php new file mode 100644 index 000000000..07c65aa57 --- /dev/null +++ b/sandbox/migrations/003_add_child.class.php @@ -0,0 +1,42 @@ +createTable('child', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'adult_id' => + array ( + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('child'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/004_add_contact.class.php b/sandbox/migrations/004_add_contact.class.php new file mode 100644 index 000000000..b9757934b --- /dev/null +++ b/sandbox/migrations/004_add_contact.class.php @@ -0,0 +1,37 @@ +createTable('contact', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('contact'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/005_add_dog.class.php b/sandbox/migrations/005_add_dog.class.php new file mode 100644 index 000000000..760fa202b --- /dev/null +++ b/sandbox/migrations/005_add_dog.class.php @@ -0,0 +1,42 @@ +createTable('dog', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'user_id' => + array ( + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('dog'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/006_add_entity.class.php b/sandbox/migrations/006_add_entity.class.php new file mode 100644 index 000000000..5226c3a3d --- /dev/null +++ b/sandbox/migrations/006_add_entity.class.php @@ -0,0 +1,32 @@ +createTable('entity', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('entity'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/007_add_groups.class.php b/sandbox/migrations/007_add_groups.class.php new file mode 100644 index 000000000..4e202c175 --- /dev/null +++ b/sandbox/migrations/007_add_groups.class.php @@ -0,0 +1,38 @@ +createTable('groups', array ( + 'id' => + array ( + 'notnull' => true, + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('groups'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/008_add_self_reference.class.php b/sandbox/migrations/008_add_self_reference.class.php new file mode 100644 index 000000000..3b10ad78e --- /dev/null +++ b/sandbox/migrations/008_add_self_reference.class.php @@ -0,0 +1,57 @@ +createTable('self_reference', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'name' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'user_id1' => + array ( + 'type' => 'integer', + 'length' => 11, + ), + 'user_id2' => + array ( + 'type' => 'integer', + 'length' => 11, + ), + 'parent_self_reference_id' => + array ( + 'type' => 'integer', + 'length' => 11, + ), + 'parent_self_reference_id2' => + array ( + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('self_reference'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/009_add_user.class.php b/sandbox/migrations/009_add_user.class.php new file mode 100644 index 000000000..488e4fb08 --- /dev/null +++ b/sandbox/migrations/009_add_user.class.php @@ -0,0 +1,60 @@ +createTable('user', array ( + 'id' => + array ( + 'primary' => true, + 'autoincrement' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'username' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'hair_color' => + array ( + 'type' => 'string', + 'length' => 255, + ), + 'contact_id' => + array ( + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + 'name_x' => + array ( + 'fields' => + array ( + 'username' => + array ( + 'sorting' => 'ASC', + 'length' => '11', + 'primary' => true, + ), + ), + 'type' => 'unique', + ), + ), + 'primary' => + array ( + 0 => 'id', + ), +)); + } + + public function down() + { + $this->dropTable('user'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/010_add_user_car.class.php b/sandbox/migrations/010_add_user_car.class.php new file mode 100644 index 000000000..aa2acd412 --- /dev/null +++ b/sandbox/migrations/010_add_user_car.class.php @@ -0,0 +1,38 @@ +createTable('user_car', array ( + 'user_id' => + array ( + 'primary' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'car_id' => + array ( + 'primary' => true, + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'user_id', + 1 => 'car_id', + ), +)); + } + + public function down() + { + $this->dropTable('user_car'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/011_add_user_group.class.php b/sandbox/migrations/011_add_user_group.class.php new file mode 100644 index 000000000..8777a5e27 --- /dev/null +++ b/sandbox/migrations/011_add_user_group.class.php @@ -0,0 +1,38 @@ +createTable('user_group', array ( + 'user_id' => + array ( + 'primary' => true, + 'type' => 'integer', + 'length' => 11, + ), + 'group_id' => + array ( + 'primary' => true, + 'type' => 'integer', + 'length' => 11, + ), +), array ( + 'indexes' => + array ( + ), + 'primary' => + array ( + 0 => 'user_id', + 1 => 'group_id', + ), +)); + } + + public function down() + { + $this->dropTable('user_group'); + } +} \ No newline at end of file diff --git a/sandbox/migrations/012_apply_foreign_key_constraints.class.php b/sandbox/migrations/012_apply_foreign_key_constraints.class.php new file mode 100644 index 000000000..d5b64196a --- /dev/null +++ b/sandbox/migrations/012_apply_foreign_key_constraints.class.php @@ -0,0 +1,112 @@ +createForeignKey('adult', array ( + 'local' => 'contact_id', + 'foreign' => 'id', + 'foreignTable' => 'contact', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'adult_contact_contact_id_id', +)); $this->createForeignKey('child', array ( + 'local' => 'adult_id', + 'foreign' => 'id', + 'foreignTable' => 'adult', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'child_adult_adult_id_id', +)); $this->createForeignKey('dog', array ( + 'local' => 'user_id', + 'foreign' => 'id', + 'foreignTable' => 'user', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'dog_user_user_id_id', +)); $this->createForeignKey('self_reference', array ( + 'local' => 'user_id1', + 'foreign' => 'id', + 'foreignTable' => 'user', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'self_reference_user_user_id1_id', +)); $this->createForeignKey('self_reference', array ( + 'local' => 'user_id2', + 'foreign' => 'id', + 'foreignTable' => 'user', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'self_reference_user_user_id2_id', +)); $this->createForeignKey('self_reference', array ( + 'local' => 'parent_self_reference_id', + 'foreign' => 'id', + 'foreignTable' => 'self_reference', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'self_reference_self_reference_parent_self_reference_id_id', +)); $this->createForeignKey('self_reference', array ( + 'local' => 'parent_self_reference_id2', + 'foreign' => 'id', + 'foreignTable' => 'self_reference', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'self_reference_self_reference_parent_self_reference_id2_id', +)); $this->createForeignKey('user', array ( + 'local' => 'contact_id', + 'foreign' => 'id', + 'foreignTable' => 'contact', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'user_contact_contact_id_id', +)); $this->createForeignKey('user_car', array ( + 'local' => 'user_id', + 'foreign' => 'id', + 'foreignTable' => 'user', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'user_car_user_user_id_id', +)); $this->createForeignKey('user_car', array ( + 'local' => 'car_id', + 'foreign' => 'id', + 'foreignTable' => 'car', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'user_car_car_car_id_id', +)); $this->createForeignKey('user_group', array ( + 'local' => 'user_id', + 'foreign' => 'id', + 'foreignTable' => 'user', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'user_group_user_user_id_id', +)); $this->createForeignKey('user_group', array ( + 'local' => 'group_id', + 'foreign' => 'id', + 'foreignTable' => 'groups', + 'onUpdate' => NULL, + 'onDelete' => NULL, + 'name' => 'user_group_groups_group_id_id', +)); + } + + public function down() + { + $this->dropForeignKey('adult', 'adult_contact_contact_id_id'); + $this->dropForeignKey('child', 'child_adult_adult_id_id'); + $this->dropForeignKey('dog', 'dog_user_user_id_id'); + $this->dropForeignKey('self_reference', 'self_reference_user_user_id1_id'); + $this->dropForeignKey('self_reference', 'self_reference_user_user_id2_id'); + $this->dropForeignKey('self_reference', 'self_reference_self_reference_parent_self_reference_id_id'); + $this->dropForeignKey('self_reference', 'self_reference_self_reference_parent_self_reference_id2_id'); + $this->dropForeignKey('user', 'user_contact_contact_id_id'); + $this->dropForeignKey('user_car', 'user_car_user_user_id_id'); + $this->dropForeignKey('user_car', 'user_car_car_car_id_id'); + $this->dropForeignKey('user_group', 'user_group_user_user_id_id'); + $this->dropForeignKey('user_group', 'user_group_groups_group_id_id'); + + } +} \ No newline at end of file diff --git a/sandbox/models/generated/BaseGroup.class.php b/sandbox/models/generated/BaseGroup.class.php index d50e2cd10..ab882814c 100644 --- a/sandbox/models/generated/BaseGroup.class.php +++ b/sandbox/models/generated/BaseGroup.class.php @@ -8,10 +8,10 @@ abstract class BaseGroup extends Doctrine_Record public function setTableDefinition() { - $this->setTableName('group'); - $this->hasColumn('id', 'integer', 4, array('notnull' => true, - 'primary' => true, - 'autoincrement' => true)); + $this->setTableName('groups'); + $this->hasColumn('id', 'integer', 11, array('notnull' => true, + 'primary' => true, + 'autoincrement' => true)); $this->hasColumn('name', 'string', 255); diff --git a/sandbox/models/generated/BaseUserGroup.class.php b/sandbox/models/generated/BaseUserGroup.class.php index 9048a0709..d7183f467 100644 --- a/sandbox/models/generated/BaseUserGroup.class.php +++ b/sandbox/models/generated/BaseUserGroup.class.php @@ -9,8 +9,8 @@ abstract class BaseUserGroup extends Doctrine_Record public function setTableDefinition() { $this->setTableName('user_group'); - $this->hasColumn('user_id', 'integer', 4, array('primary' => true)); - $this->hasColumn('group_id', 'integer', 4, array('primary' => true)); + $this->hasColumn('user_id', 'integer', 11, array('primary' => true)); + $this->hasColumn('group_id', 'integer', 11, array('primary' => true)); diff --git a/sandbox/sandbox.db b/sandbox/sandbox.db index 6f0820dba..bb6c5ff0a 100755 Binary files a/sandbox/sandbox.db and b/sandbox/sandbox.db differ diff --git a/sandbox/schema/group.yml b/sandbox/schema/group.yml index 3f9ee988b..ad891519e 100644 --- a/sandbox/schema/group.yml +++ b/sandbox/schema/group.yml @@ -1,12 +1,13 @@ --- Group: + tableName: groups columns: id: notnull: true primary: true autoincrement: true type: integer - length: 4 + length: 11 name: id name: type: string @@ -14,4 +15,4 @@ Group: relations: Users: class: User - refClass: UserGroup \ No newline at end of file + refClass: UserGroup diff --git a/sandbox/schema/self_reference.yml b/sandbox/schema/self_reference.yml index 30a8b234b..3a78b68b1 100644 --- a/sandbox/schema/self_reference.yml +++ b/sandbox/schema/self_reference.yml @@ -3,24 +3,24 @@ SelfReference: fields: id: type: integer - size: 11 + length: 11 primary: true autoincrement: true name: type: string - size: 255 + length: 255 user_id1: type: integer - size: 11 + length: 11 user_id2: type: integer - size: 11 + length: 11 parent_self_reference_id: type: integer - size: 11 + length: 11 parent_self_reference_id2: type: integer - size: 11 + length: 11 relations: User1: class: User @@ -37,4 +37,4 @@ SelfReference: SelfReference2: class: SelfReference local: parent_self_reference_id2 - foreignAlias: SelfReferences2 \ No newline at end of file + foreignAlias: SelfReferences2 diff --git a/sandbox/schema/user_group.yml b/sandbox/schema/user_group.yml index 0d3bd07ae..6642aa826 100644 --- a/sandbox/schema/user_group.yml +++ b/sandbox/schema/user_group.yml @@ -3,12 +3,12 @@ UserGroup: columns: user_id: type: integer - length: 4 + length: 11 primary: true group_id: type: integer - length: 4 + length: 11 primary: true relations: User: - - Group: - \ No newline at end of file + Group: - diff --git a/tests/ExportTestCase.php b/tests/ExportTestCase.php index 3ff67fa4c..900cc5c17 100644 --- a/tests/ExportTestCase.php +++ b/tests/ExportTestCase.php @@ -56,7 +56,7 @@ class Doctrine_Export_TestCase extends Doctrine_UnitTestCase { $this->export->dropConstraint('sometable', 'relevancy'); - $this->assertEqual($this->adapter->pop(), 'ALTER TABLE sometable DROP CONSTRAINT relevancy'); + $this->assertEqual($this->adapter->pop(), 'ALTER TABLE sometable DROP CONSTRAINT relevancy_idx'); } public function testCreateIndexExecutesSql() {