Skip to content

[9.x] Added dropForeignIdFor method to match foreignIdFor method #40950

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

Merged
merged 5 commits into from
Feb 12, 2022
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
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