Skip to content

Draft: #2341 PDOAdapter : cast dates #2346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 0.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Phinx/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@ public function dropSchema(string $schemaName): void;
* @return mixed
*/
public function castToBool(mixed $value): mixed;

/**
* @param string|null $value Date & time, in iso-format (Y-m-d H:i:s)
* @return string
*/
public function castToDate(?string $value);
}
8 changes: 8 additions & 0 deletions src/Phinx/Db/Adapter/AdapterWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ public function castToBool($value): mixed
return $this->getAdapter()->castToBool($value);
}

/**
* @inheritDoc
*/
public function castToDate($value)
{
return $this->getAdapter()->castToDate($value);
}

/**
* @return \PDO
*/
Expand Down
19 changes: 17 additions & 2 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,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);
}
}

Expand Down Expand Up @@ -432,6 +434,8 @@ public function bulkinsert(Table $table, array $rows): void
continue;
} elseif (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;
}
Expand Down Expand Up @@ -517,8 +521,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)
);

Expand Down Expand Up @@ -683,6 +687,15 @@ public function castToBool($value): mixed
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
*
Expand Down Expand Up @@ -711,6 +724,8 @@ protected function getDefaultValueDefinition(mixed $default, string|Literal|null
$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 && (string)$columnType === static::PHINX_TYPE_BOOLEAN) {
$default = $this->castToBool((bool)$default);
}
Expand Down
Loading