From eee602bf75d58677ec56992b78249d4742df6a6c Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 12 Dec 2023 11:59:56 +1030 Subject: [PATCH 01/37] add json and medium/long text column type --- src/Schema/Grammar.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 1499d9cd..3a6d0dcd 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -403,6 +403,39 @@ protected function typeText(Fluent $column) return "string(max)"; } + /** + * Create the column definition for a medium text type. + * + * @param \Illuminate\Support\Fluent $column + * @return string + */ + protected function typeMediumText(Fluent $column) + { + return $this->typeText($column); + } + + /** + * Create the column definition for a long text type. + * + * @param \Illuminate\Support\Fluent $column + * @return string + */ + protected function typeLongText(Fluent $column) + { + return $this->typeText($column); + } + + /** + * Create the column definition for a json type. + * + * @param \Illuminate\Support\Fluent $column + * @return string + */ + protected function typeJson(Fluent $column) + { + return 'json'; + } + /** * Create the column definition for a binary type. * From 2c57408550db62fdb6b1a91fe57a8daeb304b7a6 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 12 Dec 2023 12:50:44 +1030 Subject: [PATCH 02/37] Update src/Schema/Grammar.php Co-authored-by: Takayasu Oyama --- src/Schema/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 3a6d0dcd..4d8eb7d9 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -406,7 +406,7 @@ protected function typeText(Fluent $column) /** * Create the column definition for a medium text type. * - * @param \Illuminate\Support\Fluent $column + * @param Fluent $column * @return string */ protected function typeMediumText(Fluent $column) From b7e5282bed0436e4b302582b46998666b412721d Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 12 Dec 2023 12:50:49 +1030 Subject: [PATCH 03/37] Update src/Schema/Grammar.php Co-authored-by: Takayasu Oyama --- src/Schema/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 4d8eb7d9..97c4b901 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -417,7 +417,7 @@ protected function typeMediumText(Fluent $column) /** * Create the column definition for a long text type. * - * @param \Illuminate\Support\Fluent $column + * @param Fluent $column * @return string */ protected function typeLongText(Fluent $column) From f57a184c6fed359a7548e42d573a6df04892d258 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 12 Dec 2023 12:50:53 +1030 Subject: [PATCH 04/37] Update src/Schema/Grammar.php Co-authored-by: Takayasu Oyama --- src/Schema/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 97c4b901..3e56cffc 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -428,7 +428,7 @@ protected function typeLongText(Fluent $column) /** * Create the column definition for a json type. * - * @param \Illuminate\Support\Fluent $column + * @param Fluent $column * @return string */ protected function typeJson(Fluent $column) From 468cb19b26955c591e8b40ec529ddab6f7ac2ad3 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 12 Dec 2023 14:25:45 +1030 Subject: [PATCH 05/37] add unit tests --- tests/Schema/BlueprintTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index d9313d1f..7db213ca 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -39,8 +39,11 @@ public function testCreateTable(): void $table->decimal('decimal'); $table->string('name'); $table->text('text'); + $table->mediumText('medium_text'); + $table->longText('long_text'); $table->dateTime('started_at'); $table->binary('blob'); + $table->json('json'); $table->timestamps(); $table->primary('id'); @@ -56,8 +59,11 @@ public function testCreateTable(): void '`decimal` numeric not null', '`name` string(255) not null', '`text` string(max) not null', + '`medium_text` string(max) not null', + '`long_text` string(max) not null', '`started_at` timestamp not null', '`blob` bytes(255) not null', + '`json` json not null', '`created_at` timestamp, `updated_at` timestamp', ]) . ') primary key (`id`)', $queries[0] @@ -413,7 +419,10 @@ public function test_default_values(): void $table->boolean('bool')->default(true); $table->string('string')->default('a'); $table->text('string_max')->default('a'); + $table->mediumText('medium_text')->default('a'); + $table->longText('long_text')->default('a'); $table->float('raw')->default(DB::raw('1.1')); + $table->json('json')->default(DB::raw('json "[1,2,3]"')); $table->date('date_as_string')->default('2022-01-01'); $table->date('date_as_carbon')->default(new Carbon('2022-01-01')); $table->dateTime('time_as_string')->default('2022-01-01'); @@ -445,7 +454,10 @@ public function test_default_values(): void '`bool` bool not null default (true)', '`string` string(255) not null default ("a")', '`string_max` string(max) not null default ("a")', + '`medium_text` string(max) not null default ("a")', + '`long_text` string(max) not null default ("a")', '`raw` float64 not null default (1.1)', + '`json` json not null default (json "[1,2,3]")', '`date_as_string` date not null default (DATE "2022-01-01")', '`date_as_carbon` date not null default (DATE "2022-01-01")', '`time_as_string` timestamp not null default (TIMESTAMP "2022-01-01T00:00:00.000000+00:00")', @@ -476,6 +488,7 @@ public function test_default_values(): void self::assertSame(0.1, $result['float']); self::assertSame(true, $result['bool']); self::assertSame('a', $result['string']); + self::assertSame('[1,2,3]', $result['json']); self::assertSame(1.1, $result['raw']); self::assertSame('2022-01-01T00:00:00.000000+00:00', $result['date_as_string']->get()->format($grammar->getDateFormat())); self::assertSame('2022-01-01T00:00:00.000000+00:00', $result['date_as_carbon']->get()->format($grammar->getDateFormat())); From e56b6b555745eb3f1a4d3272e118b714d4533c12 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Tue, 12 Dec 2023 13:18:34 +0900 Subject: [PATCH 06/37] Update tests/Schema/BlueprintTest.php --- tests/Schema/BlueprintTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index 7db213ca..e23c9984 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -488,6 +488,9 @@ public function test_default_values(): void self::assertSame(0.1, $result['float']); self::assertSame(true, $result['bool']); self::assertSame('a', $result['string']); + self::assertSame('a', $result['string_max']); + self::assertSame('a', $result['medium_text']); + self::assertSame('a', $result['long_text']); self::assertSame('[1,2,3]', $result['json']); self::assertSame(1.1, $result['raw']); self::assertSame('2022-01-01T00:00:00.000000+00:00', $result['date_as_string']->get()->format($grammar->getDateFormat())); From 2797836f95662ca7c795bc32499eaa9c008bcbbc Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 12 Dec 2023 21:37:43 +1030 Subject: [PATCH 07/37] add typeChar --- src/Schema/Grammar.php | 11 +++++++++++ tests/Schema/BlueprintTest.php | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 3e56cffc..9d5a4d7a 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -392,6 +392,17 @@ protected function typeString(Fluent $column) return "string({$column->length})"; } + /** + * Create the column definition for a char type. + * + * @param Fluent $column + * @return string + */ + protected function typeChar(Fluent $column) + { + return $this->typeString($column); + } + /** * Create the column definition for a text type. * diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index e23c9984..42e81afb 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -38,6 +38,7 @@ public function testCreateTable(): void $table->float('float'); $table->decimal('decimal'); $table->string('name'); + $table->char('char'); $table->text('text'); $table->mediumText('medium_text'); $table->longText('long_text'); @@ -58,6 +59,7 @@ public function testCreateTable(): void '`float` float64 not null', '`decimal` numeric not null', '`name` string(255) not null', + '`char` string(255) not null', '`text` string(max) not null', '`medium_text` string(max) not null', '`long_text` string(max) not null', @@ -419,6 +421,7 @@ public function test_default_values(): void $table->boolean('bool')->default(true); $table->string('string')->default('a'); $table->text('string_max')->default('a'); + $table->char('char')->default('a'); $table->mediumText('medium_text')->default('a'); $table->longText('long_text')->default('a'); $table->float('raw')->default(DB::raw('1.1')); @@ -454,6 +457,7 @@ public function test_default_values(): void '`bool` bool not null default (true)', '`string` string(255) not null default ("a")', '`string_max` string(max) not null default ("a")', + '`char` string(255) not null default ("a")', '`medium_text` string(max) not null default ("a")', '`long_text` string(max) not null default ("a")', '`raw` float64 not null default (1.1)', From 5e0c321d1401e7331d4fdeef51be930abb2a6fce Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 12 Dec 2023 22:20:46 +1030 Subject: [PATCH 08/37] add/alter columns should be separate statements --- src/Schema/Grammar.php | 14 ++++++++------ tests/Schema/BlueprintTest.php | 14 ++++++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 3e56cffc..e774015c 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -134,9 +134,10 @@ public function compileCreate(Blueprint $blueprint, Fluent $command) */ public function compileAdd(Blueprint $blueprint, Fluent $command) { - $columns = $this->prefixArray('add column', $this->getColumns($blueprint)); - - return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); + return $this->prefixArray( + 'alter table '.$this->wrapTable($blueprint).' add column', + $this->getColumns($blueprint) + ); } /** @@ -149,9 +150,10 @@ public function compileAdd(Blueprint $blueprint, Fluent $command) */ public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) { - $columns = $this->prefixArray('alter column', $this->getChangedColumns($blueprint)); - - return ['alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns)]; + return $this->prefixArray( + 'alter table '.$this->wrapTable($blueprint).' alter column', + $this->getChangedColumns($blueprint) + ); } /** diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index e23c9984..aafa53eb 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -108,12 +108,15 @@ public function testAddColumn(): void $blueprint = new Blueprint('Test3', function (Blueprint $table) { $table->string('description', 255); + $table->integer('value'); }); $queries = $blueprint->toSql($conn, new Grammar()); - $this->assertEquals( + $this->assertEquals([ 'alter table `Test3` add column `description` string(255) not null', - $queries[0] + 'alter table `Test3` add column `value` int64 not null' + ], + $queries ); } @@ -123,12 +126,15 @@ public function testChangeColumn(): void $blueprint = new Blueprint('Test3', function (Blueprint $table) { $table->string('description', 512)->change(); + $table->float('value')->change(); }); $queries = $blueprint->toSql($conn, new Grammar()); - $this->assertEquals( + $this->assertEquals([ 'alter table `Test3` alter column `description` string(512) not null', - $queries[0] + 'alter table `Test3` alter column `value` float64 not null', + ], + $queries ); } From 3621299f87c41979ebe2ca2035c2abcee2dae158 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 12 Dec 2023 22:44:34 +1030 Subject: [PATCH 09/37] type hint compileAdd properly --- src/Schema/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index e774015c..293ffe46 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -130,7 +130,7 @@ public function compileCreate(Blueprint $blueprint, Fluent $command) * * @param Blueprint $blueprint * @param Fluent $command - * @return string + * @return string[] */ public function compileAdd(Blueprint $blueprint, Fluent $command) { From 374266405348d722424a0cbab2d6c3733910f1e7 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 01:52:15 +1030 Subject: [PATCH 10/37] dropAllTables - just delete and recreate the database --- src/Schema/Builder.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 1e922692..0cb983be 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -20,6 +20,7 @@ use Closure; use Colopl\Spanner\Query\Processor; use Illuminate\Database\Schema\Builder as BaseBuilder; +use Illuminate\Support\Facades\DB; /** * @property Grammar $grammar @@ -99,4 +100,12 @@ protected function createBlueprint($table, Closure $callback = null) ? ($this->resolver)($table, $callback) : new Blueprint($table, $callback); } + + public function dropAllTables() + { + /** @var \Colopl\Spanner\Connection */ + $connection = DB::connection(); + $connection->dropDatabase(); + $connection->createDatabase(); + } } From 8eccb282a3199b1f13064bbc631a1b1590347e55 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 02:01:51 +1030 Subject: [PATCH 11/37] phpdoc --- src/Schema/Builder.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 0cb983be..cd562b37 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -101,6 +101,11 @@ protected function createBlueprint($table, Closure $callback = null) : new Blueprint($table, $callback); } + /** + * Drop all tables from the database. + * + * @return void + */ public function dropAllTables() { /** @var \Colopl\Spanner\Connection */ From 513a5b194b7c2a1e9f2bd552c29b833b3b067985 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 15:18:05 +1030 Subject: [PATCH 12/37] only drop if db exists --- src/Schema/Builder.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index cd562b37..498b34d7 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -110,7 +110,10 @@ public function dropAllTables() { /** @var \Colopl\Spanner\Connection */ $connection = DB::connection(); - $connection->dropDatabase(); + + if($connection->databaseExists()) + $connection->dropDatabase(); + $connection->createDatabase(); } } From 0cbb9f5de973cf643ed3341f431bb49ce871bb91 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 16:11:38 +1030 Subject: [PATCH 13/37] add dropForeign compilation --- src/Schema/Grammar.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 3e56cffc..bb29fbcc 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -341,6 +341,20 @@ public function compileDropUnique(Blueprint $blueprint, Fluent $command) return $this->compileDropIndex($blueprint, $command); } + /** + * Compile a drop foreign key command. + * + * @param Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @return string + */ + public function compileDropForeign(Blueprint $blueprint, Fluent $command) + { + $index = $this->wrap($command->index); + + return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; + } + /** * Get the primary key syntax for a table creation statement. * From c3a5ae253b54412f590ca163313a8a974c50ff89 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 16:32:47 +1030 Subject: [PATCH 14/37] fix dropColumn as well --- src/Schema/Grammar.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 293ffe46..ef7efe26 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -161,13 +161,14 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection * * @param Blueprint $blueprint * @param Fluent $command - * @return string + * @return string[] */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) { - $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); - - return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns); + return $this->prefixArray( + 'alter table '.$this->wrapTable($blueprint).' drop column', + $this->wrapArray($command->columns) + ); } /** From 8d9328adb8c2b3e4455488e46a5de1c71080a3dd Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 13 Dec 2023 16:34:25 +1030 Subject: [PATCH 15/37] fix type hint --- src/Schema/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index bb29fbcc..be8588a1 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -345,7 +345,7 @@ public function compileDropUnique(Blueprint $blueprint, Fluent $command) * Compile a drop foreign key command. * * @param Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Support\Fluent $command * @return string */ public function compileDropForeign(Blueprint $blueprint, Fluent $command) From d83137e30eb77dbe0afabd35840f0f6f9720e06c Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 11:23:16 +1030 Subject: [PATCH 16/37] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c031d626..bb4b45c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # v6.2.0 (Not Released Yet) Added -- `json` `mediumText` `longText` support for `Schema\Builder` (#155) +- `json` `mediumText` `longText` `char` support for `Schema\Builder` (#155) Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) From d42b1afca8978dd3717832c917789e7ad7e23423 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Thu, 14 Dec 2023 10:08:37 +0900 Subject: [PATCH 17/37] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb4b45c2..a17adb12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # v6.2.0 (Not Released Yet) Added -- `json` `mediumText` `longText` `char` support for `Schema\Builder` (#155) +- `json` `mediumText` `longText` `char` support for `Schema\Builder` (#155) (#158) Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) From 35f2d26440d289ec2c3d65d12c221a457ee938af Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 11:48:49 +1030 Subject: [PATCH 18/37] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c031d626..9adc697f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Added Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) +Fixed +- `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements + # v6.1.1 (2023-12-11) Fixed From b79f8a0f55dae444fcdb41e8e84cd87d9b47825c Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Thu, 14 Dec 2023 10:20:44 +0900 Subject: [PATCH 19/37] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9adc697f..de1f9f7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) Fixed -- `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements +- `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159) # v6.1.1 (2023-12-11) From c042d03e2aa36c93899d2a54f1bedc4694d47f52 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 12:00:11 +1030 Subject: [PATCH 20/37] added test and changelog --- CHANGELOG.md | 1 + tests/Schema/BlueprintTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c031d626..22a81813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Added - `json` `mediumText` `longText` support for `Schema\Builder` (#155) +- `Schema\Grammar::compileDropForeign` to allow dropping foreign key constraints (#163) Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index e23c9984..8c8ed6bd 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -185,6 +185,23 @@ public function testDropIndex(): void ); } + public function testDropForeign(): void + { + $conn = $this->getDefaultConnection(); + + $blueprint = new Blueprint('Test3', function (Blueprint $table) { + $table->dropForeign('fk_test3'); + }); + + $queries = $blueprint->toSql($conn, new Grammar()); + $this->assertEquals( + [ + 'alter table `Test3` drop constraint `fk_test3`', + ], + $queries + ); + } + public function test_no_primaryKey(): void { $this->expectException(LogicException::class); From ea9dc3d4847d77e1a668482b64b094025dc13937 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 14:06:55 +1030 Subject: [PATCH 21/37] instead of dropping db, move towards proper drop sequence --- src/Schema/Builder.php | 80 +++++++++++++++++++++++++++++++++--- src/Schema/Grammar.php | 12 +++++- tests/Schema/BuilderTest.php | 24 ++++++++++- 3 files changed, 109 insertions(+), 7 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 498b34d7..b9635038 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -21,6 +21,7 @@ use Colopl\Spanner\Query\Processor; use Illuminate\Database\Schema\Builder as BaseBuilder; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Fluent; /** * @property Grammar $grammar @@ -47,6 +48,18 @@ public function getAllTables() ); } + /** + * @inheritDoc + * + * @return list + */ + public function getTables() + { + return $this->connection->select( + $this->grammar->compileTables() + ); + } + /** * @param string $table * @return string[] @@ -65,6 +78,24 @@ public function getIndexListing($table) return $processor->processIndexListing($results); } + /** + * @param string $table + * @return string[] + */ + public function getForeignListing($table) + { + $table = $this->connection->getTablePrefix().$table; + + $results = $this->connection->select( + $this->grammar->compileForeignListing(), [$table] + ); + + /** @var Processor $processor */ + $processor = $this->connection->getPostProcessor(); + + return $processor->processIndexListing($results); + } + /** * @param string $table * @param string $name @@ -108,12 +139,51 @@ protected function createBlueprint($table, Closure $callback = null) */ public function dropAllTables() { - /** @var \Colopl\Spanner\Connection */ - $connection = DB::connection(); + $tables = self::getTables(); + $sort = []; - if($connection->databaseExists()) - $connection->dropDatabase(); + foreach ($tables as $table) { + $tableName = $table['name']; + $parentTableName = $table['parent']; + $indexes = self::getIndexListing($tableName); + $foreigns = self::getForeignListing($tableName); - $connection->createDatabase(); + $sort[$tableName] = [ + 'name' => $tableName, + 'parent' => $parentTableName, + 'parents' => 0, + 'indexes' => $indexes, + 'foreigns' => $foreigns, + ]; + } + + foreach ($sort as $tableName => $tableData) { + if(!$tableData['parent']) continue; + + $current = $tableData; + while($current['parent']) { + $tableData['parents'] += 1; + $current = $sort[$current['parent']]; + } + $sort[$tableName] = $tableData; + } + + usort($sort, fn($a, $b) => $b['parents'] <=> $a['parents']); + + $queries = []; + foreach ($sort as $tableData) { + $tableName = $tableData['name']; + $blueprint = new Blueprint($tableName); + foreach ($tableData['foreigns'] as $foreign) { + // $queries[] = $this->grammar->compileDrop + } + foreach ($tableData['indexes'] as $index) { + $queries [] = $this->grammar->compileDropIndex($blueprint, new Fluent(['index' => $index])); + } + $queries[] = $this->grammar->compileDrop($blueprint, new Fluent()); + + } + + dd($sort); } } diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index fca379e3..34ce5f6e 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -58,7 +58,7 @@ public function compileTableExists() */ public function compileTables() { - return 'select `table_name` as name from information_schema.tables where table_schema = \'\' and table_type = \'BASE TABLE\''; + return 'select `table_name` as name, `table_type` as type, `parent_table_name` as parent from information_schema.tables where table_schema = \'\' and table_type = \'BASE TABLE\''; } /** @@ -93,6 +93,16 @@ public function compileIndexListing() return 'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = ?'; } + /** + * Compile the query to determine the list of foreign keys. + * + * @return string + */ + public function compileForeignListing() + { + return 'select constraint_name as `index_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = ?'; + } + /** * Compile the query to determine the columns. * diff --git a/tests/Schema/BuilderTest.php b/tests/Schema/BuilderTest.php index 73b91d3f..13f3a6c9 100644 --- a/tests/Schema/BuilderTest.php +++ b/tests/Schema/BuilderTest.php @@ -243,11 +243,33 @@ public function test_getAllTables(): void /** @var array{ name: string, type: string } $row */ $row = Arr::first( - $sb->getAllTables(), + $sb->getTables(), static fn (array $row): bool => $row['name'] === $table, ); $this->assertSame($table, $row['name']); $this->assertSame('BASE TABLE', $row['type']); } + + public function test_dropAllTables(): void { + $conn = $this->getDefaultConnection(); + $sb = $conn->getSchemaBuilder(); + $table1 = $this->generateTableName(class_basename(__CLASS__)); + $sb->create($table1, function (Blueprint $table) { + $table->uuid('id')->primary(); + $table->uuid('something'); + $table->index('something'); + }); + + $table2 = $this->generateTableName(class_basename(__CLASS__)); + $sb->create($table2, function (Blueprint $table) use ($table1) { + $table->uuid('id'); + $table->uuid('other_id'); + $table->primary('id'); + $table->index('other_id'); + $table->foreign('other_id')->references('id')->on($table1); + }); + + $sb->dropAllTables(); + } } From 8a6e4eb958898ef9f1ce9ca1acc51dedbcc1abbe Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 14:49:47 +1030 Subject: [PATCH 22/37] proper drop sequence with testss --- src/Schema/Builder.php | 48 ++++++++++++++++++++++-------------- tests/Schema/BuilderTest.php | 23 +++++++++++++++-- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index b9635038..eedbbe1d 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -19,6 +19,7 @@ use Closure; use Colopl\Spanner\Query\Processor; +use Colopl\Spanner\Connection; use Illuminate\Database\Schema\Builder as BaseBuilder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Fluent; @@ -139,51 +140,62 @@ protected function createBlueprint($table, Closure $callback = null) */ public function dropAllTables() { + /** @var Connection */ + $connection = $this->connection; $tables = self::getTables(); - $sort = []; + $sortedTables = []; + // get all tables foreach ($tables as $table) { $tableName = $table['name']; $parentTableName = $table['parent']; - $indexes = self::getIndexListing($tableName); - $foreigns = self::getForeignListing($tableName); - $sort[$tableName] = [ + $sortedTables[$tableName] = [ 'name' => $tableName, 'parent' => $parentTableName, - 'parents' => 0, - 'indexes' => $indexes, - 'foreigns' => $foreigns, + 'parents' => 0 ]; } - foreach ($sort as $tableName => $tableData) { + // loop through all tables and count how many parents they have + foreach ($sortedTables as $tableName => $tableData) { if(!$tableData['parent']) continue; $current = $tableData; while($current['parent']) { $tableData['parents'] += 1; - $current = $sort[$current['parent']]; + $current = $sortedTables[$current['parent']]; } - $sort[$tableName] = $tableData; + $sortedTables[$tableName] = $tableData; } - usort($sort, fn($a, $b) => $b['parents'] <=> $a['parents']); + // sort tables desc based on parent count + usort($sortedTables, fn($a, $b) => $b['parents'] <=> $a['parents']); + // drop foreign keys first (otherwise index queries will include them) $queries = []; - foreach ($sort as $tableData) { + foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; $blueprint = new Blueprint($tableName); - foreach ($tableData['foreigns'] as $foreign) { - // $queries[] = $this->grammar->compileDrop + $foreigns = self::getForeignListing($tableName); + foreach ($foreigns as $foreign) { + $queries[] = $this->grammar->compileDropForeign($blueprint, new Fluent(['index' => $foreign])); } - foreach ($tableData['indexes'] as $index) { + } + $connection->runDdlBatch($queries); + + // drop indexes and tables + $queries = []; + foreach ($sortedTables as $tableData) { + $tableName = $tableData['name']; + $blueprint = new Blueprint($tableName); + $indexes = self::getIndexListing($tableName); + foreach ($indexes as $index) { + if($index == 'PRIMARY_KEY') continue; $queries [] = $this->grammar->compileDropIndex($blueprint, new Fluent(['index' => $index])); } $queries[] = $this->grammar->compileDrop($blueprint, new Fluent()); - } - - dd($sort); + $connection->runDdlBatch($queries); } } diff --git a/tests/Schema/BuilderTest.php b/tests/Schema/BuilderTest.php index 13f3a6c9..7b011122 100644 --- a/tests/Schema/BuilderTest.php +++ b/tests/Schema/BuilderTest.php @@ -263,13 +263,32 @@ public function test_dropAllTables(): void { $table2 = $this->generateTableName(class_basename(__CLASS__)); $sb->create($table2, function (Blueprint $table) use ($table1) { - $table->uuid('id'); + $table->uuid('table2_id')->primary(); $table->uuid('other_id'); - $table->primary('id'); $table->index('other_id'); $table->foreign('other_id')->references('id')->on($table1); }); + $table3 = $this->generateTableName(class_basename(__CLASS__)); + $sb->create($table3, function (Blueprint $table) use ($table2) { + $table->uuid('table2_id'); + $table->uuid('table3_id'); + $table->primary(['table2_id', 'table3_id']); + $table->interleaveInParent($table2); + }); + + $table4 = $this->generateTableName(class_basename(__CLASS__)); + $sb->create($table4, function (Blueprint $table) use ($table3) { + $table->uuid('table2_id'); + $table->uuid('table3_id'); + $table->uuid('table4_id'); + $table->primary(['table2_id', 'table3_id', 'table4_id']); + $table->interleaveInParent($table3); + }); + $sb->dropAllTables(); + + $tables = $sb->getTables(); + $this->assertEmpty($tables); } } From bf2bc5d24379e120021eab6fab99477c238431c5 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 14:51:52 +1030 Subject: [PATCH 23/37] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 082c6754..2127052e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Added - `json` `mediumText` `longText` support for `Schema\Builder` (#155) - `Schema\Grammar::compileDropForeign` to allow dropping foreign key constraints (#163) +- `Schema\Builder::dropAllTables` works properly, dropping foreign keys, indexes, then tables in order of interleaving (#161) Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) From 6bac86d2f3a46169bc03034a31c3f140890973ae Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 14:59:35 +1030 Subject: [PATCH 24/37] oh man phpstan --- src/Schema/Builder.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index eedbbe1d..087e9fad 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -179,7 +179,9 @@ public function dropAllTables() $blueprint = new Blueprint($tableName); $foreigns = self::getForeignListing($tableName); foreach ($foreigns as $foreign) { - $queries[] = $this->grammar->compileDropForeign($blueprint, new Fluent(['index' => $foreign])); + $column = new Fluent(); + $column->index = $foreign; + $queries[] = $this->grammar->compileDropForeign($blueprint, $column); } } $connection->runDdlBatch($queries); @@ -192,7 +194,9 @@ public function dropAllTables() $indexes = self::getIndexListing($tableName); foreach ($indexes as $index) { if($index == 'PRIMARY_KEY') continue; - $queries [] = $this->grammar->compileDropIndex($blueprint, new Fluent(['index' => $index])); + $column = new Fluent(); + $column->index = $index; + $queries [] = $this->grammar->compileDropIndex($blueprint, $column); } $queries[] = $this->grammar->compileDrop($blueprint, new Fluent()); } From bbd6ec1e1c61c38e24672f781121f60ca6bd6230 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 16:01:35 +1030 Subject: [PATCH 25/37] Allow for some tests to be run last --- phpunit.xml | 1 + tests/Schema/{BuilderTest.php => BuilderTestLast.php} | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) rename tests/Schema/{BuilderTest.php => BuilderTestLast.php} (98%) diff --git a/phpunit.xml b/phpunit.xml index 274c1c37..8426cc50 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,6 +27,7 @@ stopOnFailure="true"> ./tests + ./tests diff --git a/tests/Schema/BuilderTest.php b/tests/Schema/BuilderTestLast.php similarity index 98% rename from tests/Schema/BuilderTest.php rename to tests/Schema/BuilderTestLast.php index 7b011122..9d5f6d16 100644 --- a/tests/Schema/BuilderTest.php +++ b/tests/Schema/BuilderTestLast.php @@ -23,7 +23,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; -class BuilderTest extends TestCase +class BuilderTestLast extends TestCase { private const TABLE_NAME_CREATED = 'schema_builder_test_table'; private const TABLE_NAME_RELATION_PARENT = 'users'; @@ -251,7 +251,9 @@ public function test_getAllTables(): void $this->assertSame('BASE TABLE', $row['type']); } - public function test_dropAllTables(): void { + public function test_dropAllTables(): void + { + dd('last'); $conn = $this->getDefaultConnection(); $sb = $conn->getSchemaBuilder(); $table1 = $this->generateTableName(class_basename(__CLASS__)); From 4dab33f8a1dbae9c49962ee70d63a7577b506eaf Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 16:15:06 +1030 Subject: [PATCH 26/37] ugh dd --- tests/Schema/BuilderTestLast.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Schema/BuilderTestLast.php b/tests/Schema/BuilderTestLast.php index 9d5f6d16..ceaebe77 100644 --- a/tests/Schema/BuilderTestLast.php +++ b/tests/Schema/BuilderTestLast.php @@ -253,7 +253,6 @@ public function test_getAllTables(): void public function test_dropAllTables(): void { - dd('last'); $conn = $this->getDefaultConnection(); $sb = $conn->getSchemaBuilder(); $table1 = $this->generateTableName(class_basename(__CLASS__)); From a153e04a92ef48bae8069467d898e7bd389962a9 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 14 Dec 2023 16:20:23 +1030 Subject: [PATCH 27/37] Update src/Schema/Builder.php Co-authored-by: Takayasu Oyama --- src/Schema/Builder.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 087e9fad..d95a4488 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -21,7 +21,6 @@ use Colopl\Spanner\Query\Processor; use Colopl\Spanner\Connection; use Illuminate\Database\Schema\Builder as BaseBuilder; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Fluent; /** From 74fe12cf1395f09ba0c537e39530d1bc3d9bf9d0 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 16:17:37 +1030 Subject: [PATCH 28/37] compileForeignListing > compileForeignKeys --- src/Schema/Builder.php | 2 +- src/Schema/Grammar.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index d95a4488..53ecf146 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -87,7 +87,7 @@ public function getForeignListing($table) $table = $this->connection->getTablePrefix().$table; $results = $this->connection->select( - $this->grammar->compileForeignListing(), [$table] + $this->grammar->compileForeignKeys(), [$table] ); /** @var Processor $processor */ diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 924917c7..fda9adf5 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -98,7 +98,7 @@ public function compileIndexListing() * * @return string */ - public function compileForeignListing() + public function compileForeignKeys() { return 'select constraint_name as `index_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = ?'; } From 13c7b950f9f724980aec4730069b2e8963100ef0 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 17:24:51 +1030 Subject: [PATCH 29/37] tidy up dropAllTables --- src/Schema/Builder.php | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 53ecf146..50a8c12a 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -144,28 +144,21 @@ public function dropAllTables() $tables = self::getTables(); $sortedTables = []; - // get all tables + // add parents counter foreach ($tables as $table) { - $tableName = $table['name']; - $parentTableName = $table['parent']; - - $sortedTables[$tableName] = [ - 'name' => $tableName, - 'parent' => $parentTableName, - 'parents' => 0 - ]; + $sortedTables[$table['name']] = ['parents' => 0, ...$table]; } // loop through all tables and count how many parents they have - foreach ($sortedTables as $tableName => $tableData) { - if(!$tableData['parent']) continue; + foreach ($sortedTables as $key => $table) { + if(!$table['parent']) continue; - $current = $tableData; + $current = $table; while($current['parent']) { - $tableData['parents'] += 1; + $table['parents'] += 1; $current = $sortedTables[$current['parent']]; } - $sortedTables[$tableName] = $tableData; + $sortedTables[$key] = $table; } // sort tables desc based on parent count @@ -175,13 +168,12 @@ public function dropAllTables() $queries = []; foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; - $blueprint = new Blueprint($tableName); $foreigns = self::getForeignListing($tableName); + $blueprint = $this->createBlueprint($tableName); foreach ($foreigns as $foreign) { - $column = new Fluent(); - $column->index = $foreign; - $queries[] = $this->grammar->compileDropForeign($blueprint, $column); + $blueprint->dropForeign($foreign); } + array_push($queries, ...$blueprint->toSql($connection, $this->grammar)); } $connection->runDdlBatch($queries); @@ -189,15 +181,14 @@ public function dropAllTables() $queries = []; foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; - $blueprint = new Blueprint($tableName); $indexes = self::getIndexListing($tableName); + $blueprint = $this->createBlueprint($tableName); foreach ($indexes as $index) { if($index == 'PRIMARY_KEY') continue; - $column = new Fluent(); - $column->index = $index; - $queries [] = $this->grammar->compileDropIndex($blueprint, $column); + $blueprint->dropIndex($index); } - $queries[] = $this->grammar->compileDrop($blueprint, new Fluent()); + $blueprint->drop(); + array_push($queries, ...$blueprint->toSql($connection, $this->grammar)); } $connection->runDdlBatch($queries); } From 8f79961b466b8dd5437dd17c81ea7d49d483f1cd Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 17:32:56 +1030 Subject: [PATCH 30/37] getForeignKeyListing > getForeignKeys --- src/Query/Processor.php | 11 +++++++++++ src/Schema/Builder.php | 6 +++--- src/Schema/Grammar.php | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Query/Processor.php b/src/Query/Processor.php index ec9106ec..f24c638e 100644 --- a/src/Query/Processor.php +++ b/src/Query/Processor.php @@ -87,4 +87,15 @@ public function processIndexListing($results) return ((object) $result)->index_name; }, $results); } + + /** + * @param array $results + * @return array + */ + public function processForeignKeys($results) + { + return array_map(function ($result) { + return ((object) $result)->key_name; + }, $results); + } } diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 50a8c12a..75572148 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -82,7 +82,7 @@ public function getIndexListing($table) * @param string $table * @return string[] */ - public function getForeignListing($table) + public function getForeignKeys($table) { $table = $this->connection->getTablePrefix().$table; @@ -93,7 +93,7 @@ public function getForeignListing($table) /** @var Processor $processor */ $processor = $this->connection->getPostProcessor(); - return $processor->processIndexListing($results); + return $processor->processForeignKeys($results); } /** @@ -168,7 +168,7 @@ public function dropAllTables() $queries = []; foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; - $foreigns = self::getForeignListing($tableName); + $foreigns = self::getForeignKeys($tableName); $blueprint = $this->createBlueprint($tableName); foreach ($foreigns as $foreign) { $blueprint->dropForeign($foreign); diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index fda9adf5..ebdd5011 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -100,7 +100,7 @@ public function compileIndexListing() */ public function compileForeignKeys() { - return 'select constraint_name as `index_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = ?'; + return 'select constraint_name as `key_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = ?'; } /** From 558f8476f7e6912e346aabda1bcd8358041028f7 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 18:55:28 +1030 Subject: [PATCH 31/37] remove unneeded override of getForeignKeys --- src/Schema/Builder.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 75572148..1b7d1657 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -78,24 +78,6 @@ public function getIndexListing($table) return $processor->processIndexListing($results); } - /** - * @param string $table - * @return string[] - */ - public function getForeignKeys($table) - { - $table = $this->connection->getTablePrefix().$table; - - $results = $this->connection->select( - $this->grammar->compileForeignKeys(), [$table] - ); - - /** @var Processor $processor */ - $processor = $this->connection->getPostProcessor(); - - return $processor->processForeignKeys($results); - } - /** * @param string $table * @param string $name From c6ba2b415f96c4aaa98128a36fd8e0089683c401 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 18:58:41 +1030 Subject: [PATCH 32/37] composer laravel dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 268654f1..99d4d42f 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "php": "^8.1", "ext-grpc": "*", "ext-json": "*", - "laravel/framework": "^10.34.2", + "laravel/framework": "^10.37.0", "google/cloud-spanner": "^1.58.4", "grpc/grpc": "^1.42", "symfony/cache": "~6", From f0f27440754dbc73f05afdcd572a505db4356b36 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 19:06:54 +1030 Subject: [PATCH 33/37] getTables inheritDoc --- src/Schema/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 1b7d1657..1752da01 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -49,8 +49,8 @@ public function getAllTables() } /** - * @inheritDoc - * + * @inheritDoc Adds a parent key, for tracking interleaving + * * @return list */ public function getTables() From 3da8fe2a879006614bc0e95bae1824cae3f99792 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 20:02:14 +1030 Subject: [PATCH 34/37] fix getForeignKey and getIndexListing > getIndexes --- CHANGELOG.md | 1 + src/Query/Processor.php | 2 +- src/Schema/Builder.php | 26 ++++---------------------- src/Schema/Grammar.php | 16 ++++++++++++---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2127052e..e382404b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Added Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) +- `Schema\Builder::getIndexListing()` `Schema\Grammar::compileIndexListing()` converted to `getIndexes()` and `compileIndexes()` to align with standard Laravel methods Fixed - `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159) diff --git a/src/Query/Processor.php b/src/Query/Processor.php index f24c638e..ec84e54c 100644 --- a/src/Query/Processor.php +++ b/src/Query/Processor.php @@ -81,7 +81,7 @@ public function processColumns($results) * @param array $results * @return array */ - public function processIndexListing($results) + public function processIndexes($results) { return array_map(function ($result) { return ((object) $result)->index_name; diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 1752da01..371445e0 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -60,24 +60,6 @@ public function getTables() ); } - /** - * @param string $table - * @return string[] - */ - public function getIndexListing($table) - { - $table = $this->connection->getTablePrefix().$table; - - $results = $this->connection->select( - $this->grammar->compileIndexListing(), [$table] - ); - - /** @var Processor $processor */ - $processor = $this->connection->getPostProcessor(); - - return $processor->processIndexListing($results); - } - /** * @param string $table * @param string $name @@ -97,7 +79,7 @@ public function dropIndex($table, $name) */ public function dropIndexIfExist($table, $name) { - if(in_array($name, $this->getIndexListing($table))) { + if(in_array($name, $this->getIndexes($table))) { $blueprint = $this->createBlueprint($table); $blueprint->dropIndex($name); $this->build($blueprint); @@ -123,7 +105,7 @@ public function dropAllTables() { /** @var Connection */ $connection = $this->connection; - $tables = self::getTables(); + $tables = $this->getTables(); $sortedTables = []; // add parents counter @@ -150,7 +132,7 @@ public function dropAllTables() $queries = []; foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; - $foreigns = self::getForeignKeys($tableName); + $foreigns = $this->getForeignKeys($tableName); $blueprint = $this->createBlueprint($tableName); foreach ($foreigns as $foreign) { $blueprint->dropForeign($foreign); @@ -163,7 +145,7 @@ public function dropAllTables() $queries = []; foreach ($sortedTables as $tableData) { $tableName = $tableData['name']; - $indexes = self::getIndexListing($tableName); + $indexes = $this->getIndexes($tableName); $blueprint = $this->createBlueprint($tableName); foreach ($indexes as $index) { if($index == 'PRIMARY_KEY') continue; diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index ebdd5011..d0dd7770 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -86,21 +86,29 @@ public function compileColumnListing() /** * Compile the query to determine the list of indexes. * + * @param string $table * @return string */ - public function compileIndexListing() + public function compileIndexes($table) { - return 'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = ?'; + return sprintf( + 'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = %s', + $this->quoteString($table) + ); } /** * Compile the query to determine the list of foreign keys. * + * @param string $table * @return string */ - public function compileForeignKeys() + public function compileForeignKeys($table) { - return 'select constraint_name as `key_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = ?'; + return sprintf( + 'select constraint_name as `key_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = %s', + $this->quoteString($table) + ); } /** From 01ecae06a15393c6176c7491c7962336f8d14b67 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 20:08:13 +1030 Subject: [PATCH 35/37] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e382404b..4e62d4d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Added Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) -- `Schema\Builder::getIndexListing()` `Schema\Grammar::compileIndexListing()` converted to `getIndexes()` and `compileIndexes()` to align with standard Laravel methods +- `Schema\Builder::getIndexListing()` `Schema\Grammar::compileIndexListing()` converted to `getIndexes()` and `compileIndexes()` to align with standard Laravel methods (#161) Fixed - `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159) From c5b51ea0684db0d2276897e355085afcf27e1b59 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 20:22:45 +1030 Subject: [PATCH 36/37] dup changelog --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e171106a..7cdf774c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,6 @@ Changed Fixed - `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159) -Fixed -- `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159) - # v6.1.1 (2023-12-11) Fixed From 7d520afad02b183f41cf51627bb1f0cfa324660f Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 14 Dec 2023 21:20:26 +1030 Subject: [PATCH 37/37] deprecate instead of delete index listing methods --- src/Query/Processor.php | 10 ++++++++++ src/Schema/Builder.php | 11 +++++++++++ src/Schema/Grammar.php | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/Query/Processor.php b/src/Query/Processor.php index ec84e54c..589c95ca 100644 --- a/src/Query/Processor.php +++ b/src/Query/Processor.php @@ -77,6 +77,16 @@ public function processColumns($results) }, $results); } + /** + * @deprecated Use processIndexes($results) instead. + * @param array $results + * @return array + */ + public function processIndexListing($results) + { + return self::processIndexes($results); + } + /** * @param array $results * @return array diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 371445e0..cc40b360 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -60,6 +60,17 @@ public function getTables() ); } + /** + * @deprecated Use getIndexes($table) instead + * + * @param string $table + * @return string[] + */ + public function getIndexListing($table) + { + return parent::getIndexes($table); + } + /** * @param string $table * @param string $name diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index d0dd7770..f1567e0f 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -83,6 +83,18 @@ public function compileColumnListing() return 'select column_name as `column_name` from information_schema.columns where table_schema = \'\' and table_name = ?'; } + /** + * Compile the query to determine the list of indexes. + * + * @deprecated Use compileIndexes($table) instead. + * + * @return string + */ + public function compileIndexListing() + { + return 'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = ?'; + } + /** * Compile the query to determine the list of indexes. *