From c77d46b24e737542dc806d4cd6ed07789ec9d096 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Vares Date: Sun, 2 Mar 2025 18:58:12 +0100 Subject: [PATCH] #2341 - Allow overriding for dates OCI Adapter will override castToDate --- src/Phinx/Db/Adapter/AdapterInterface.php | 6 ++++++ src/Phinx/Db/Adapter/AdapterWrapper.php | 8 ++++++++ src/Phinx/Db/Adapter/PdoAdapter.php | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Db/Adapter/AdapterInterface.php b/src/Phinx/Db/Adapter/AdapterInterface.php index 65d7cc35b..5edc421d3 100644 --- a/src/Phinx/Db/Adapter/AdapterInterface.php +++ b/src/Phinx/Db/Adapter/AdapterInterface.php @@ -502,4 +502,10 @@ public function dropSchema(string $schemaName): void; * @return mixed */ public function castToBool($value); + + /** + * @param string|null $value Date & time, in iso-format (Y-m-d H:i:s) + * @return string + */ + public function castToDate(?string $value); } diff --git a/src/Phinx/Db/Adapter/AdapterWrapper.php b/src/Phinx/Db/Adapter/AdapterWrapper.php index 7df50a8e8..acb3ce356 100644 --- a/src/Phinx/Db/Adapter/AdapterWrapper.php +++ b/src/Phinx/Db/Adapter/AdapterWrapper.php @@ -461,6 +461,14 @@ public function castToBool($value) return $this->getAdapter()->castToBool($value); } + /** + * @inheritDoc + */ + public function castToDate($value) + { + return $this->getAdapter()->castToDate($value); + } + /** * @return \PDO */ diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 73e98a666..3cb2ea7e9 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -262,6 +262,8 @@ public function insert(Table $table, array $row): void foreach ($row as $column => $value) { if (is_bool($value)) { $row[$column] = $this->castToBool($value); + } elseif (preg_match('/^[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/', $value)) { + $row[$column] = $this->castToDate($value); } } @@ -337,6 +339,8 @@ public function bulkinsert(Table $table, array $rows): void foreach ($row as $v) { if (is_bool($v)) { $vals[] = $this->castToBool($v); + } elseif (preg_match('/^[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/', $v)) { + $vals[] = $this->castToDate($v); } else { $vals[] = $v; } @@ -412,8 +416,8 @@ public function migrated(MigrationInterface $migration, string $direction, strin $this->quoteColumnName('breakpoint'), $migration->getVersion(), substr($migration->getName(), 0, 100), - $startTime, - $endTime, + $this->castToDate($startTime), + $this->castToDate($endTime), $this->castToBool(false) ); @@ -578,6 +582,15 @@ public function castToBool($value) return (bool)$value ? 1 : 0; } + /** + * @param string|null $value Date & time, in iso format (Y-m-d H:i:s) + * @return string + */ + public function castToDate(?string $value) + { + return $value; + } + /** * Retrieve a database connection attribute * @@ -606,6 +619,8 @@ protected function getDefaultValueDefinition($default, ?string $columnType = nul $default = $this->getConnection()->quote($default); } elseif (is_bool($default)) { $default = $this->castToBool($default); + } elseif ($default !== null && $columnType === static::PHINX_TYPE_DATETIME) { + $default = $this->castToDate($default); } elseif ($default !== null && $columnType === static::PHINX_TYPE_BOOLEAN) { $default = $this->castToBool((bool)$default); }