Skip to content

Commit aeea9ff

Browse files
[9.x] Added dropForeignIdFor method to match foreignIdFor method (#40950)
* 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 <taylor@laravel.com>
1 parent 6c5ecc1 commit aeea9ff

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,38 @@ public function dropConstrainedForeignId($column)
413413
return $this->dropColumn($column);
414414
}
415415

416+
/**
417+
* Indicate that the given foreign key should be dropped.
418+
*
419+
* @param \Illuminate\Database\Eloquent\Model|string $model
420+
* @param string|null $column
421+
* @return \Illuminate\Support\Fluent
422+
*/
423+
public function dropForeignIdFor($model, $column = null)
424+
{
425+
if (is_string($model)) {
426+
$model = new $model;
427+
}
428+
429+
return $this->dropForeign([$column ?: $model->getForeignKey()]);
430+
}
431+
432+
/**
433+
* Indicate that the given foreign key should be dropped.
434+
*
435+
* @param \Illuminate\Database\Eloquent\Model|string $model
436+
* @param string|null $column
437+
* @return \Illuminate\Support\Fluent
438+
*/
439+
public function dropConstrainedForeignIdFor($model, $column = null)
440+
{
441+
if (is_string($model)) {
442+
$model = new $model;
443+
}
444+
445+
return $this->dropConstrainedForeignId($column ?: $model->getForeignKey());
446+
}
447+
416448
/**
417449
* Indicate that the given indexes should be renamed.
418450
*

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,72 @@ public function testGenerateRelationshipColumnWithUuidModel()
292292
], $blueprint->toSql($connection, new MySqlGrammar));
293293
}
294294

295+
public function testDropRelationshipColumnWithIncrementalModel()
296+
{
297+
$base = new Blueprint('posts', function ($table) {
298+
$table->dropForeignIdFor('Illuminate\Foundation\Auth\User');
299+
});
300+
301+
$connection = m::mock(Connection::class);
302+
303+
$blueprint = clone $base;
304+
305+
$this->assertEquals([
306+
'alter table `posts` drop foreign key `posts_user_id_foreign`',
307+
], $blueprint->toSql($connection, new MySqlGrammar));
308+
}
309+
310+
public function testDropRelationshipColumnWithUuidModel()
311+
{
312+
require_once __DIR__.'/stubs/EloquentModelUuidStub.php';
313+
314+
$base = new Blueprint('posts', function ($table) {
315+
$table->dropForeignIdFor('EloquentModelUuidStub');
316+
});
317+
318+
$connection = m::mock(Connection::class);
319+
320+
$blueprint = clone $base;
321+
322+
$this->assertEquals([
323+
'alter table `posts` drop foreign key `posts_eloquent_model_uuid_stub_id_foreign`',
324+
], $blueprint->toSql($connection, new MySqlGrammar));
325+
}
326+
327+
public function testDropConstrainedRelationshipColumnWithIncrementalModel()
328+
{
329+
$base = new Blueprint('posts', function ($table) {
330+
$table->dropConstrainedForeignIdFor('Illuminate\Foundation\Auth\User');
331+
});
332+
333+
$connection = m::mock(Connection::class);
334+
335+
$blueprint = clone $base;
336+
337+
$this->assertEquals([
338+
'alter table `posts` drop foreign key `posts_user_id_foreign`',
339+
'alter table `posts` drop `user_id`',
340+
], $blueprint->toSql($connection, new MySqlGrammar));
341+
}
342+
343+
public function testDropConstrainedRelationshipColumnWithUuidModel()
344+
{
345+
require_once __DIR__.'/stubs/EloquentModelUuidStub.php';
346+
347+
$base = new Blueprint('posts', function ($table) {
348+
$table->dropConstrainedForeignIdFor('EloquentModelUuidStub');
349+
});
350+
351+
$connection = m::mock(Connection::class);
352+
353+
$blueprint = clone $base;
354+
355+
$this->assertEquals([
356+
'alter table `posts` drop foreign key `posts_eloquent_model_uuid_stub_id_foreign`',
357+
'alter table `posts` drop `eloquent_model_uuid_stub_id`',
358+
], $blueprint->toSql($connection, new MySqlGrammar));
359+
}
360+
295361
public function testTinyTextColumn()
296362
{
297363
$base = new Blueprint('posts', function ($table) {

0 commit comments

Comments
 (0)