Skip to content
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

[5.6] Add dropMorphs to Blueprint #23431

Closed
wants to merge 6 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ public function dropRememberToken()
$this->dropColumn('remember_token');
}

/**
* Indicate that the polymorphic columns should be dropped.
*
* @param string $name
* @param string|null $indexName
* @return void
*/
public function dropMorphs($name, $indexName = null)
{
$indexName = $indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]);

$this->dropIndex($indexName);

$this->dropColumn("{$name}_type", "{$name}_id");
}

/**
* Rename the table to a given name.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,17 @@ public function testDropTimestampsTz()
$this->assertEquals('alter table `users` drop `created_at`, drop `updated_at`', $statements[0]);
}

public function testDropMorphs()
{
$blueprint = new Blueprint('photos');
$blueprint->dropMorphs('imageable');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertEquals('alter table `photos` drop index `photos_imageable_type_imageable_id_index`', $statements[0]);
$this->assertEquals('alter table `photos` drop `imageable_type`, drop `imageable_id`', $statements[1]);
}

public function testRenameTable()
{
$blueprint = new Blueprint('users');
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ public function testDropTimestampsTz()
$this->assertEquals('alter table "users" drop column "created_at", drop column "updated_at"', $statements[0]);
}

public function testDropMorphs()
{
$blueprint = new Blueprint('photos');
$blueprint->dropMorphs('imageable');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertEquals('drop index "photos_imageable_type_imageable_id_index"', $statements[0]);
$this->assertEquals('alter table "photos" drop column "imageable_type", drop column "imageable_id"', $statements[1]);
}

public function testRenameTable()
{
$blueprint = new Blueprint('users');
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ public function testDropTimestampsTz()
$this->assertEquals('alter table "users" drop column "created_at", "updated_at"', $statements[0]);
}

public function testDropMorphs()
{
$blueprint = new Blueprint('photos');
$blueprint->dropMorphs('imageable');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertEquals('drop index "photos_imageable_type_imageable_id_index" on "photos"', $statements[0]);
$this->assertEquals('alter table "photos" drop column "imageable_type", "imageable_id"', $statements[1]);
}

public function testRenameTable()
{
$blueprint = new Blueprint('users');
Expand Down