From 692f792dc09885ec6191c4aa92ec3859711a3c32 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 5 Mar 2018 22:52:29 -0500 Subject: [PATCH 1/6] Add dropMorphs to Blueprint --- src/Illuminate/Database/Schema/Blueprint.php | 12 ++++++++++++ tests/Database/DatabaseMySqlSchemaGrammarTest.php | 10 ++++++++++ tests/Database/DatabasePostgresSchemaGrammarTest.php | 10 ++++++++++ .../Database/DatabaseSqlServerSchemaGrammarTest.php | 10 ++++++++++ 4 files changed, 42 insertions(+) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 92e07106a0d7..90df23c52357 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -407,6 +407,18 @@ public function dropRememberToken() $this->dropColumn('remember_token'); } + /** + * Indicate that the polymorphic columns should be dropped. + * + * @param string $name + * + * @return void + */ + public function dropMorphs($name) + { + $this->dropColumn("{$name}_type", "{$name}_id"); + } + /** * Rename the table to a given name. * diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 5cc6389c72a4..0e0c897976e9 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -258,6 +258,16 @@ 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(1, $statements); + $this->assertEquals('alter table `photos` drop `imageable_type`, drop `imageable_id`', $statements[0]); + } + public function testRenameTable() { $blueprint = new Blueprint('users'); diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 352a893a9fd3..635c555184b1 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -173,6 +173,16 @@ 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(1, $statements); + $this->assertEquals('alter table "photos" drop column "imageable_type", drop column "imageable_id"', $statements[0]); + } + public function testRenameTable() { $blueprint = new Blueprint('users'); diff --git a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php index a670928ebd8c..7fa48a276ef9 100755 --- a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php +++ b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php @@ -183,6 +183,16 @@ 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(1, $statements); + $this->assertEquals('alter table "photos" drop column "imageable_type", "imageable_id"', $statements[0]); + } + public function testRenameTable() { $blueprint = new Blueprint('users'); From cfdf6815054beb8cf3f4246dcfad7820a1ebc83c Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 6 Mar 2018 23:33:17 -0500 Subject: [PATCH 2/6] Added drop index to dropMorphs --- src/Illuminate/Database/Schema/Blueprint.php | 6 +++++- tests/Database/DatabaseMySqlSchemaGrammarTest.php | 3 ++- tests/Database/DatabasePostgresSchemaGrammarTest.php | 3 ++- tests/Database/DatabaseSqlServerSchemaGrammarTest.php | 3 ++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 90df23c52357..d249a7beab99 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -414,9 +414,13 @@ public function dropRememberToken() * * @return void */ - public function dropMorphs($name) + public function dropMorphs($name, $indexName = null) { $this->dropColumn("{$name}_type", "{$name}_id"); + + $indexName = $indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]); + + $this->dropIndex($indexName); } /** diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 0e0c897976e9..98e9d08b0347 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -264,8 +264,9 @@ public function testDropMorphs() $blueprint->dropMorphs('imageable'); $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); - $this->assertCount(1, $statements); + $this->assertCount(2, $statements); $this->assertEquals('alter table `photos` drop `imageable_type`, drop `imageable_id`', $statements[0]); + $this->assertEquals('alter table `photos` drop index `photos_imageable_type_imageable_id_index`', $statements[1]); } public function testRenameTable() diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 635c555184b1..d36f45a5b50a 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -179,8 +179,9 @@ public function testDropMorphs() $blueprint->dropMorphs('imageable'); $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); - $this->assertCount(1, $statements); + $this->assertCount(2, $statements); $this->assertEquals('alter table "photos" drop column "imageable_type", drop column "imageable_id"', $statements[0]); + $this->assertEquals('drop index "photos_imageable_type_imageable_id_index"', $statements[1]); } public function testRenameTable() diff --git a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php index 7fa48a276ef9..1192ceef2998 100755 --- a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php +++ b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php @@ -189,8 +189,9 @@ public function testDropMorphs() $blueprint->dropMorphs('imageable'); $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); - $this->assertCount(1, $statements); + $this->assertCount(2, $statements); $this->assertEquals('alter table "photos" drop column "imageable_type", "imageable_id"', $statements[0]); + $this->assertEquals('drop index "photos_imageable_type_imageable_id_index" on "photos"', $statements[1]); } public function testRenameTable() From ca74f37573b8c223c010414fe1910d4596544db2 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 7 Mar 2018 11:12:32 -0500 Subject: [PATCH 3/6] Drop index before columns --- src/Illuminate/Database/Schema/Blueprint.php | 4 ++-- tests/Database/DatabaseMySqlSchemaGrammarTest.php | 4 ++-- tests/Database/DatabasePostgresSchemaGrammarTest.php | 4 ++-- tests/Database/DatabaseSqlServerSchemaGrammarTest.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index d249a7beab99..6d70d82234ed 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -416,11 +416,11 @@ public function dropRememberToken() */ public function dropMorphs($name, $indexName = null) { - $this->dropColumn("{$name}_type", "{$name}_id"); - $indexName = $indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]); $this->dropIndex($indexName); + + $this->dropColumn("{$name}_type", "{$name}_id"); } /** diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 98e9d08b0347..2c30ae51811d 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -265,8 +265,8 @@ public function testDropMorphs() $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); $this->assertCount(2, $statements); - $this->assertEquals('alter table `photos` drop `imageable_type`, drop `imageable_id`', $statements[0]); - $this->assertEquals('alter table `photos` drop index `photos_imageable_type_imageable_id_index`', $statements[1]); + $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() diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index d36f45a5b50a..568b5d52b235 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -180,8 +180,8 @@ public function testDropMorphs() $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); $this->assertCount(2, $statements); - $this->assertEquals('alter table "photos" drop column "imageable_type", drop column "imageable_id"', $statements[0]); - $this->assertEquals('drop index "photos_imageable_type_imageable_id_index"', $statements[1]); + $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() diff --git a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php index 1192ceef2998..35e8729074e7 100755 --- a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php +++ b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php @@ -190,8 +190,8 @@ public function testDropMorphs() $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); $this->assertCount(2, $statements); - $this->assertEquals('alter table "photos" drop column "imageable_type", "imageable_id"', $statements[0]); - $this->assertEquals('drop index "photos_imageable_type_imageable_id_index" on "photos"', $statements[1]); + $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() From 037418664691c777c0e550161da1e72631e1e9d5 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 7 Mar 2018 17:27:02 -0500 Subject: [PATCH 4/6] Corrected Doc block --- src/Illuminate/Database/Schema/Blueprint.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 6d70d82234ed..c5ae5c563775 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -410,7 +410,8 @@ public function dropRememberToken() /** * Indicate that the polymorphic columns should be dropped. * - * @param string $name + * @param string $name + * @param string|null $indexName * * @return void */ From 34ab179d366753650e5d5ebddd78f3aaa33f16b7 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 7 Mar 2018 17:32:37 -0500 Subject: [PATCH 5/6] MIssed a space --- src/Illuminate/Database/Schema/Blueprint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index c5ae5c563775..ee5d453a4f56 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -410,7 +410,7 @@ public function dropRememberToken() /** * Indicate that the polymorphic columns should be dropped. * - * @param string $name + * @param string $name * @param string|null $indexName * * @return void From dba7288f20d8f82aa33c0107c546d49430b6fc1b Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 8 Mar 2018 09:34:55 -0500 Subject: [PATCH 6/6] Removed extra line in Doc block --- src/Illuminate/Database/Schema/Blueprint.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index ee5d453a4f56..45fb7d4d2b8e 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -412,7 +412,6 @@ public function dropRememberToken() * * @param string $name * @param string|null $indexName - * * @return void */ public function dropMorphs($name, $indexName = null)