Skip to content

Commit 04c93b4

Browse files
authored
[12.x] Fixes issues related to Laravel 12, introduces minor refactoring (#44)
* Fix PostgresGrammar initialization and update createBlueprint method signature * Extract compiling to individual trait for re-usability
1 parent d4f62fe commit 04c93b4

File tree

3 files changed

+279
-239
lines changed

3 files changed

+279
-239
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
<?php
2+
3+
namespace ORPTech\MigrationPartition\Database\Concerns;
4+
5+
use Illuminate\Support\Fluent;
6+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
7+
8+
trait CompilesPartitionQueries
9+
{
10+
/**
11+
* Compile a create table command with its range partitions.
12+
*
13+
* @param Blueprint $blueprint
14+
* @param Fluent $command
15+
*
16+
* @return array
17+
*/
18+
public function compileCreateRangePartitioned(Blueprint $blueprint, Fluent $command): array
19+
{
20+
$columns = implode(', ', $this->getColumns($blueprint));
21+
22+
if ($primaryKey = $this->shouldUsePrimaryKey($blueprint))
23+
{
24+
$columns = sprintf('%s, %s', $columns, sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo));
25+
}
26+
27+
return array_values(array_filter(array_merge([sprintf(
28+
'create table %s (%s) partition by range (%s)',
29+
$this->wrapTable($blueprint),
30+
$columns,
31+
$blueprint->rangeKey
32+
)], $primaryKey ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
33+
}
34+
35+
/**
36+
* Compile a create table partition command for a range partitioned table.
37+
*
38+
* @param Blueprint $blueprint
39+
* @param Fluent $command
40+
*
41+
* @return array
42+
*/
43+
public function compileCreateRangePartition(Blueprint $blueprint, Fluent $command): array
44+
{
45+
return array_values(array_filter(array_merge([sprintf(
46+
'create table %s_%s partition of %s for values from (\'%s\') to (\'%s\')',
47+
str_replace("\"", "", $this->wrapTable($blueprint)),
48+
$blueprint->suffixForPartition,
49+
str_replace("\"", "", $this->wrapTable($blueprint)),
50+
$blueprint->startDate,
51+
$blueprint->endDate
52+
)], $this->shouldUsePrimaryKey($blueprint) ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
53+
}
54+
55+
/**
56+
* Compile a create table command with its list partitions.
57+
*
58+
* @param Blueprint $blueprint
59+
* @param Fluent $command
60+
*
61+
* @return string
62+
*/
63+
public function compileCreateListPartitioned(Blueprint $blueprint, Fluent $command): string
64+
{
65+
$columns = implode(', ', $this->getColumns($blueprint));
66+
67+
if ($this->shouldUsePrimaryKey($blueprint))
68+
{
69+
$columns = sprintf('%s, %s', $columns, sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo));
70+
}
71+
72+
return sprintf(
73+
'create table %s (%s) partition by list(%s)',
74+
$this->wrapTable($blueprint),
75+
$columns,
76+
$blueprint->listPartitionKey
77+
);
78+
}
79+
80+
/**
81+
* Compile an attach partition command for a range partitioned table.
82+
*
83+
* @param Blueprint $blueprint
84+
* @param Fluent $command
85+
*
86+
* @return string
87+
*/
88+
public function compileAttachRangePartition(Blueprint $blueprint, Fluent $command): string
89+
{
90+
return sprintf(
91+
'ALTER table %s attach partition %s for values from (\'%s\') to (\'%s\')',
92+
str_replace("\"", "", $this->wrapTable($blueprint)),
93+
$blueprint->partitionTableName,
94+
$blueprint->startDate,
95+
$blueprint->endDate
96+
);
97+
}
98+
99+
/**
100+
* Compile a create table partition command for a list partitioned table.
101+
*
102+
* @param Blueprint $blueprint
103+
* @param Fluent $command
104+
*
105+
* @return string
106+
*/
107+
public function compileCreateListPartition(Blueprint $blueprint, Fluent $command): string
108+
{
109+
return sprintf(
110+
'create table %s_%s partition of %s for values in (\'%s\')',
111+
str_replace("\"", "", $this->wrapTable($blueprint)),
112+
$blueprint->suffixForPartition,
113+
str_replace("\"", "", $this->wrapTable($blueprint)),
114+
$blueprint->listPartitionValue,
115+
);
116+
}
117+
118+
/**
119+
* Compile an attach partition command for a list partitioned table.
120+
*
121+
* @param Blueprint $blueprint
122+
* @param Fluent $command
123+
*
124+
* @return string
125+
*/
126+
public function compileAttachListPartition(Blueprint $blueprint, Fluent $command): string
127+
{
128+
return sprintf(
129+
'alter table %s partition of %s for values in (\'%s\')',
130+
str_replace("\"", "", $this->wrapTable($blueprint)),
131+
$blueprint->partitionTableName,
132+
$blueprint->listPartitionValue,
133+
);
134+
}
135+
136+
/**
137+
* Compile a create table partition command for a hash partitioned table.
138+
*
139+
* @param Blueprint $blueprint
140+
* @param Fluent $command
141+
*
142+
* @return string
143+
*/
144+
public function compileCreateHashPartition(Blueprint $blueprint, Fluent $command): string
145+
{
146+
return sprintf(
147+
'create table %s_%s partition of %s for values with (modulus %s, remainder %s)',
148+
str_replace("\"", "", $this->wrapTable($blueprint)),
149+
$blueprint->suffixForPartition,
150+
str_replace("\"", "", $this->wrapTable($blueprint)),
151+
$blueprint->hashModulus,
152+
$blueprint->hashRemainder
153+
);
154+
}
155+
156+
/**
157+
* Compile a create table command with its hash partitions.
158+
*
159+
* @param Blueprint $blueprint
160+
* @param Fluent $command
161+
*
162+
* @return array
163+
*/
164+
public function compileCreateHashPartitioned(Blueprint $blueprint, Fluent $command): array
165+
{
166+
$columns = implode(', ', $this->getColumns($blueprint));
167+
168+
if ($primaryKey = $this->shouldUsePrimaryKey($blueprint))
169+
{
170+
$columns = sprintf('%s, %s', $columns, sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo));
171+
}
172+
173+
return array_values(array_filter(array_merge([sprintf(
174+
'create table %s (%s) partition by hash(%s)',
175+
$this->wrapTable($blueprint),
176+
$columns,
177+
$blueprint->hashPartitionKey
178+
)], $primaryKey ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
179+
}
180+
181+
/**
182+
* Compile an attach partition command for a hash partitioned table.
183+
*
184+
* @param Blueprint $blueprint
185+
* @param Fluent $command
186+
*
187+
* @return string
188+
*/
189+
public function compileAttachHashPartition(Blueprint $blueprint, Fluent $command): string
190+
{
191+
return sprintf(
192+
'alter table %s partition of %s for values with (modulus %s, remainder %s)',
193+
str_replace("\"", "", $this->wrapTable($blueprint)),
194+
$blueprint->partitionTableName,
195+
$blueprint->hashModulus,
196+
$blueprint->hashRemainder,
197+
);
198+
}
199+
200+
/**
201+
* Get a list of all partitioned tables in the Database.
202+
*
203+
* @param string $table
204+
*
205+
* @return string
206+
*/
207+
public function compileGetPartitions(string $table): string
208+
{
209+
return sprintf(
210+
"SELECT inhrelid::regclass as tables
211+
FROM pg_catalog.pg_inherits
212+
WHERE inhparent = '%s'::regclass;",
213+
$table,
214+
);
215+
}
216+
217+
/**
218+
* Get all range partitioned tables.
219+
*
220+
* @return string
221+
*/
222+
public function compileGetAllRangePartitionedTables(): string
223+
{
224+
return "select pg_class.relname as tables from pg_class inner join pg_partitioned_table on pg_class.oid = pg_partitioned_table.partrelid where pg_partitioned_table.partstrat = 'r';";
225+
}
226+
227+
/**
228+
* Get all list partitioned tables.
229+
*
230+
* @return string
231+
*/
232+
public function compileGetAllListPartitionedTables(): string
233+
{
234+
return "select pg_class.relname as tables from pg_class inner join pg_partitioned_table on pg_class.oid = pg_partitioned_table.partrelid where pg_partitioned_table.partstrat = 'l';";
235+
}
236+
237+
/**
238+
* Get all hash partitioned tables.
239+
*
240+
* @return string
241+
*/
242+
public function compileGetAllHashPartitionedTables(): string
243+
{
244+
return "select pg_class.relname as tables from pg_class inner join pg_partitioned_table on pg_class.oid = pg_partitioned_table.partrelid where pg_partitioned_table.partstrat = 'h';";
245+
}
246+
247+
/**
248+
* Compile a detach query for a partitioned table.
249+
*
250+
* @param Blueprint $blueprint
251+
* @param Fluent $command
252+
*
253+
* @return string
254+
*/
255+
public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string
256+
{
257+
return sprintf(
258+
'alter table %s detach partition %s',
259+
str_replace("\"", "", $this->wrapTable($blueprint)),
260+
$blueprint->partitionTableName
261+
);
262+
}
263+
264+
/**
265+
* Checks if the table should have a primary key.
266+
*
267+
* @param Blueprint $blueprint
268+
*
269+
* @return bool
270+
*/
271+
public function shouldUsePrimaryKey(Blueprint $blueprint): bool
272+
{
273+
return $blueprint->pkCompositeOne && $blueprint->pkCompositeTwo;
274+
}
275+
}

src/Database/Schema/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Builder extends IlluminateBuilder
1515
public function __construct(Connection $connection)
1616
{
1717
parent::__construct($connection);
18-
$this->grammar = new PostgresGrammar();
18+
$this->grammar = new PostgresGrammar($connection);
1919
}
2020

2121
/**
@@ -284,7 +284,7 @@ public function detachPartition(string $table, Closure $callback, string $partit
284284
* @return Closure|mixed|object|Blueprint|null
285285
* @throws BindingResolutionException
286286
*/
287-
protected function createBlueprint($table, Closure $callback = null): mixed
287+
protected function createBlueprint($table, ?Closure $callback = null): mixed
288288
{
289289
$prefix = $this->connection->getConfig('prefix_indexes')
290290
? $this->connection->getConfig('prefix')

0 commit comments

Comments
 (0)