Skip to content
Open
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
20 changes: 20 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ method instead:
- `Column::setComment()` - use `ColumnEditor::setComment()`.
- `Column::setValues()` - use `ColumnEditor::setValues()`.

## Deprecated `Column::getType()`, `Column::setType()` and passing a `Type` instance to `Column::__construct()`

`Column` now stores the DBAL type name (string) as source of truth. Use
`Column::getTypeName()` / `Column::setTypeName()` and pass the type name to
the constructor instead of a `Type` instance.

```diff
-use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

-$column = new Column('id', Type::getType(Types::INTEGER));
+$column = new Column('id', Types::INTEGER);

-$typeName = Type::getTypeRegistry()->lookupName($column->getType());
+$typeName = $column->getTypeName();
```

Libraries that need to support older DBAL versions can keep passing a `Type`
instance; the deprecated path stays functional until the next major.

## Deprecated `Table` features

The `Table` constructor has been marked as internal. Use `Table::editor()` to instantiate an editor and
Expand Down
8 changes: 4 additions & 4 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$queryParts = [];

foreach ($diff->getAddedColumns() as $column) {
$columnProperties = array_merge($column->toArray(), [
$columnProperties = array_merge($column->toArray(true), [
'comment' => $column->getComment(),
]);

Expand All @@ -383,7 +383,7 @@ public function getAlterTableSQL(TableDiff $diff): array
foreach ($diff->getChangedColumns() as $columnDiff) {
$newColumn = $columnDiff->getNewColumn();

$newColumnProperties = array_merge($newColumn->toArray(), [
$newColumnProperties = array_merge($newColumn->toArray(true), [
'comment' => $newColumn->getComment(),
]);

Expand Down Expand Up @@ -536,7 +536,7 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde
$column->setAutoincrement(false);

$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray(true));

// original autoincrement information might be needed later on by other parts of the table alteration
$column->setAutoincrement(true);
Expand Down Expand Up @@ -593,7 +593,7 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff): array
$column->setAutoincrement(false);

$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray(true));

// Restore the autoincrement attribute as it might be needed later on
// by other parts of the table alteration.
Expand Down
21 changes: 16 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,15 @@ final protected function getCreateTableWithoutForeignKeysSQL(Table $table): arra
return $this->buildCreateTableSQL($table, false);
}

/**
* Resolves the DBAL type instance for a column type name.
*/
protected function getType(string $typeName): Type
{
// @phpstan-ignore missingType.checkedException
return Type::getType($typeName);
}

/** @return list<string> */
private function buildCreateTableSQL(Table $table, bool $createForeignKeys): array
{
Expand Down Expand Up @@ -1493,7 +1502,7 @@ public function getColumnDeclarationSQL(string $name, array $column): string

$notnull = ! empty($column['notnull']) ? ' NOT NULL' : '';

$typeDecl = $column['type']->getSQLDeclaration($column, $this);
$typeDecl = $this->getType($column['typeName'])->getSQLDeclaration($column, $this);
$declaration = $typeDecl . $charset . $default . $notnull . $collation;

if ($this->supportsInlineColumnComments() && isset($column['comment']) && $column['comment'] !== '') {
Expand Down Expand Up @@ -1544,12 +1553,14 @@ public function getDefaultValueDeclarationSQL(array $column): string
return ' DEFAULT ' . $default->toSQL($this);
}

if (! isset($column['type'])) {
if (isset($column['typeName']) && is_string($column['typeName'])) {
$type = $this->getType($column['typeName']);
} elseif (isset($column['type']) && $column['type'] instanceof Type) {
$type = $column['type'];
} else {
return " DEFAULT '" . $default . "'";
}

$type = $column['type'];

if ($type instanceof Types\PhpIntegerMappingType) {
return ' DEFAULT ' . $default;
}
Expand Down Expand Up @@ -2382,7 +2393,7 @@ final public function escapeStringForLike(string $inputString, string $escapeCha
*/
private function columnToArray(Column $column): array
{
return array_merge($column->toArray(), [
return array_merge($column->toArray(true), [
'name' => $column->getQuotedName($this),
'version' => $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false,
'comment' => $column->getComment(),
Expand Down
18 changes: 14 additions & 4 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\PhpDateTimeMappingType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\Deprecation;

Expand All @@ -27,6 +28,7 @@
use function current;
use function explode;
use function implode;
use function is_string;
use function sprintf;
use function str_contains;

Expand Down Expand Up @@ -284,7 +286,7 @@ public function getAlterTableSQL(TableDiff $diff): array

$queryParts = [];
foreach ($diff->getAddedColumns() as $column) {
$columnDef = $column->toArray();
$columnDef = $column->toArray(true);
$queryPart = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnDef);

// Adding non-nullable columns to a table requires a default value to be specified.
Expand Down Expand Up @@ -400,7 +402,7 @@ private function gatherAlterColumnSQL(
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff, bool &$needsReorg): array
{
$newColumn = $columnDiff->getNewColumn();
$columnArray = $newColumn->toArray();
$columnArray = $newColumn->toArray(true);

$newName = $columnDiff->getNewColumn()->getQuotedName($this);
$oldName = $columnDiff->getOldColumn()->getQuotedName($this);
Expand All @@ -427,7 +429,7 @@ private function getAlterColumnClausesSQL(ColumnDiff $columnDiff, bool &$needsRe
$columnDiff->hasFixedChanged()
) {
$needsReorg = true;
$clauses[] = $alterClause . ' SET DATA TYPE ' . $newColumn->getType()
$clauses[] = $alterClause . ' SET DATA TYPE ' . $this->getType($newColumn->getTypeName())
->getSQLDeclaration($columnArray, $this);
}

Expand Down Expand Up @@ -484,7 +486,15 @@ public function getDefaultValueDeclarationSQL(array $column): string
'The "version" column platform option is deprecated.',
);

if ($column['type'] instanceof PhpDateTimeMappingType) {
if (isset($column['typeName']) && is_string($column['typeName'])) {
$type = $this->getType($column['typeName']);
} elseif (isset($column['type']) && $column['type'] instanceof Type) {
$type = $column['type'];
} else {
$type = null;
}

if ($type instanceof PhpDateTimeMappingType) {
$column['default'] = '1';
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Platforms/MariaDBPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ public function getColumnDeclarationSQL(string $name, array $column): string
{
// MariaDb forces column collation to utf8mb4_bin where the column was declared as JSON so ignore
// collation and character set for json columns as attempting to set them can cause an error.
if ($this->getJsonTypeDeclarationSQL([]) === 'JSON' && ($column['type'] ?? null) instanceof JsonType) {
if (
$this->getJsonTypeDeclarationSQL([]) === 'JSON'
&& isset($column['typeName'])
&& $this->getType($column['typeName']) instanceof JsonType
) {
unset($column['collation']);
unset($column['charset']);
}
Expand Down
15 changes: 13 additions & 2 deletions src/Platforms/MySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Doctrine\DBAL\SQL\Builder\WithSQLBuilder;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;

use function is_string;

/**
* Provides the behavior, features and SQL dialect of the Oracle MySQL database platform
* of the oldest supported version.
Expand All @@ -30,8 +33,16 @@ class MySQLPlatform extends AbstractMySQLPlatform
*/
public function getDefaultValueDeclarationSQL(array $column): string
{
if ($column['type'] instanceof TextType || $column['type'] instanceof BlobType) {
unset($column['default']);
if (isset($column['typeName']) && is_string($column['typeName'])) {
$type = $this->getType($column['typeName']);
} elseif (isset($column['type']) && $column['type'] instanceof Type) {
$type = $column['type'];
} else {
$type = null;
}

if ($type instanceof TextType || $type instanceof BlobType) {
$column['default'] = null;
}

return parent::getDefaultValueDeclarationSQL($column);
Expand Down
10 changes: 5 additions & 5 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

foreach ($diff->getAddedColumns() as $column) {
$addColumnSQL[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$addColumnSQL[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray(true));
$comment = $column->getComment();

if ($comment !== '') {
Expand Down Expand Up @@ -614,7 +614,7 @@ public function getAlterTableSQL(TableDiff $diff): array
// Oracle only supports binary type columns with variable length.
// Avoids unnecessary table alteration statements.
if (
$newColumn->getType() instanceof BinaryType &&
$this->getType($newColumn->getTypeName()) instanceof BinaryType &&
$columnDiff->hasFixedChanged() &&
$countChangedProperties === 1
) {
Expand All @@ -627,9 +627,9 @@ public function getAlterTableSQL(TableDiff $diff): array
* Do not add query part if only comment has changed
*/
if ($countChangedProperties > ($columnHasChangedComment ? 1 : 0)) {
$newColumnProperties = $newColumn->toArray();
$newColumnProperties = $newColumn->toArray(true);

$oldSQL = $this->getColumnDeclarationSQL('', $oldColumn->toArray());
$oldSQL = $this->getColumnDeclarationSQL('', $oldColumn->toArray(true));
$newSQL = $this->getColumnDeclarationSQL('', $newColumnProperties);

if ($newSQL !== $oldSQL) {
Expand Down Expand Up @@ -690,7 +690,7 @@ public function getColumnDeclarationSQL(string $name, array $column): string
$notnull = $column['notnull'] ? ' NOT NULL' : ' NULL';
}

$typeDecl = $column['type']->getSQLDeclaration($column, $this);
$typeDecl = $this->getType($column['typeName'])->getSQLDeclaration($column, $this);
$declaration = $typeDecl . $default . $notnull;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function getAlterTableSQL(TableDiff $diff): array
foreach ($diff->getAddedColumns() as $addedColumn) {
$query = 'ADD ' . $this->getColumnDeclarationSQL(
$addedColumn->getQuotedName($this),
$addedColumn->toArray(),
$addedColumn->toArray(true),
);

$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' ' . $query;
Expand Down Expand Up @@ -272,7 +272,7 @@ public function getAlterTableSQL(TableDiff $diff): array
if ($columnDiff->hasDefaultChanged()) {
$defaultClause = $newColumn->getDefault() === null
? ' DROP DEFAULT'
: ' SET' . $this->getDefaultValueDeclarationSQL($newColumn->toArray());
: ' SET' . $this->getDefaultValueDeclarationSQL($newColumn->toArray(true));

$query = 'ALTER ' . $newColumnName . $defaultClause;
$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' ' . $query;
Expand Down Expand Up @@ -312,10 +312,10 @@ public function getAlterTableSQL(TableDiff $diff): array

private function getTypeSQLDeclaration(Column $column): string
{
$type = $column->getType();
$type = $this->getType($column->getTypeName());

// SERIAL/BIGSERIAL are not "real" types and we can't alter a column to that type
$columnDefinition = $column->toArray();
$columnDefinition = $column->toArray(true);
$columnDefinition['autoincrement'] = false;

return $type->getSQLDeclaration($columnDefinition, $this);
Expand Down
18 changes: 10 additions & 8 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
/**
* Provides the behavior, features and SQL dialect of the Microsoft SQL Server database platform
* of the oldest supported version.
*
* @phpstan-import-type ColumnProperties from Column
*/
class SQLServerPlatform extends AbstractPlatform
{
Expand Down Expand Up @@ -325,7 +327,7 @@ protected function getCreateColumnCommentSQL(string $tableName, string $columnNa
*
* @internal The method should be only used by the {@see SQLServerPlatform} class.
*
* @param mixed[] $column Column definition.
* @param ColumnProperties $column Column definition.
*/
protected function getDefaultConstraintDeclarationSQL(array $column): string
{
Expand Down Expand Up @@ -393,7 +395,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$tableName = $table->getName();

foreach ($diff->getAddedColumns() as $column) {
$columnProperties = $column->toArray();
$columnProperties = $column->toArray(true);

$addColumnSql = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnProperties);

Expand Down Expand Up @@ -469,8 +471,8 @@ public function getAlterTableSQL(TableDiff $diff): array

$columnNameSQL = $newColumn->getQuotedName($this);

$newDeclarationSQL = $this->getColumnDeclarationSQL($columnNameSQL, $newColumn->toArray());
$oldDeclarationSQL = $this->getColumnDeclarationSQL($columnNameSQL, $oldColumn->toArray());
$newDeclarationSQL = $this->getColumnDeclarationSQL($columnNameSQL, $newColumn->toArray(true));
$oldDeclarationSQL = $this->getColumnDeclarationSQL($columnNameSQL, $oldColumn->toArray(true));
$declarationSQLChanged = $newDeclarationSQL !== $oldDeclarationSQL;

$defaultChanged = $columnDiff->hasDefaultChanged();
Expand Down Expand Up @@ -522,7 +524,7 @@ public function getRenameTableSQL(string $oldName, string $newName): string
*/
private function getAlterTableAddDefaultConstraintClause(string $tableName, Column $column): string
{
$columnDef = $column->toArray();
$columnDef = $column->toArray(true);
$columnDef['name'] = $column->getQuotedName($this);

return 'ADD' . $this->getDefaultConstraintDeclarationSQL($columnDef);
Expand Down Expand Up @@ -1244,7 +1246,7 @@ public function getColumnDeclarationSQL(string $name, array $column): string

$notnull = ! empty($column['notnull']) ? ' NOT NULL' : '';

$typeDecl = $column['type']->getSQLDeclaration($column, $this);
$typeDecl = $this->getType($column['typeName'])->getSQLDeclaration($column, $this);
$declaration = $typeDecl . $collation . $notnull;
}

Expand All @@ -1265,8 +1267,8 @@ public function columnsEqual(Column $column1, Column $column2): bool
return false;
}

return $this->getDefaultValueDeclarationSQL($column1->toArray())
=== $this->getDefaultValueDeclarationSQL($column2->toArray());
return $this->getDefaultValueDeclarationSQL($column1->toArray(true))
=== $this->getDefaultValueDeclarationSQL($column2->toArray(true));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,9 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false
$sql = [];

foreach ($diff->getAddedColumns() as $column) {
$definition = $column->toArray();
$definition = $column->toArray(true);

$type = $definition['type'];
$type = $this->getType($definition['typeName']);

switch (true) {
case isset($definition['columnDefinition']):
Expand Down
Loading