Skip to content

Commit 79897cd

Browse files
[11.x] Added useCascadeTruncate method for PostgresGrammar (#53343)
* Added `useCascadeTruncate` method for `PostgresGrammar` * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9d974bb commit 79897cd

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Illuminate/Database/Query/Grammars/PostgresGrammar.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class PostgresGrammar extends Grammar
3838
'~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
3939
];
4040

41+
/**
42+
* Indicates if the cascade option should be used when truncating.
43+
*
44+
* @var bool
45+
*/
46+
protected static $cascadeTruncate = true;
47+
4148
/**
4249
* Compile a basic where clause.
4350
*
@@ -653,7 +660,7 @@ protected function compileDeleteWithJoinsOrLimit(Builder $query)
653660
*/
654661
public function compileTruncate(Builder $query)
655662
{
656-
return ['truncate '.$this->wrapTable($query->from).' restart identity cascade' => []];
663+
return ['truncate '.$this->wrapTable($query->from).' restart identity'.(static::$cascadeTruncate ? ' cascade' : '') => []];
657664
}
658665

659666
/**
@@ -802,4 +809,15 @@ public static function customOperators(array $operators)
802809
array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string')))
803810
);
804811
}
812+
813+
/**
814+
* Enable or disable the "cascade" option when compiling the truncate statement.
815+
*
816+
* @param bool $value
817+
* @return void
818+
*/
819+
public static function cascadeOnTrucate(bool $value = true)
820+
{
821+
static::$cascadeTruncate = $value;
822+
}
805823
}

tests/Database/DatabasePostgresQueryGrammarTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Database;
44

55
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Query\Builder;
67
use Illuminate\Database\Query\Grammars\PostgresGrammar;
78
use Mockery as m;
89
use PHPUnit\Framework\TestCase;
@@ -47,4 +48,27 @@ public function testCustomOperators()
4748
$this->assertNotContains(1, $operators);
4849
$this->assertSame(array_unique($operators), $operators);
4950
}
51+
52+
public function testCompileTruncate()
53+
{
54+
$postgres = new PostgresGrammar;
55+
$builder = m::mock(Builder::class);
56+
$builder->from = 'users';
57+
58+
$this->assertEquals([
59+
'truncate "users" restart identity cascade' => [],
60+
], $postgres->compileTruncate($builder));
61+
62+
PostgresGrammar::cascadeOnTrucate(false);
63+
64+
$this->assertEquals([
65+
'truncate "users" restart identity' => [],
66+
], $postgres->compileTruncate($builder));
67+
68+
PostgresGrammar::cascadeOnTrucate();
69+
70+
$this->assertEquals([
71+
'truncate "users" restart identity cascade' => [],
72+
], $postgres->compileTruncate($builder));
73+
}
5074
}

0 commit comments

Comments
 (0)