1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

[2.0][DDC-64] Fixing issue with converting D1 schema clob to D2

This commit is contained in:
jwage 2009-11-11 20:44:14 +00:00
parent d047f0e25e
commit 2a663bee05
3 changed files with 34 additions and 17 deletions

View file

@ -21,7 +21,8 @@
namespace Doctrine\ORM\Tools; namespace Doctrine\ORM\Tools;
use Doctrine\ORM\Mapping\ClassMetadataInfo, use Doctrine\Common\DoctrineException,
Doctrine\ORM\Mapping\ClassMetadataInfo,
Doctrine\ORM\Tools\Export\Driver\AbstractExporter, Doctrine\ORM\Tools\Export\Driver\AbstractExporter,
Doctrine\Common\Util\Inflector; Doctrine\Common\Util\Inflector;
@ -45,6 +46,11 @@ if ( ! class_exists('sfYaml', false)) {
*/ */
class ConvertDoctrine1Schema class ConvertDoctrine1Schema
{ {
private $_legacyTypeMap = array(
// TODO: This list may need to be updated
'clob' => 'text'
);
/** /**
* Constructor passes the directory or array of directories * Constructor passes the directory or array of directories
* to convert the Doctrine 1 schema files from * to convert the Doctrine 1 schema files from
@ -78,26 +84,26 @@ class ConvertDoctrine1Schema
} }
$metadatas = array(); $metadatas = array();
foreach ($schema as $name => $model) { foreach ($schema as $className => $mappingInformation) {
$metadatas[] = $this->_convertToClassMetadataInfo($name, $model); $metadatas[] = $this->_convertToClassMetadataInfo($className, $mappingInformation);
} }
return $metadatas; return $metadatas;
} }
private function _convertToClassMetadataInfo($modelName, $model) private function _convertToClassMetadataInfo($className, $mappingInformation)
{ {
$metadata = new ClassMetadataInfo($modelName); $metadata = new ClassMetadataInfo($className);
$this->_convertTableName($modelName, $model, $metadata); $this->_convertTableName($className, $mappingInformation, $metadata);
$this->_convertColumns($modelName, $model, $metadata); $this->_convertColumns($className, $mappingInformation, $metadata);
$this->_convertIndexes($modelName, $model, $metadata); $this->_convertIndexes($className, $mappingInformation, $metadata);
$this->_convertRelations($modelName, $model, $metadata); $this->_convertRelations($className, $mappingInformation, $metadata);
return $metadata; return $metadata;
} }
private function _convertTableName($modelName, array $model, ClassMetadataInfo $metadata) private function _convertTableName($className, array $model, ClassMetadataInfo $metadata)
{ {
if (isset($model['tableName']) && $model['tableName']) { if (isset($model['tableName']) && $model['tableName']) {
$e = explode('.', $model['tableName']); $e = explode('.', $model['tableName']);
@ -110,13 +116,13 @@ class ConvertDoctrine1Schema
} }
} }
private function _convertColumns($modelName, array $model, ClassMetadataInfo $metadata) private function _convertColumns($className, array $model, ClassMetadataInfo $metadata)
{ {
$id = false; $id = false;
if (isset($model['columns']) && $model['columns']) { if (isset($model['columns']) && $model['columns']) {
foreach ($model['columns'] as $name => $column) { foreach ($model['columns'] as $name => $column) {
$fieldMapping = $this->_convertColumn($modelName, $name, $column, $metadata); $fieldMapping = $this->_convertColumn($className, $name, $column, $metadata);
if (isset($fieldMapping['id']) && $fieldMapping['id']) { if (isset($fieldMapping['id']) && $fieldMapping['id']) {
$id = true; $id = true;
@ -136,7 +142,7 @@ class ConvertDoctrine1Schema
} }
} }
private function _convertColumn($modelName, $name, $column, ClassMetadataInfo $metadata) private function _convertColumn($className, $name, $column, ClassMetadataInfo $metadata)
{ {
if (is_string($column)) { if (is_string($column)) {
$string = $column; $string = $column;
@ -150,6 +156,15 @@ class ConvertDoctrine1Schema
if ( ! isset($column['name'])) { if ( ! isset($column['name'])) {
$column['name'] = $name; $column['name'] = $name;
} }
$column['type'] = strtolower($column['type']);
// check if legacy column type (1.x) needs to be mapped to a 2.0 one
if (isset($this->_legacyTypeMap[$column['type']])) {
$column['type'] = $this->_legacyTypeMap[$column['type']];
}
if ( ! \Doctrine\DBAL\Types\Type::hasType($column['type'])) {
throw DoctrineException::couldNotMapDoctrine1Type($column['type']);
}
$fieldMapping = array(); $fieldMapping = array();
if (isset($column['primary'])) { if (isset($column['primary'])) {
$fieldMapping['id'] = true; $fieldMapping['id'] = true;
@ -187,7 +202,7 @@ class ConvertDoctrine1Schema
return $fieldMapping; return $fieldMapping;
} }
private function _convertIndexes($modelName, array $model, ClassMetadataInfo $metadata) private function _convertIndexes($className, array $model, ClassMetadataInfo $metadata)
{ {
if (isset($model['indexes']) && $model['indexes']) { if (isset($model['indexes']) && $model['indexes']) {
foreach ($model['indexes'] as $name => $index) { foreach ($model['indexes'] as $name => $index) {
@ -202,7 +217,7 @@ class ConvertDoctrine1Schema
} }
} }
private function _convertRelations($modelName, array $model, ClassMetadataInfo $metadata) private function _convertRelations($className, array $model, ClassMetadataInfo $metadata)
{ {
if (isset($model['relations']) && $model['relations']) { if (isset($model['relations']) && $model['relations']) {
foreach ($model['relations'] as $name => $relation) { foreach ($model['relations'] as $name => $relation) {
@ -219,7 +234,7 @@ class ConvertDoctrine1Schema
$relation['foreign'] = 'id'; $relation['foreign'] = 'id';
} }
if ( ! isset($relation['foreignAlias'])) { if ( ! isset($relation['foreignAlias'])) {
$relation['foreignAlias'] = $modelName; $relation['foreignAlias'] = $className;
} }
if (isset($relation['refClass'])) { if (isset($relation['refClass'])) {

View file

@ -57,7 +57,8 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('Profile', $metadatas['Profile']->name); $this->assertEquals('Profile', $metadatas['Profile']->name);
$this->assertEquals('User', $metadatas['User']->name); $this->assertEquals('User', $metadatas['User']->name);
$this->assertEquals(4, count($metadatas['Profile']->fieldMappings)); $this->assertEquals(4, count($metadatas['Profile']->fieldMappings));
$this->assertEquals(3, count($metadatas['User']->fieldMappings)); $this->assertEquals(4, count($metadatas['User']->fieldMappings));
$this->assertEquals('text', $metadatas['User']->fieldMappings['clob']['type']);
$this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName); $this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName);
$this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName); $this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName);

View file

@ -8,6 +8,7 @@ User:
unique: true unique: true
password: password:
type: string(255) type: string(255)
clob: clob
indexes: indexes:
username: username:
fields: [username] fields: [username]