Skip to content

Commit

Permalink
[9.x] Added dropForeignIdFor method to match foreignIdFor method (#40950
Browse files Browse the repository at this point in the history
)

* Added dropForeignIdFor method to match foreignIdFor method so adding and dropping can be performed similarly

* Updated to match constrained and non constrained

* Updated formatting so CI passes

* Updated formatting so CI passes

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
bretto36 and taylorotwell authored Feb 12, 2022
1 parent 6c5ecc1 commit aeea9ff
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,38 @@ public function dropConstrainedForeignId($column)
return $this->dropColumn($column);
}

/**
* Indicate that the given foreign key should be dropped.
*
* @param \Illuminate\Database\Eloquent\Model|string $model
* @param string|null $column
* @return \Illuminate\Support\Fluent
*/
public function dropForeignIdFor($model, $column = null)
{
if (is_string($model)) {
$model = new $model;
}

return $this->dropForeign([$column ?: $model->getForeignKey()]);
}

/**
* Indicate that the given foreign key should be dropped.
*
* @param \Illuminate\Database\Eloquent\Model|string $model
* @param string|null $column
* @return \Illuminate\Support\Fluent
*/
public function dropConstrainedForeignIdFor($model, $column = null)
{
if (is_string($model)) {
$model = new $model;
}

return $this->dropConstrainedForeignId($column ?: $model->getForeignKey());
}

/**
* Indicate that the given indexes should be renamed.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,72 @@ public function testGenerateRelationshipColumnWithUuidModel()
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDropRelationshipColumnWithIncrementalModel()
{
$base = new Blueprint('posts', function ($table) {
$table->dropForeignIdFor('Illuminate\Foundation\Auth\User');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `posts` drop foreign key `posts_user_id_foreign`',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDropRelationshipColumnWithUuidModel()
{
require_once __DIR__.'/stubs/EloquentModelUuidStub.php';

$base = new Blueprint('posts', function ($table) {
$table->dropForeignIdFor('EloquentModelUuidStub');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `posts` drop foreign key `posts_eloquent_model_uuid_stub_id_foreign`',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDropConstrainedRelationshipColumnWithIncrementalModel()
{
$base = new Blueprint('posts', function ($table) {
$table->dropConstrainedForeignIdFor('Illuminate\Foundation\Auth\User');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `posts` drop foreign key `posts_user_id_foreign`',
'alter table `posts` drop `user_id`',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDropConstrainedRelationshipColumnWithUuidModel()
{
require_once __DIR__.'/stubs/EloquentModelUuidStub.php';

$base = new Blueprint('posts', function ($table) {
$table->dropConstrainedForeignIdFor('EloquentModelUuidStub');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `posts` drop foreign key `posts_eloquent_model_uuid_stub_id_foreign`',
'alter table `posts` drop `eloquent_model_uuid_stub_id`',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testTinyTextColumn()
{
$base = new Blueprint('posts', function ($table) {
Expand Down

0 comments on commit aeea9ff

Please sign in to comment.