[SCHEMADEF] Bug fixes

This commit is contained in:
Hugo Sales 2020-03-30 17:15:30 +00:00 committed by Hugo Sales
parent d14ac1edf6
commit 57298da60e
1 changed files with 23 additions and 14 deletions

View File

@ -41,7 +41,9 @@ class SchemaDefDriver extends StaticPHPDriver
*/ */
private const types = [ private const types = [
'varchar' => 'string', 'varchar' => 'string',
'char' => 'string', // char is a fixed witdh varchar
'int' => 'integer', 'int' => 'integer',
'serial' => 'integer',
'tinyint' => 'smallint', // no portable tinyint 'tinyint' => 'smallint', // no portable tinyint
'bigint' => 'bigint', 'bigint' => 'bigint',
'bool' => 'boolean', 'bool' => 'boolean',
@ -62,22 +64,25 @@ class SchemaDefDriver extends StaticPHPDriver
]; ];
/** /**
* Fill in the database $metadata for $className * Fill in the database $metadata for $class_name
* *
* @param string $className * @param string $class_name
* @param ClassMetadata $metadata * @param ClassMetadata $metadata
*/ */
public function loadMetadataForClass($className, ClassMetadata $metadata) public function loadMetadataForClass($class_name, ClassMetadata $metadata)
{ {
$schema = $className::schemaDef(); $schema = $class_name::schemaDef();
$metadata->setPrimaryTable(['name' => $schema['name'], $metadata->setPrimaryTable(['name' => $schema['name'],
'indexes' => self::kv_to_name_col($schema['indexes']), 'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),
'uniqueConstraints' => self::kv_to_name_col($schema['unique keys']), 'uniqueConstraints' => self::kv_to_name_col($schema['unique keys'] ?? []),
'options' => ['comment' => $schema['description'] ?? ''], 'options' => ['comment' => $schema['description'] ?? ''],
]); ]);
echo "{$class_name}\n";
foreach ($schema['fields'] as $name => $opts) { foreach ($schema['fields'] as $name => $opts) {
// TODO
// Convert old to new types // Convert old to new types
$type = // $name === 'date' $type = // $name === 'date'
// // Old date fields were stored as int, store as datetime/timestamp // // Old date fields were stored as int, store as datetime/timestamp
@ -88,7 +93,7 @@ class SchemaDefDriver extends StaticPHPDriver
self::types[($opts['size'] ?? '') . $opts['type']]; self::types[($opts['size'] ?? '') . $opts['type']];
$unique = null; $unique = null;
foreach ($schema['unique keys'] as $key => $uniq_arr) { foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) {
if (in_array($name, $uniq_arr)) { if (in_array($name, $uniq_arr)) {
$unique = $key; $unique = $key;
break; break;
@ -115,10 +120,11 @@ class SchemaDefDriver extends StaticPHPDriver
// integer, optional // integer, optional
'scale' => $opts['scale'] ?? null, 'scale' => $opts['scale'] ?? null,
'options' => [ 'options' => [
'comment' => $opts['description'], 'comment' => $opts['description'] ?? null,
'default' => $opts['default'] ?? null, 'default' => $opts['default'] ?? null,
'unsigned' => $opts['unsigned'] ?? null, 'unsigned' => $opts['unsigned'] ?? null,
// 'fixed' => bool, unused // bool, optional
'fixed' => $opts['type'] === 'char',
// 'collation' => string, unused // 'collation' => string, unused
// 'check', unused // 'check', unused
], ],
@ -129,6 +135,9 @@ class SchemaDefDriver extends StaticPHPDriver
$field['options'] = array_filter($field['options'], F\not('is_null')); $field['options'] = array_filter($field['options'], F\not('is_null'));
$metadata->mapField($field); $metadata->mapField($field);
if ($opts['type'] === 'serial') {
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_AUTO);
}
} }
// TODO foreign keys // TODO foreign keys
} }
@ -138,11 +147,11 @@ class SchemaDefDriver extends StaticPHPDriver
* we care about classes that have the method `schemaDef`, * we care about classes that have the method `schemaDef`,
* instead of `loadMetadata`. * instead of `loadMetadata`.
* *
* @param string $className * @param string $class_name
*/ */
public function isTransient($className) public function isTransient($class_name)
{ {
return !method_exists($className, 'schemaDef'); return !method_exists($class_name, 'schemaDef');
} }
/** /**