From 09c333f4ab5f1dd87d902174055cd6785ca5bb43 Mon Sep 17 00:00:00 2001 From: Victor Korkoshko Date: Wed, 30 Oct 2024 02:02:16 +0200 Subject: [PATCH 1/2] Added `useCascadeTruncate` method for `PostgresGrammar` --- .../Query/Grammars/PostgresGrammar.php | 20 +++++++++++++++- .../DatabasePostgresQueryGrammarTest.php | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index 4fa643bcc4bc..1f72b747f6b3 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -38,6 +38,13 @@ class PostgresGrammar extends Grammar '~', '&', '|', '#', '<<', '>>', '<<=', '>>=', ]; + /** + * Whether to use the cascade option for truncating. + * + * @var bool + */ + protected static $cascadeTruncate = true; + /** * Compile a basic where clause. * @@ -653,7 +660,7 @@ protected function compileDeleteWithJoinsOrLimit(Builder $query) */ public function compileTruncate(Builder $query) { - return ['truncate '.$this->wrapTable($query->from).' restart identity cascade' => []]; + return ['truncate '.$this->wrapTable($query->from).' restart identity'.(static::$cascadeTruncate ? ' cascade' : '') => []]; } /** @@ -802,4 +809,15 @@ public static function customOperators(array $operators) array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string'))) ); } + + /** + * Enable or disable the "cascade" option when compiling the truncate statement. + * + * @param bool $value + * @return void + */ + public static function useCascadeTruncate(bool $value = true) + { + static::$cascadeTruncate = $value; + } } diff --git a/tests/Database/DatabasePostgresQueryGrammarTest.php b/tests/Database/DatabasePostgresQueryGrammarTest.php index 6bbc313a763c..c531f69207ed 100755 --- a/tests/Database/DatabasePostgresQueryGrammarTest.php +++ b/tests/Database/DatabasePostgresQueryGrammarTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Database; use Illuminate\Database\Connection; +use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Grammars\PostgresGrammar; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -47,4 +48,27 @@ public function testCustomOperators() $this->assertNotContains(1, $operators); $this->assertSame(array_unique($operators), $operators); } + + public function testCompileTruncate() + { + $postgres = new PostgresGrammar; + $builder = m::mock(Builder::class); + $builder->from = 'users'; + + $this->assertEquals([ + 'truncate "users" restart identity cascade' => [], + ], $postgres->compileTruncate($builder)); + + PostgresGrammar::useCascadeTruncate(false); + + $this->assertEquals([ + 'truncate "users" restart identity' => [], + ], $postgres->compileTruncate($builder)); + + PostgresGrammar::useCascadeTruncate(); + + $this->assertEquals([ + 'truncate "users" restart identity cascade' => [], + ], $postgres->compileTruncate($builder)); + } } From 2d3f75aae3f54d120d827339db27a3a2f6eeed1d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 31 Oct 2024 13:01:15 -0500 Subject: [PATCH 2/2] formatting --- src/Illuminate/Database/Query/Grammars/PostgresGrammar.php | 4 ++-- tests/Database/DatabasePostgresQueryGrammarTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index 1f72b747f6b3..6e8b64e9b89c 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -39,7 +39,7 @@ class PostgresGrammar extends Grammar ]; /** - * Whether to use the cascade option for truncating. + * Indicates if the cascade option should be used when truncating. * * @var bool */ @@ -816,7 +816,7 @@ public static function customOperators(array $operators) * @param bool $value * @return void */ - public static function useCascadeTruncate(bool $value = true) + public static function cascadeOnTrucate(bool $value = true) { static::$cascadeTruncate = $value; } diff --git a/tests/Database/DatabasePostgresQueryGrammarTest.php b/tests/Database/DatabasePostgresQueryGrammarTest.php index c531f69207ed..2fc526946ef5 100755 --- a/tests/Database/DatabasePostgresQueryGrammarTest.php +++ b/tests/Database/DatabasePostgresQueryGrammarTest.php @@ -59,13 +59,13 @@ public function testCompileTruncate() 'truncate "users" restart identity cascade' => [], ], $postgres->compileTruncate($builder)); - PostgresGrammar::useCascadeTruncate(false); + PostgresGrammar::cascadeOnTrucate(false); $this->assertEquals([ 'truncate "users" restart identity' => [], ], $postgres->compileTruncate($builder)); - PostgresGrammar::useCascadeTruncate(); + PostgresGrammar::cascadeOnTrucate(); $this->assertEquals([ 'truncate "users" restart identity cascade' => [],