Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 5 additions & 6 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ protected function getAddForeignKeyInstructions(TableMetadata $table, ForeignKey
{
$alter = sprintf(
'ADD %s',
$this->getForeignKeySqlDefinition($foreignKey),
$this->getForeignKeySqlDefinition($foreignKey, $table->getName()),
);

return new AlterInstructions([$alter]);
Expand Down Expand Up @@ -1192,14 +1192,13 @@ protected function getIndexSqlDefinition(Index $index): string
* Gets the MySQL Foreign Key Definition for an ForeignKey object.
*
* @param \Migrations\Db\Table\ForeignKey $foreignKey Foreign key
* @param string $tableName Table name for auto-generating constraint name
* @return string
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey): string
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$def = '';
if ($foreignKey->getName()) {
$def .= ' CONSTRAINT ' . $this->quoteColumnName((string)$foreignKey->getName());
}
$constraintName = $foreignKey->getName() ?: ($tableName . '_' . implode('_', $foreignKey->getColumns()));
Comment thread
dereuromark marked this conversation as resolved.
Outdated
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName);
$columnNames = [];
foreach ($foreignKey->getColumns() as $column) {
$columnNames[] = $this->quoteColumnName($column);
Expand Down
11 changes: 5 additions & 6 deletions src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ protected function getAddForeignKeyInstructions(TableMetadata $table, ForeignKey
$tableName = $table->getName();
$instructions->addPostStep(function ($state) use ($foreignKey, $tableName) {
$this->execute('pragma foreign_keys = ON');
$sql = substr($state['createSQL'], 0, -1) . ',' . $this->getForeignKeySqlDefinition($foreignKey) . '); ';
$sql = substr($state['createSQL'], 0, -1) . ',' . $this->getForeignKeySqlDefinition($foreignKey, $tableName) . '); ';

//Delete indexes from original table and recreate them in temporary table
$schema = $this->getSchemaName($tableName, true)['schema'];
Expand Down Expand Up @@ -1670,14 +1670,13 @@ public function getColumnTypes(): array
* Gets the SQLite Foreign Key Definition for an ForeignKey object.
*
* @param \Migrations\Db\Table\ForeignKey $foreignKey Foreign key
* @param string $tableName Table name for auto-generating constraint name
* @return string
*/
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey): string
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$def = '';
if ($foreignKey->getName()) {
$def .= ' CONSTRAINT ' . $this->quoteColumnName((string)$foreignKey->getName());
}
$constraintName = $foreignKey->getName() ?: ($tableName . '_' . implode('_', $foreignKey->getColumns()));
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName);
$columnNames = [];
foreach ($foreignKey->getColumns() as $column) {
$columnNames[] = $this->quoteColumnName($column);
Expand Down
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/comparisons/Diff/default/the_diff_default_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TheDiffDefaultMysql extends BaseMigration
public function up(): void
{
$this->table('articles')
->dropForeignKey([], 'articles_ibfk_1')
->dropForeignKey([], 'articles_user_id')
->removeIndexByName('UNIQUE_SLUG')
->removeIndexByName('rating_index')
->removeIndexByName('BY_NAME')
Expand Down Expand Up @@ -86,7 +86,7 @@ public function up(): void
])
->addIndex(
$this->index('user_id')
->setName('categories_ibfk_1')
->setName('categories_user_id')
)
->addIndex(
$this->index('name')
Expand All @@ -101,7 +101,7 @@ public function up(): void
->setReferencedColumns('id')
->setOnDelete('RESTRICT')
->setOnUpdate('RESTRICT')
->setName('categories_ibfk_1')
->setName('categories_user_id')
)
->update();

Expand Down Expand Up @@ -234,7 +234,7 @@ public function down(): void
->setReferencedColumns('id')
->setOnDelete('CASCADE')
->setOnUpdate('CASCADE')
->setName('articles_ibfk_1')
->setName('articles_user_id')
)
->update();

Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Diff/simple/the_diff_simple_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function up(): void
->setReferencedColumns('id')
->setOnDelete('RESTRICT')
->setOnUpdate('RESTRICT')
->setName('articles_ibfk_1')
->setName('articles_user_id')
)
->update();
}
Expand Down