diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index 4fa643bcc4bc..6e8b64e9b89c 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 '~', '&', '|', '#', '<<', '>>', '<<=', '>>=', ]; + /** + * Indicates if the cascade option should be used when 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 cascadeOnTrucate(bool $value = true) + { + static::$cascadeTruncate = $value; + } } diff --git a/tests/Database/DatabasePostgresQueryGrammarTest.php b/tests/Database/DatabasePostgresQueryGrammarTest.php index 6bbc313a763c..2fc526946ef5 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::cascadeOnTrucate(false); + + $this->assertEquals([ + 'truncate "users" restart identity' => [], + ], $postgres->compileTruncate($builder)); + + PostgresGrammar::cascadeOnTrucate(); + + $this->assertEquals([ + 'truncate "users" restart identity cascade' => [], + ], $postgres->compileTruncate($builder)); + } }