From cc741566fbd2d0b4b0dd42435c3ba166e315adc1 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 31 Aug 2023 09:15:50 -0400 Subject: [PATCH] - Bugfixes made while working on lean/console database migrations --- src/Adapter/DefaultAdapterTrait.php | 35 +++++++++++++++++++++++--- src/Adapter/MsSQL.php | 25 ------------------ src/Adapter/MySQL.php | 24 ------------------ src/Adapter/SQLite.php | 19 -------------- src/Entity/InformationSchema/Table.php | 2 +- src/Migration/FieldDefinition.php | 12 ++++----- 6 files changed, 39 insertions(+), 78 deletions(-) diff --git a/src/Adapter/DefaultAdapterTrait.php b/src/Adapter/DefaultAdapterTrait.php index 1d9f75b..253532f 100644 --- a/src/Adapter/DefaultAdapterTrait.php +++ b/src/Adapter/DefaultAdapterTrait.php @@ -2,7 +2,12 @@ namespace Ulmus\Adapter; -use Ulmus\{ConnectionAdapter, Entity\InformationSchema\Table, Migration\FieldDefinition, Repository, QueryBuilder}; +use Ulmus\{ConnectionAdapter, + Entity\InformationSchema\Table, + Migration\FieldDefinition, + Repository, + QueryBuilder, + Ulmus}; trait DefaultAdapterTrait { @@ -103,8 +108,8 @@ trait DefaultAdapterTrait public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable { - if ($field['previous']) { - $position = sprintf('AFTER %s', $this->escapeIdentifier($field['previous']['field'], AdapterInterface::IDENTIFIER_FIELD)); + if (! empty($field['previous']) ) { + $position = sprintf('AFTER %s', $this->escapeIdentifier($field['previous']->field, AdapterInterface::IDENTIFIER_FIELD)); } else { $position = "FIRST"; @@ -118,4 +123,28 @@ trait DefaultAdapterTrait $position, ]); } + + public function writableValue(mixed $value) : mixed + { + switch (true) { + case $value instanceof \UnitEnum: + return Ulmus::convertEnum($value); + + case is_object($value): + return Ulmus::convertObject($value); + + case is_array($value): + return json_encode($value); + + case is_bool($value): + return (int) $value; + } + + return $value; + } + + public function splitAlterQuery() : bool + { + return false; + } } diff --git a/src/Adapter/MsSQL.php b/src/Adapter/MsSQL.php index 311605d..ed7fc6a 100644 --- a/src/Adapter/MsSQL.php +++ b/src/Adapter/MsSQL.php @@ -218,26 +218,6 @@ class MsSQL implements AdapterInterface { } } - - public function writableValue(mixed $value) /*: mixed*/ - { - switch (true) { - case $value instanceof \UnitEnum: - return Ulmus::convertEnum($value); - - case is_object($value): - return Ulmus::convertObject($value); - - case is_array($value): - return json_encode($value); - - case is_bool($value): - return (int) $value; - } - - return $value; - } - public function defaultEngine(): ? string { return null; @@ -252,9 +232,4 @@ class MsSQL implements AdapterInterface { { return QueryBuilder\MssqlQueryBuilder::class; } - - public function splitAlterQuery() : bool - { - return false; - } } diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index 50179d5..93bd9e8 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -152,32 +152,8 @@ class MySQL implements AdapterInterface { } } - public function writableValue(mixed $value) : mixed - { - switch (true) { - case $value instanceof \UnitEnum: - return Ulmus::convertEnum($value); - - case is_object($value): - return Ulmus::convertObject($value); - - case is_array($value): - return json_encode($value); - - case is_bool($value): - return (int) $value; - } - - return $value; - } - public function defaultEngine(): ? string { return "InnoDB"; } - - public function splitAlterQuery() : bool - { - return false; - } } diff --git a/src/Adapter/SQLite.php b/src/Adapter/SQLite.php index 05464dd..5a90d55 100644 --- a/src/Adapter/SQLite.php +++ b/src/Adapter/SQLite.php @@ -137,25 +137,6 @@ class SQLite implements AdapterInterface { return $typeOnly ? $type : $type . ( $length ? "($length" . ( isset($precision) ? ",$precision" : "" ) . ")" : "" ); } - public function writableValue(mixed $value) /*: mixed*/ - { - switch (true) { - case $value instanceof \UnitEnum: - return Ulmus::convertEnum($value); - - case is_object($value): - return Ulmus::convertObject($value); - - case is_array($value): - return json_encode($value); - - case is_bool($value): - return (int) $value; - } - - return $value; - } - public function tableSyntax() : array { return [ diff --git a/src/Entity/InformationSchema/Table.php b/src/Entity/InformationSchema/Table.php index 384837a..17c306b 100644 --- a/src/Entity/InformationSchema/Table.php +++ b/src/Entity/InformationSchema/Table.php @@ -83,6 +83,6 @@ class Table public ? string $temporary; #[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)] - #[Where('TABLE_SCHEMA', [ Column::class, 'tableSchema' ])] + #[Where('TABLE_SCHEMA', fieldValue: [ Column::class, 'tableSchema' ])] public EntityCollection $columns; } \ No newline at end of file diff --git a/src/Migration/FieldDefinition.php b/src/Migration/FieldDefinition.php index 7a67969..5bf9a4f 100644 --- a/src/Migration/FieldDefinition.php +++ b/src/Migration/FieldDefinition.php @@ -90,14 +90,14 @@ class FieldDefinition { public function getDefault() : mixed { - if ( isset($this->default) ) { - if (is_bool($this->default)) { - return (int) $this->default; + if (isset($this->default)) { + $value = $this->adapter->writableValue($this->default); + + if (is_string($value) ) { + $value = $this->adapter->escapeIdentifier($value, AdapterInterface::IDENTIFIER_VALUE); } - # Specific default cases here .. - - return $this->default; + return $value; } # Fallback on attribute's default value