Commit 9440cf04 authored by Jonathan.Wage's avatar Jonathan.Wage

Additions to migrations to support generating migrations from models or existing databases.

parent 16933ef9
......@@ -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 = '<?php' . PHP_EOL;
$code .= "// Automatically generated by the Doctrine ORM Framework\n";
$code .= "class " . self::classify($className) . " extends Doctrine_Migration\n";
$code .= "{\n";
$code .= "\tpublic function up()\n\t{ }\n\n";
$code .= "\tpublic function down()\n\t{ }\n";
$code .= "}";
return $builder->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);
}
/**
......
......@@ -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
......
......@@ -158,7 +158,6 @@ class Doctrine_Import_Builder
%s
}
END;
}
/*
......
......@@ -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';
......
<?php
/*
* $Id: Builder.php 2939 2007-10-19 14:23:42Z Jonathan.Wage $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Migration_Builder
*
* @package Doctrine
* @subpackage Migration
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @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 =<<<END
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class %s extends %s
{
public function up()
{
%s
}
public function down()
{
%s
}
}
END;
}
/**
* generateMigrationsFromDb
*
* @return void
*/
public function generateMigrationsFromDb()
{
$directory = '/tmp/tmp_doctrine_models';
Doctrine::generateModelsFromDb($directory);
$result = $this->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 = '<?php' . PHP_EOL;
$content .= sprintf(self::$tpl, $className,
$extends,
$up,
$down);
return $content;
}
}
\ No newline at end of file
......@@ -99,7 +99,7 @@ abstract class Doctrine_Task
*/
public function getArgument($name, $default = null)
{
if (isset($this->arguments[$name]) && $this->arguments[$name]) {
if (isset($this->arguments[$name]) && $this->arguments[$name] !== null) {
return $this->arguments[$name];
} else {
return $default;
......
<?php
/*
* $Id: GenerateMigrationsFromDb.php 2761 2007-10-07 23:42:29Z zYne $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* 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 <jwage@mac.com>
*/
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
<?php
/*
* $Id: GenerateMigrationsFromModels.php 2761 2007-10-07 23:42:29Z zYne $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* 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 <jwage@mac.com>
*/
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
......@@ -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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddAdult extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddCar extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddChild extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddContact extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddDog extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddEntity extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddGroups extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddSelfReference extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddUser extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddUserCar extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class AddUserGroup extends Doctrine_Migration
{
public function up()
{
$this->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
<?php
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class ApplyForeignKeyConstraints extends Doctrine_Migration
{
public function up()
{
$this->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
......@@ -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);
......
......@@ -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));
......
No preview for this file type
---
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
......@@ -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
......@@ -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: -
......@@ -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()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment