Commit 7ef91a64 authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Some work over SchemaTool update of schema (affects directly the CLI...

[2.0] Some work over SchemaTool update of schema (affects directly the CLI schema-tool task on --update option.
parent 93445983
...@@ -762,7 +762,8 @@ abstract class AbstractPlatform ...@@ -762,7 +762,8 @@ abstract class AbstractPlatform
public function getDecimalTypeDeclarationSql(array $columnDef) public function getDecimalTypeDeclarationSql(array $columnDef)
{ {
$columnDef['precision'] = ( ! isset($columnDef['precision']) || empty($columnDef['precision'])) $columnDef['precision'] = ( ! isset($columnDef['precision']) || empty($columnDef['precision']))
? 10 : $columnDef['precision']; ? (( ! isset($columnDef['length']) || empty($columnDef['length'])) ? 10 : $columnDef['length'])
: $columnDef['precision'];
$columnDef['scale'] = ( ! isset($columnDef['scale']) || empty($columnDef['scale'])) $columnDef['scale'] = ( ! isset($columnDef['scale']) || empty($columnDef['scale']))
? 0 : $columnDef['scale']; ? 0 : $columnDef['scale'];
......
...@@ -109,7 +109,7 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -109,7 +109,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
$values = null; $values = null;
$scale = null; $scale = null;
switch ($dbType) { switch ($dbType) {
case 'tinyint': case 'tinyint':
$type = 'integer'; $type = 'integer';
...@@ -239,27 +239,24 @@ class MySqlSchemaManager extends AbstractSchemaManager ...@@ -239,27 +239,24 @@ class MySqlSchemaManager extends AbstractSchemaManager
'unsigned' => (bool) $unsigned, 'unsigned' => (bool) $unsigned,
'fixed' => (bool) $fixed 'fixed' => (bool) $fixed
); );
if ($values !== null) {
$def['values'] = $values;
}
if ($scale !== null) { if ($scale !== null) {
$def['scale'] = $scale; $def['scale'] = $scale;
} }
$values = isset($def['values']) ? $def['values'] : array(); $values = ($values !== null) ? $values : array();
$column = array( $column = array(
'name' => $tableColumn['Field'], 'name' => $tableColumn['Field'],
'values' => $values, 'values' => $values,
'primary' => (bool) (strtolower($tableColumn['Key']) == 'pri'), 'primary' => (bool) (strtolower($tableColumn['Key']) == 'pri'),
'unique' => (bool) (strtolower($tableColumn['Key']) == 'uni'),
'default' => $tableColumn['Default'], 'default' => $tableColumn['Default'],
'notnull' => (bool) ($tableColumn['Null'] != 'YES'), 'notnull' => (bool) ($tableColumn['Null'] != 'YES'),
'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false), 'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false),
); );
$column = array_merge($column, $def); return array_merge($column, $def);
return $column;
} }
public function _getPortableTableForeignKeyDefinition($tableForeignKey) public function _getPortableTableForeignKeyDefinition($tableForeignKey)
......
...@@ -628,11 +628,11 @@ final class ClassMetadata ...@@ -628,11 +628,11 @@ final class ClassMetadata
* @param string $fieldName The field name * @param string $fieldName The field name
* @return boolean TRUE if the field is not null, FALSE otherwise. * @return boolean TRUE if the field is not null, FALSE otherwise.
*/ */
public function isNotNull($fieldName) public function isNullable($fieldName)
{ {
$mapping = $this->getFieldMapping($fieldName); $mapping = $this->getFieldMapping($fieldName);
if ($mapping !== false) { if ($mapping !== false) {
return isset($mapping['nullable']) && $mapping['nullable'] == false; return isset($mapping['nullable']) && $mapping['nullable'] == true;
} }
return false; return false;
} }
......
...@@ -30,11 +30,13 @@ use Doctrine\Common\DoctrineException, ...@@ -30,11 +30,13 @@ use Doctrine\Common\DoctrineException,
* metadata mapping informations of a class which describes how a class should be mapped * metadata mapping informations of a class which describes how a class should be mapped
* to a relational database. * to a relational database.
* *
* @author Roman Borschel <roman@code-factory.org> * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org
* @version $Revision$ * @since 2.0
* @link www.doctrine-project.org * @version $Revision: 3938 $
* @since 2.0 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/ */
class ClassMetadataFactory class ClassMetadataFactory
{ {
......
...@@ -169,8 +169,10 @@ class AnnotationDriver implements Driver ...@@ -169,8 +169,10 @@ class AnnotationDriver implements Driver
$mapping['type'] = $columnAnnot->type; $mapping['type'] = $columnAnnot->type;
$mapping['length'] = $columnAnnot->length; $mapping['length'] = $columnAnnot->length;
$mapping['fixed'] = $columnAnnot->fixed;
$mapping['precision'] = $columnAnnot->precision; $mapping['precision'] = $columnAnnot->precision;
$mapping['scale'] = $columnAnnot->scale; $mapping['scale'] = $columnAnnot->scale;
$mapping['unsigned'] = $columnAnnot->unsigned;
$mapping['nullable'] = $columnAnnot->nullable; $mapping['nullable'] = $columnAnnot->nullable;
$mapping['options'] = $columnAnnot->options; $mapping['options'] = $columnAnnot->options;
$mapping['unique'] = $columnAnnot->unique; $mapping['unique'] = $columnAnnot->unique;
......
...@@ -56,8 +56,10 @@ final class JoinColumns extends Annotation {} ...@@ -56,8 +56,10 @@ final class JoinColumns extends Annotation {}
final class Column extends Annotation { final class Column extends Annotation {
public $type; public $type;
public $length; public $length;
public $fixed = false;
public $precision = 0; // The precision for a decimal (exact numeric) column (Applies only for decimal column) public $precision = 0; // The precision for a decimal (exact numeric) column (Applies only for decimal column)
public $scale = 0; // The scale for a decimal (exact numeric) column (Applies only for decimal column) public $scale = 0; // The scale for a decimal (exact numeric) column (Applies only for decimal column)
public $unsigned = false;
public $unique = false; public $unique = false;
public $nullable = false; public $nullable = false;
public $default; public $default;
......
...@@ -125,6 +125,10 @@ class XmlDriver extends AbstractFileDriver ...@@ -125,6 +125,10 @@ class XmlDriver extends AbstractFileDriver
$mapping['length'] = (int)$fieldMapping['length']; $mapping['length'] = (int)$fieldMapping['length'];
} }
if (isset($fieldMapping['fixed'])) {
$mapping['fixed'] = (bool)$fieldMapping['fixed'];
}
if (isset($fieldMapping['precision'])) { if (isset($fieldMapping['precision'])) {
$mapping['precision'] = (int)$fieldMapping['precision']; $mapping['precision'] = (int)$fieldMapping['precision'];
} }
...@@ -133,6 +137,10 @@ class XmlDriver extends AbstractFileDriver ...@@ -133,6 +137,10 @@ class XmlDriver extends AbstractFileDriver
$mapping['scale'] = (int)$fieldMapping['scale']; $mapping['scale'] = (int)$fieldMapping['scale'];
} }
if (isset($fieldMapping['unsigned'])) {
$mapping['unsigned'] = (bool)$fieldMapping['unsigned'];
}
if (isset($fieldMapping['unique'])) { if (isset($fieldMapping['unique'])) {
$mapping['unique'] = (bool)$fieldMapping['unique']; $mapping['unique'] = (bool)$fieldMapping['unique'];
} }
......
...@@ -128,6 +128,10 @@ class YamlDriver extends AbstractFileDriver ...@@ -128,6 +128,10 @@ class YamlDriver extends AbstractFileDriver
$mapping['length'] = $fieldMapping['length']; $mapping['length'] = $fieldMapping['length'];
} }
if (isset($fieldMapping['fixed'])) {
$mapping['fixed'] = (bool)$fieldMapping['fixed'];
}
if (isset($fieldMapping['precision'])) { if (isset($fieldMapping['precision'])) {
$mapping['precision'] = $fieldMapping['precision']; $mapping['precision'] = $fieldMapping['precision'];
} }
...@@ -136,6 +140,10 @@ class YamlDriver extends AbstractFileDriver ...@@ -136,6 +140,10 @@ class YamlDriver extends AbstractFileDriver
$mapping['scale'] = $fieldMapping['scale']; $mapping['scale'] = $fieldMapping['scale'];
} }
if (isset($fieldMapping['unsigned'])) {
$mapping['unsigned'] = (bool)$fieldMapping['unsigned'];
}
if (isset($fieldMapping['unique'])) { if (isset($fieldMapping['unique'])) {
$mapping['unique'] = (bool)$fieldMapping['unique']; $mapping['unique'] = (bool)$fieldMapping['unique'];
} }
......
...@@ -33,8 +33,13 @@ use Doctrine\Common\DoctrineException, ...@@ -33,8 +33,13 @@ use Doctrine\Common\DoctrineException,
* Specifies that the schema of the classes should be updated. * Specifies that the schema of the classes should be updated.
* *
* *
* @author robo * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 2.0 * @link www.doctrine-project.org
* @since 2.0
* @version $Revision: 3938 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/ */
class SchemaToolTask extends AbstractTask class SchemaToolTask extends AbstractTask
{ {
......
...@@ -497,20 +497,95 @@ class SchemaTool ...@@ -497,20 +497,95 @@ class SchemaTool
$newClasses[] = $class; $newClasses[] = $class;
} else { } else {
$newFields = array(); $newFields = array();
$updateFields = array();
$dropIndexes = array();
$newJoinColumns = array(); $newJoinColumns = array();
$currentColumns = $sm->listTableColumns($tableName); $currentColumns = $sm->listTableColumns($tableName);
foreach ($class->fieldMappings as $fieldMapping) { foreach ($class->fieldMappings as $fieldName => $fieldMapping) {
$exists = false; $exists = false;
foreach ($currentColumns as $index => $column) { foreach ($currentColumns as $index => $column) {
if ($column['name'] == $fieldMapping['columnName']) { if ($column['name'] == $fieldMapping['columnName']) {
// Column exists, check for changes // Column exists, check for changes
$columnInfo = $column;
$columnChanged = false;
echo $column['name'] . ' ';
// 1. check for nullability change // 1. check for nullability change
$columnInfo['notnull'] = ( ! isset($columnInfo['notnull']))
? false : $columnInfo['notnull'];
$notnull = ! $class->isNullable($fieldName);
if ($columnInfo['notnull'] != $notnull) {
$columnInfo['notnull'] = $notnull;
$columnChanged = true;
}
unset($notnull);
// 2. check for uniqueness change // 2. check for uniqueness change
// 3. check for length change if type string $columnInfo['unique'] = ( ! isset($columnInfo['unique']))
// 4. check for type change ? 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);
// 4. check for length change
// 5. check for scale and precision change
/*if (isset($fieldMapping['scale'])) {
$columnInfo['length'] = $fieldMapping['precision'];
$columnInfo['scale'] = $fieldMapping['scale'];
} else {
$columnInfo['length'] = $fieldMapping['length'];
}*/
// 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;
}
// 7. check for unsigned change
$fieldMapping['unsigned'] = ( ! isset($fieldMapping['unsigned']))
? false : $fieldMapping['unsigned'];
if ($columnInfo['unsigned'] != $fieldMapping['unsigned']) {
$columnInfo['unsigned'] = $fieldMapping['unsigned'];
$columnChanged = true;
}
// Only add to column changed list if it was actually changed
if ($columnChanged) {
$updateFields[] = $columnInfo;
}
//var_dump($columnInfo);
echo PHP_EOL . PHP_EOL;
unset($currentColumns[$index]); unset($currentColumns[$index]);
$exists = true; $exists = true;
...@@ -550,6 +625,14 @@ class SchemaTool ...@@ -550,6 +625,14 @@ class SchemaTool
} }
} }
// Drop indexes
if ($dropIndexes) {
foreach ($dropIndexes as $dropIndex) {
$sql[] = $this->_platform->getDropIndexSql($tableName, $dropIndex);
}
}
// Create new columns
if ($newFields || $newJoinColumns) { if ($newFields || $newJoinColumns) {
$changes = array(); $changes = array();
...@@ -565,6 +648,20 @@ class SchemaTool ...@@ -565,6 +648,20 @@ class SchemaTool
$sql[] = $this->_platform->getAlterTableSql($tableName, $changes); $sql[] = $this->_platform->getAlterTableSql($tableName, $changes);
} }
// 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);
}
// Drop any remaining columns // Drop any remaining columns
if ($currentColumns) { if ($currentColumns) {
$changes = array(); $changes = array();
......
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