2009-04-03 15:06:58 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools;
|
|
|
|
|
2009-08-17 21:58:16 +04:00
|
|
|
use Doctrine\DBAL\Types\Type,
|
2009-08-31 20:21:29 +04:00
|
|
|
Doctrine\ORM\EntityManager,
|
|
|
|
Doctrine\ORM\Internal\CommitOrderCalculator;
|
2009-04-03 15:06:58 +04:00
|
|
|
|
|
|
|
/**
|
2009-08-31 20:21:29 +04:00
|
|
|
* The SchemaTool is a tool to create/drop/update database schemas based on
|
2009-04-03 15:06:58 +04:00
|
|
|
* <tt>ClassMetadata</tt> class descriptors.
|
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-11-01 01:23:36 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
class SchemaTool
|
|
|
|
{
|
2009-11-02 15:53:05 +03:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\EntityManager
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
private $_em;
|
2009-11-02 15:53:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
private $_platform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new SchemaTool instance that uses the connection of the
|
|
|
|
* provided EntityManager.
|
|
|
|
*
|
|
|
|
* @param Doctrine\ORM\EntityManager $em
|
|
|
|
*/
|
|
|
|
public function __construct(EntityManager $em)
|
|
|
|
{
|
|
|
|
$this->_em = $em;
|
|
|
|
$this->_platform = $em->getConnection()->getDatabasePlatform();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the database schema for the given array of ClassMetadata instances.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
*/
|
|
|
|
public function createSchema(array $classes)
|
|
|
|
{
|
|
|
|
$createSchemaSql = $this->getCreateSchemaSql($classes);
|
|
|
|
$conn = $this->_em->getConnection();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
foreach ($createSchemaSql as $sql) {
|
|
|
|
$conn->execute($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-21 22:13:22 +04:00
|
|
|
* Gets the list of DDL statements that are required to create the database schema for
|
|
|
|
* the given list of ClassMetadata instances.
|
2009-04-03 15:06:58 +04:00
|
|
|
*
|
|
|
|
* @param array $classes
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return array $sql The SQL statements needed to create the schema for the classes.
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
public function getCreateSchemaSql(array $classes)
|
|
|
|
{
|
2009-05-05 21:20:55 +04:00
|
|
|
$sql = array(); // All SQL statements
|
|
|
|
$processedClasses = array(); // Reminder for processed classes, used for hierarchies
|
|
|
|
$foreignKeyConstraints = array(); // FK SQL statements. Appended to $sql at the end.
|
|
|
|
$sequences = array(); // Sequence SQL statements. Appended to $sql at the end.
|
2009-04-03 15:06:58 +04:00
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
2009-09-29 19:54:16 +04:00
|
|
|
if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) {
|
2009-04-09 22:12:48 +04:00
|
|
|
continue;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
$options = array(); // table options
|
2009-05-21 12:53:40 +04:00
|
|
|
$columns = array(); // table columns
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable()) {
|
2009-05-21 12:53:40 +04:00
|
|
|
$columns = $this->_gatherColumns($class, $options);
|
|
|
|
$this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
// Add the discriminator column
|
|
|
|
$discrColumnDef = $this->_getDiscriminatorColumnDefinition($class);
|
|
|
|
$columns[$discrColumnDef['name']] = $discrColumnDef;
|
|
|
|
|
|
|
|
// Aggregate all the information from all classes in the hierarchy
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
2009-04-09 22:12:48 +04:00
|
|
|
// Parent class information is already contained in this class
|
|
|
|
$processedClasses[$parentClassName] = true;
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->subClasses as $subClassName) {
|
2009-05-03 14:58:16 +04:00
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
|
|
|
$columns = array_merge($columns, $this->_gatherColumns($subClass, $options));
|
|
|
|
$this->_gatherRelationsSql($subClass, $sql, $columns, $foreignKeyConstraints);
|
2009-04-09 22:12:48 +04:00
|
|
|
$processedClasses[$subClassName] = true;
|
|
|
|
}
|
|
|
|
} else if ($class->isInheritanceTypeJoined()) {
|
2009-05-21 12:53:40 +04:00
|
|
|
// Add all non-inherited fields as columns
|
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if ( ! isset($mapping['inherited'])) {
|
2009-10-19 00:36:02 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
|
2009-08-11 14:51:38 +04:00
|
|
|
$columns[$columnName] = $this->_gatherColumn($class, $mapping, $options);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);
|
|
|
|
|
|
|
|
// Add the discriminator column only to the root table
|
|
|
|
if ($class->name == $class->rootEntityName) {
|
|
|
|
$discrColumnDef = $this->_getDiscriminatorColumnDefinition($class);
|
|
|
|
$columns[$discrColumnDef['name']] = $discrColumnDef;
|
|
|
|
} else {
|
|
|
|
// Add an ID FK column to child tables
|
|
|
|
$idMapping = $class->fieldMappings[$class->identifier[0]];
|
|
|
|
$idColumn = $this->_gatherColumn($class, $idMapping, $options);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
unset($idColumn['autoincrement']);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$columns[$idColumn['name']] = $idColumn;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
// Add a FK constraint on the ID column
|
|
|
|
$constraint = array();
|
2009-08-11 14:51:38 +04:00
|
|
|
$constraint['tableName'] = $class->getQuotedTableName($this->_platform);
|
|
|
|
$constraint['foreignTable'] = $this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform);
|
|
|
|
$constraint['local'] = array($idColumn['name']);
|
|
|
|
$constraint['foreign'] = array($idColumn['name']);
|
2009-05-21 12:53:40 +04:00
|
|
|
$constraint['onDelete'] = 'CASCADE';
|
|
|
|
$foreignKeyConstraints[] = $constraint;
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($class->isInheritanceTypeTablePerClass()) {
|
2009-08-21 22:13:22 +04:00
|
|
|
throw DoctrineException::notSupported();
|
2009-05-21 12:53:40 +04:00
|
|
|
} else {
|
|
|
|
$columns = $this->_gatherColumns($class, $options);
|
|
|
|
$this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
if (isset($class->primaryTable['indexes'])) {
|
|
|
|
$options['indexes'] = $class->primaryTable['indexes'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($class->primaryTable['uniqueConstraints'])) {
|
|
|
|
$options['uniqueConstraints'] = $class->primaryTable['uniqueConstraints'];
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql = array_merge($sql, $this->_platform->getCreateTableSql(
|
2009-09-05 21:46:18 +04:00
|
|
|
$class->getQuotedTableName($this->_platform), $columns, $options)
|
|
|
|
);
|
2009-09-08 22:12:01 +04:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
$processedClasses[$class->name] = true;
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2009-08-20 06:59:42 +04:00
|
|
|
// TODO if we're reusing the sequence previously defined (in another model),
|
|
|
|
// it should not attempt to create a new sequence.
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
|
2009-05-05 21:20:55 +04:00
|
|
|
$seqDef = $class->getSequenceGeneratorDefinition();
|
|
|
|
$sequences[] = $this->_platform->getCreateSequenceSql(
|
|
|
|
$seqDef['sequenceName'],
|
|
|
|
$seqDef['initialValue'],
|
|
|
|
$seqDef['allocationSize']
|
|
|
|
);
|
|
|
|
}
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
// Append the foreign key constraints SQL
|
2009-04-03 15:06:58 +04:00
|
|
|
if ($this->_platform->supportsForeignKeyConstraints()) {
|
|
|
|
foreach ($foreignKeyConstraints as $fkConstraint) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$sql = array_merge($sql, (array) $this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint));
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
// Append the sequence SQL
|
|
|
|
$sql = array_merge($sql, $sequences);
|
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gets a portable column definition as required by the DBAL for the discriminator
|
|
|
|
* column of a class.
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @return array The portable column definition of the discriminator column as required by
|
|
|
|
* the DBAL.
|
|
|
|
*/
|
2009-04-09 22:12:48 +04:00
|
|
|
private function _getDiscriminatorColumnDefinition($class)
|
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$discrColumn = $class->discriminatorColumn;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
return array(
|
2009-08-11 14:51:38 +04:00
|
|
|
'name' => $class->getQuotedDiscriminatorColumnName($this->_platform),
|
2009-04-09 22:12:48 +04:00
|
|
|
'type' => Type::getType($discrColumn['type']),
|
|
|
|
'length' => $discrColumn['length'],
|
|
|
|
'notnull' => true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
/**
|
2009-08-21 22:13:22 +04:00
|
|
|
* Gathers the column definitions as required by the DBAL of all field mappings
|
|
|
|
* found in the given class.
|
2009-05-21 12:53:40 +04:00
|
|
|
*
|
|
|
|
* @param ClassMetadata $class
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $options The table options/constraints where any additional options/constraints
|
|
|
|
* that are required by columns should be appended.
|
|
|
|
* @return array The list of portable column definitions as required by the DBAL.
|
2009-05-21 12:53:40 +04:00
|
|
|
*/
|
2009-05-03 14:58:16 +04:00
|
|
|
private function _gatherColumns($class, array &$options)
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
|
|
|
$columns = array();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$column = $this->_gatherColumn($class, $mapping, $options);
|
|
|
|
$columns[$column['name']] = $column;
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
return $columns;
|
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a column definition as required by the DBAL from an ORM field mapping definition.
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $class The class that owns the field mapping.
|
|
|
|
* @param array $mapping The field mapping.
|
|
|
|
* @param array $options The table options/constraints where any additional options/constraints
|
|
|
|
* required by the column should be appended.
|
|
|
|
* @return array The portable column definition as required by the DBAL.
|
|
|
|
*/
|
2009-05-21 12:53:40 +04:00
|
|
|
private function _gatherColumn($class, array $mapping, array &$options)
|
|
|
|
{
|
|
|
|
$column = array();
|
2009-08-15 22:11:51 +04:00
|
|
|
$column['name'] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
|
2009-05-21 12:53:40 +04:00
|
|
|
$column['type'] = Type::getType($mapping['type']);
|
2009-08-21 22:13:22 +04:00
|
|
|
$column['length'] = isset($mapping['length']) ? $mapping['length'] : null;
|
2009-11-02 15:53:05 +03:00
|
|
|
$column['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
|
2009-09-06 00:35:22 +04:00
|
|
|
$column['unique'] = isset($mapping['unique']) ? $mapping['unique'] : false;
|
2009-09-08 21:09:32 +04:00
|
|
|
$column['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
|
2009-09-05 23:44:26 +04:00
|
|
|
|
2009-11-02 15:53:05 +03:00
|
|
|
if(strtolower($column['type']) == 'string' && $column['length'] === null) {
|
|
|
|
$column['length'] = 255;
|
|
|
|
}
|
|
|
|
|
2009-09-05 00:31:11 +04:00
|
|
|
if (isset($mapping['precision'])) {
|
|
|
|
$column['precision'] = $mapping['precision'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-09-05 00:31:11 +04:00
|
|
|
if (isset($mapping['scale'])) {
|
|
|
|
$column['scale'] = $mapping['scale'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-07-17 22:13:03 +04:00
|
|
|
if (isset($mapping['default'])) {
|
|
|
|
$column['default'] = $mapping['default'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($class->isIdentifier($mapping['fieldName'])) {
|
|
|
|
$column['primary'] = true;
|
|
|
|
$options['primary'][] = $mapping['columnName'];
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($class->isIdGeneratorIdentity()) {
|
|
|
|
$column['autoincrement'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gathers the SQL for properly setting up the relations of the given class.
|
|
|
|
* This includes the SQL for foreign key constraints and join tables.
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @param array $sql The sequence of SQL statements where any new statements should be appended.
|
|
|
|
* @param array $columns The list of columns in the class's primary table where any additional
|
|
|
|
* columns required by relations should be appended.
|
|
|
|
* @param array $constraints The constraints of the table where any additional constraints
|
|
|
|
* required by relations should be appended.
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-05-03 14:58:16 +04:00
|
|
|
private function _gatherRelationsSql($class, array &$sql, array &$columns, array &$constraints)
|
|
|
|
{
|
2009-05-21 12:53:40 +04:00
|
|
|
foreach ($class->associationMappings as $fieldName => $mapping) {
|
|
|
|
if (isset($class->inheritedAssociationFields[$fieldName])) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
$foreignClass = $this->_em->getClassMetadata($mapping->targetEntityName);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($mapping->isOneToOne() && $mapping->isOwningSide) {
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint = array();
|
2009-08-11 14:51:38 +04:00
|
|
|
$constraint['tableName'] = $class->getQuotedTableName($this->_platform);
|
|
|
|
$constraint['foreignTable'] = $foreignClass->getQuotedTableName($this->_platform);
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint['local'] = array();
|
|
|
|
$constraint['foreign'] = array();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
foreach ($mapping->getJoinColumns() as $joinColumn) {
|
|
|
|
$column = array();
|
2009-08-11 14:51:38 +04:00
|
|
|
$column['name'] = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
|
2009-05-13 19:19:27 +04:00
|
|
|
$column['type'] = Type::getType($foreignClass->getTypeOfColumn($joinColumn['referencedColumnName']));
|
2009-08-11 14:51:38 +04:00
|
|
|
$columns[$column['name']] = $column;
|
|
|
|
$constraint['local'][] = $column['name'];
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint['foreign'][] = $joinColumn['referencedColumnName'];
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($joinColumn['onUpdate'])) {
|
|
|
|
$constraint['onUpdate'] = $joinColumn['onUpdate'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($joinColumn['onDelete'])) {
|
|
|
|
$constraint['onDelete'] = $joinColumn['onDelete'];
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraints[] = $constraint;
|
2009-05-21 12:53:40 +04:00
|
|
|
} else if ($mapping->isOneToMany() && $mapping->isOwningSide) {
|
2009-05-03 14:58:16 +04:00
|
|
|
//... create join table, one-many through join table supported later
|
2009-08-21 22:13:22 +04:00
|
|
|
throw DoctrineException::notSupported();
|
2009-05-21 12:53:40 +04:00
|
|
|
} else if ($mapping->isManyToMany() && $mapping->isOwningSide) {
|
2009-05-03 14:58:16 +04:00
|
|
|
// create join table
|
|
|
|
$joinTableColumns = array();
|
|
|
|
$joinTableOptions = array();
|
|
|
|
$joinTable = $mapping->getJoinTable();
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
// Build first FK constraint (relation table => source table)
|
2009-07-30 19:16:02 +04:00
|
|
|
$constraint1 = array(
|
2009-08-11 14:51:38 +04:00
|
|
|
'tableName' => $mapping->getQuotedJoinTableName($this->_platform),
|
|
|
|
'foreignTable' => $class->getQuotedTableName($this->_platform),
|
2009-07-30 19:16:02 +04:00
|
|
|
'local' => array(),
|
|
|
|
'foreign' => array()
|
|
|
|
);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
foreach ($joinTable['joinColumns'] as $joinColumn) {
|
|
|
|
$column = array();
|
|
|
|
$column['primary'] = true;
|
|
|
|
$joinTableOptions['primary'][] = $joinColumn['name'];
|
2009-08-11 14:51:38 +04:00
|
|
|
$column['name'] = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
|
2009-05-13 19:19:27 +04:00
|
|
|
$column['type'] = Type::getType($class->getTypeOfColumn($joinColumn['referencedColumnName']));
|
2009-08-11 14:51:38 +04:00
|
|
|
$joinTableColumns[$column['name']] = $column;
|
|
|
|
$constraint1['local'][] = $column['name'];
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint1['foreign'][] = $joinColumn['referencedColumnName'];
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($joinColumn['onUpdate'])) {
|
|
|
|
$constraint1['onUpdate'] = $joinColumn['onUpdate'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($joinColumn['onDelete'])) {
|
|
|
|
$constraint1['onDelete'] = $joinColumn['onDelete'];
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraints[] = $constraint1;
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
// Build second FK constraint (relation table => target table)
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint2 = array();
|
2009-08-11 14:51:38 +04:00
|
|
|
$constraint2['tableName'] = $mapping->getQuotedJoinTableName($this->_platform);
|
|
|
|
$constraint2['foreignTable'] = $foreignClass->getQuotedTableName($this->_platform);
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraint2['local'] = array();
|
|
|
|
$constraint2['foreign'] = array();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
foreach ($joinTable['inverseJoinColumns'] as $inverseJoinColumn) {
|
|
|
|
$column = array();
|
|
|
|
$column['primary'] = true;
|
|
|
|
$joinTableOptions['primary'][] = $inverseJoinColumn['name'];
|
|
|
|
$column['name'] = $inverseJoinColumn['name'];
|
2009-05-13 19:19:27 +04:00
|
|
|
$column['type'] = Type::getType($this->_em->getClassMetadata($mapping->getTargetEntityName())
|
|
|
|
->getTypeOfColumn($inverseJoinColumn['referencedColumnName']));
|
2009-05-03 14:58:16 +04:00
|
|
|
$joinTableColumns[$inverseJoinColumn['name']] = $column;
|
|
|
|
$constraint2['local'][] = $inverseJoinColumn['name'];
|
|
|
|
$constraint2['foreign'][] = $inverseJoinColumn['referencedColumnName'];
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($inverseJoinColumn['onUpdate'])) {
|
|
|
|
$constraint2['onUpdate'] = $inverseJoinColumn['onUpdate'];
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($joinColumn['onDelete'])) {
|
|
|
|
$constraint2['onDelete'] = $inverseJoinColumn['onDelete'];
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$constraints[] = $constraint2;
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
// Get the SQL for creating the join table and merge it with the others
|
2009-05-03 14:58:16 +04:00
|
|
|
$sql = array_merge($sql, $this->_platform->getCreateTableSql(
|
2009-09-05 21:46:18 +04:00
|
|
|
$mapping->getQuotedJoinTableName($this->_platform), $joinTableColumns, $joinTableOptions)
|
|
|
|
);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Drops the database schema for the given classes.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
public function dropSchema(array $classes)
|
|
|
|
{
|
2009-08-21 22:13:22 +04:00
|
|
|
$dropSchemaSql = $this->getDropSchemaSql($classes);
|
|
|
|
$conn = $this->_em->getConnection();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($dropSchemaSql as $sql) {
|
|
|
|
$conn->execute($sql);
|
|
|
|
}
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SQL needed to drop the database schema for the given classes.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
public function getDropSchemaSql(array $classes)
|
|
|
|
{
|
2009-08-21 22:13:22 +04:00
|
|
|
$sql = array();
|
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
$commitOrder = $this->_getCommitOrder($classes);
|
2009-09-05 21:46:18 +04:00
|
|
|
$associationTables = $this->_getAssociationTables($commitOrder);
|
|
|
|
|
|
|
|
// Drop association tables first
|
|
|
|
foreach ($associationTables as $associationTable) {
|
|
|
|
$sql[] = $this->_platform->getDropTableSql($associationTable);
|
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
// Drop tables in reverse commit order
|
|
|
|
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
|
|
|
|
$class = $commitOrder[$i];
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-10-07 11:45:08 +04:00
|
|
|
if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
|
|
|
|
|| $class->isMappedSuperclass) {
|
2009-08-21 22:13:22 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
$sql[] = $this->_platform->getDropTableSql($class->getTableName());
|
|
|
|
}
|
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
//TODO: Drop other schema elements, like sequences etc.
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
return $sql;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
2009-08-17 21:58:16 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Updates the database schema of the given classes by comparing the ClassMetadata
|
|
|
|
* instances to the current database schema that is inspected.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-08-17 21:58:16 +04:00
|
|
|
public function updateSchema(array $classes)
|
|
|
|
{
|
2009-08-21 22:13:22 +04:00
|
|
|
$updateSchemaSql = $this->getUpdateSchemaSql($classes);
|
|
|
|
$conn = $this->_em->getConnection();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($updateSchemaSql as $sql) {
|
|
|
|
$conn->execute($sql);
|
|
|
|
}
|
2009-08-17 21:58:16 +04:00
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the sequence of SQL statements that need to be performed in order
|
|
|
|
* to bring the given class mappings in-synch with the relational schema.
|
|
|
|
*
|
|
|
|
* @param array $classes The classes to consider.
|
|
|
|
* @return array The sequence of SQL statements.
|
|
|
|
*/
|
2009-08-17 21:58:16 +04:00
|
|
|
public function getUpdateSchemaSql(array $classes)
|
|
|
|
{
|
2009-08-21 22:13:22 +04:00
|
|
|
$sql = array();
|
|
|
|
$conn = $this->_em->getConnection();
|
|
|
|
$sm = $conn->getSchemaManager();
|
|
|
|
|
|
|
|
$tables = $sm->listTables();
|
|
|
|
$newClasses = array();
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$tableName = $class->getTableName();
|
|
|
|
$tableExists = false;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($tables as $index => $table) {
|
|
|
|
if ($tableName == $table) {
|
|
|
|
$tableExists = true;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
unset($tables[$index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ( ! $tableExists) {
|
|
|
|
$newClasses[] = $class;
|
|
|
|
} else {
|
|
|
|
$newFields = array();
|
2009-09-13 03:25:47 +04:00
|
|
|
$updateFields = array();
|
|
|
|
$dropIndexes = array();
|
2009-08-21 22:13:22 +04:00
|
|
|
$newJoinColumns = array();
|
|
|
|
$currentColumns = $sm->listTableColumns($tableName);
|
|
|
|
|
2009-09-13 03:25:47 +04:00
|
|
|
foreach ($class->fieldMappings as $fieldName => $fieldMapping) {
|
2009-08-21 22:13:22 +04:00
|
|
|
$exists = false;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($currentColumns as $index => $column) {
|
|
|
|
if ($column['name'] == $fieldMapping['columnName']) {
|
|
|
|
// Column exists, check for changes
|
2009-09-13 03:25:47 +04:00
|
|
|
$columnInfo = $column;
|
|
|
|
$columnChanged = false;
|
2009-10-04 23:35:47 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
// 1. check for nullability change
|
2009-09-13 03:25:47 +04:00
|
|
|
$columnInfo['notnull'] = ( ! isset($columnInfo['notnull']))
|
|
|
|
? false : $columnInfo['notnull'];
|
|
|
|
$notnull = ! $class->isNullable($fieldName);
|
|
|
|
|
|
|
|
if ($columnInfo['notnull'] != $notnull) {
|
|
|
|
$columnInfo['notnull'] = $notnull;
|
|
|
|
$columnChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($notnull);
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
// 2. check for uniqueness change
|
2009-09-13 03:25:47 +04:00
|
|
|
$columnInfo['unique'] = ( ! isset($columnInfo['unique']))
|
|
|
|
? false : $columnInfo['unique'];
|
|
|
|
$unique = $class->isUniqueField($fieldName);
|
|
|
|
|
|
|
|
if ($columnInfo['unique'] != $unique) {
|
|
|
|
// We need to call a special DROP INDEX if it was defined
|
|
|
|
if ($columnInfo['unique']) {
|
|
|
|
$dropIndexes[] = $column['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnInfo['unique'] = $unique;
|
|
|
|
$columnChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($unique);
|
|
|
|
|
|
|
|
// 3. check for type change
|
|
|
|
$type = Type::getType($fieldMapping['type']);
|
|
|
|
|
|
|
|
if ($columnInfo['type'] != $type) {
|
|
|
|
$columnInfo['type'] = $type;
|
|
|
|
$columnChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($type);
|
|
|
|
|
2009-11-02 15:53:05 +03:00
|
|
|
// 4. check for scale and precision change
|
|
|
|
if (strtolower($columnInfo['type']) == 'decimal') {
|
2009-11-01 01:23:36 +03:00
|
|
|
/*// Doesn't work yet, see DDC-89
|
|
|
|
if($columnInfo['length'] != $fieldMapping['precision'] ||
|
|
|
|
$columnInfo['scale'] != $fieldMapping['scale']) {
|
|
|
|
|
|
|
|
$columnInfo['length'] = $fieldMapping['precision'];
|
|
|
|
$columnInfo['scale'] = $fieldMapping['scale'];
|
|
|
|
$columnChanged = true;
|
|
|
|
}*/
|
2009-11-02 15:53:05 +03:00
|
|
|
}
|
|
|
|
// 5. check for length change of strings
|
|
|
|
elseif(strtolower($fieldMapping['type']) == 'string') {
|
|
|
|
if(!isset($fieldMapping['length']) || $fieldMapping['length'] === null) {
|
|
|
|
$fieldMapping['length'] = 255;
|
|
|
|
}
|
|
|
|
|
2009-11-01 01:23:36 +03:00
|
|
|
if($columnInfo['length'] != $fieldMapping['length']) {
|
|
|
|
$columnInfo['length'] = $fieldMapping['length'];
|
|
|
|
$columnChanged = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-13 03:25:47 +04:00
|
|
|
|
|
|
|
// 6. check for flexible and fixed length
|
|
|
|
$fieldMapping['fixed'] = ( ! isset($fieldMapping['fixed']))
|
|
|
|
? false : $fieldMapping['fixed'];
|
|
|
|
|
|
|
|
if ($columnInfo['fixed'] != $fieldMapping['fixed']) {
|
|
|
|
$columnInfo['fixed'] = $fieldMapping['fixed'];
|
|
|
|
$columnChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only add to column changed list if it was actually changed
|
|
|
|
if ($columnChanged) {
|
|
|
|
$updateFields[] = $columnInfo;
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
unset($currentColumns[$index]);
|
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ( ! $exists) {
|
|
|
|
$newFields[] = $fieldMapping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
|
|
|
|
foreach ($assoc->targetToSourceKeyColumns as $targetColumn => $sourceColumn) {
|
|
|
|
$exists = false;
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($currentColumns as $index => $column) {
|
|
|
|
if ($column['name'] == $sourceColumn) {
|
|
|
|
// Column exists, check for changes
|
|
|
|
|
|
|
|
// 1. check for nullability change
|
|
|
|
|
|
|
|
unset($currentColumns[$index]);
|
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ( ! $exists) {
|
|
|
|
$newJoinColumns[$sourceColumn] = array(
|
|
|
|
'name' => $sourceColumn,
|
|
|
|
'type' => 'integer' //FIXME!!!
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 03:25:47 +04:00
|
|
|
// Drop indexes
|
|
|
|
if ($dropIndexes) {
|
|
|
|
foreach ($dropIndexes as $dropIndex) {
|
|
|
|
$sql[] = $this->_platform->getDropIndexSql($tableName, $dropIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new columns
|
2009-08-21 22:13:22 +04:00
|
|
|
if ($newFields || $newJoinColumns) {
|
|
|
|
$changes = array();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($newFields as $newField) {
|
|
|
|
$options = array();
|
|
|
|
$changes['add'][$newField['columnName']] = $this->_gatherColumn($class, $newField, $options);
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($newJoinColumns as $name => $joinColumn) {
|
2009-10-07 11:45:08 +04:00
|
|
|
$joinColumn['type'] = Type::getType($joinColumn['type']);
|
2009-08-21 22:13:22 +04:00
|
|
|
$changes['add'][$name] = $joinColumn;
|
|
|
|
}
|
|
|
|
$sql[] = $this->_platform->getAlterTableSql($tableName, $changes);
|
|
|
|
}
|
|
|
|
|
2009-09-13 03:25:47 +04:00
|
|
|
// Update existent columns
|
|
|
|
if ($updateFields) {
|
|
|
|
$changes = array();
|
|
|
|
|
|
|
|
foreach ($updateFields as $updateField) {
|
|
|
|
// Now we pick the Type instance
|
|
|
|
$changes['change'][$updateField['name']] = array(
|
|
|
|
'definition' => $updateField
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql[] = $this->_platform->getAlterTableSql($tableName, $changes);
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
// Drop any remaining columns
|
|
|
|
if ($currentColumns) {
|
|
|
|
$changes = array();
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($currentColumns as $column) {
|
|
|
|
$options = array();
|
|
|
|
$changes['remove'][$column['name']] = $column;
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
$sql[] = $this->_platform->getAlterTableSql($tableName, $changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($newClasses) {
|
|
|
|
$sql = array_merge($this->getCreateSchemaSql($newClasses), $sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop any remaining tables (Probably not a good idea, because the given class list
|
|
|
|
// may not be complete!)
|
|
|
|
/*if ($tables) {
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
$sql[] = $this->_platform->getDropTableSql($table);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
return $sql;
|
2009-08-17 21:58:16 +04:00
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
|
|
|
|
private function _getCommitOrder(array $classes)
|
|
|
|
{
|
|
|
|
$calc = new CommitOrderCalculator;
|
|
|
|
|
|
|
|
// Calculate dependencies
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$calc->addClass($class);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide) {
|
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
if ( ! $calc->hasClass($targetClass->name)) {
|
|
|
|
$calc->addClass($targetClass);
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
// add dependency ($targetClass before $class)
|
|
|
|
$calc->addDependency($targetClass, $class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calc->getCommitOrder();
|
|
|
|
}
|
2009-09-05 21:46:18 +04:00
|
|
|
|
|
|
|
private function _getAssociationTables(array $classes)
|
|
|
|
{
|
|
|
|
$associationTables = array();
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide && $assoc->isManyToMany()) {
|
|
|
|
$associationTables[] = $assoc->joinTable['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $associationTables;
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|