Skip to content
Merged
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
36 changes: 35 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
class MysqlAdapter extends AbstractAdapter
{
/**
* Maximum length for identifiers (table names, column names, constraint names, etc.)
*/
protected const IDENTIFIER_MAX_LENGTH = 64;

/**
* @var string[]
*/
Expand Down Expand Up @@ -1197,7 +1202,7 @@ protected function getIndexSqlDefinition(Index $index): string
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$constraintName = $foreignKey->getName() ?: ($tableName . '_' . implode('_', $foreignKey->getColumns()));
$constraintName = $foreignKey->getName() ?: $this->getUniqueForeignKeyName($tableName, $foreignKey->getColumns());
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName);
$columnNames = [];
foreach ($foreignKey->getColumns() as $column) {
Expand All @@ -1221,6 +1226,35 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
return $def;
}

/**
* Generate a unique foreign key constraint name.
*
* @param string $tableName Table name
* @param array<string> $columns Column names
* @return string
*/
protected function getUniqueForeignKeyName(string $tableName, array $columns): string
{
$baseName = $tableName . '_' . implode('_', $columns);
$maxLength = static::IDENTIFIER_MAX_LENGTH - 3;
if (strlen($baseName) > $maxLength) {
$baseName = substr($baseName, 0, $maxLength);
}
$existingKeys = $this->getForeignKeys($tableName);
$existingNames = array_column($existingKeys, 'name');

if (!in_array($baseName, $existingNames, true)) {
return $baseName;
}

$counter = 2;
while (in_array($baseName . '_' . $counter, $existingNames, true)) {
$counter++;
}

return $baseName . '_' . $counter;
}

/**
* Returns MySQL column types (inherited and MySQL specified).
*
Expand Down
41 changes: 36 additions & 5 deletions src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

class PostgresAdapter extends AbstractAdapter
{
/**
* Maximum length for identifiers (table names, column names, constraint names, etc.)
*/
protected const IDENTIFIER_MAX_LENGTH = 63;

public const GENERATED_ALWAYS = 'ALWAYS';
public const GENERATED_BY_DEFAULT = 'BY DEFAULT';
/**
Expand Down Expand Up @@ -949,11 +954,7 @@ protected function getIndexSqlDefinition(Index $index, string $tableName): strin
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$parts = $this->getSchemaName($tableName);

$constraintName = $foreignKey->getName() ?: (
$parts['table'] . '_' . implode('_', $foreignKey->getColumns()) . '_fkey'
);
$constraintName = $foreignKey->getName() ?: $this->getUniqueForeignKeyName($tableName, $foreignKey->getColumns());
$columnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getColumns()));
$refColumnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getReferencedColumns()));
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName) .
Expand All @@ -972,6 +973,36 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
return $def;
}

/**
* Generate a unique foreign key constraint name.
*
* @param string $tableName Table name
* @param array<string> $columns Column names
* @return string
*/
protected function getUniqueForeignKeyName(string $tableName, array $columns): string
{
$parts = $this->getSchemaName($tableName);
$baseName = $parts['table'] . '_' . implode('_', $columns) . '_fkey';
$maxLength = static::IDENTIFIER_MAX_LENGTH - 3;
if (strlen($baseName) > $maxLength) {
$baseName = substr($baseName, 0, $maxLength);
}
$existingKeys = $this->getForeignKeys($tableName);
$existingNames = array_column($existingKeys, 'name');

if (!in_array($baseName, $existingNames, true)) {
return $baseName;
}

$counter = 2;
while (in_array($baseName . '_' . $counter, $existingNames, true)) {
$counter++;
}

return $baseName . '_' . $counter;
}

/**
* @inheritDoc
*/
Expand Down
27 changes: 26 additions & 1 deletion src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ public function getColumnTypes(): array
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$constraintName = $foreignKey->getName() ?: ($tableName . '_' . implode('_', $foreignKey->getColumns()));
$constraintName = $foreignKey->getName() ?: $this->getUniqueForeignKeyName($tableName, $foreignKey->getColumns());
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName);
$columnNames = [];
foreach ($foreignKey->getColumns() as $column) {
Expand All @@ -1697,6 +1697,31 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
return $def;
}

/**
* Generate a unique foreign key constraint name.
*
* @param string $tableName Table name
* @param array<string> $columns Column names
* @return string
*/
protected function getUniqueForeignKeyName(string $tableName, array $columns): string
{
$baseName = $tableName . '_' . implode('_', $columns);
$existingKeys = $this->getForeignKeys($tableName);
$existingNames = array_column($existingKeys, 'name');

if (!in_array($baseName, $existingNames, true)) {
return $baseName;
}

$counter = 2;
while (in_array($baseName . '_' . $counter, $existingNames, true)) {
$counter++;
}

return $baseName . '_' . $counter;
}

/**
* @inheritDoc
*/
Expand Down
36 changes: 35 additions & 1 deletion src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
class SqlserverAdapter extends AbstractAdapter
{
/**
* Maximum length for identifiers (table names, column names, constraint names, etc.)
*/
protected const IDENTIFIER_MAX_LENGTH = 128;

/**
* @var string[]
*/
Expand Down Expand Up @@ -864,7 +869,7 @@ protected function getIndexSqlDefinition(Index $index, string $tableName): strin
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$constraintName = $foreignKey->getName() ?: $tableName . '_' . implode('_', $foreignKey->getColumns());
$constraintName = $foreignKey->getName() ?: $this->getUniqueForeignKeyName($tableName, $foreignKey->getColumns());
$columnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getColumns()));
$refColumnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getReferencedColumns()));

Expand All @@ -881,6 +886,35 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
return $def;
}

/**
* Generate a unique foreign key constraint name.
*
* @param string $tableName Table name
* @param array<string> $columns Column names
* @return string
*/
protected function getUniqueForeignKeyName(string $tableName, array $columns): string
{
$baseName = $tableName . '_' . implode('_', $columns);
$maxLength = static::IDENTIFIER_MAX_LENGTH - 3;
if (strlen($baseName) > $maxLength) {
$baseName = substr($baseName, 0, $maxLength);
}
$existingKeys = $this->getForeignKeys($tableName);
$existingNames = array_column($existingKeys, 'name');

if (!in_array($baseName, $existingNames, true)) {
return $baseName;
}

$counter = 2;
while (in_array($baseName . '_' . $counter, $existingNames, true)) {
$counter++;
}

return $baseName . '_' . $counter;
}

/**
* Creates the specified schema.
*
Expand Down