Skip to content

Commit a055bb2

Browse files
authored
Merge pull request #7 from denizgolbas/Attach-Range-Partition-Between-Date
2 parents c531d2e + 25a8134 commit a055bb2

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/Database/Schema/Blueprint.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
2929
*/
3030
public $rangeKey;
3131

32+
/**
33+
* Partition range key for creating a partitioned table.
34+
*
35+
* @var bool
36+
*/
37+
public $subfixForPartition;
38+
39+
/**
40+
* Partition range key for creating a partitioned table.
41+
*
42+
* @var bool
43+
*/
44+
public $startDate;
45+
46+
/**
47+
* Partition range key for creating a partitioned table.
48+
*
49+
* @var bool
50+
*/
51+
public $endDate;
52+
53+
3254
/**
3355
* Determine if the blueprint has a create command.
3456
*
@@ -50,4 +72,14 @@ public function createPartitioned()
5072
{
5173
return $this->addCommand('createPartitioned');
5274
}
75+
76+
/**
77+
* Create partition and attach parttioned table.
78+
*
79+
* @return \Illuminate\Support\Fluent
80+
*/
81+
public function attachPartition()
82+
{
83+
return $this->addCommand('attachPartition');
84+
}
5385
}

src/Database/Schema/Builder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ public function createPartitioned($table, Closure $callback, string $pkComposite
3434
}));
3535
}
3636

37+
/**
38+
* Create a new table on the schema with partitions.
39+
*
40+
* @param string $table
41+
* @param \Closure $callback
42+
* @return void
43+
*/
44+
public function attachPartition($table, Closure $callback, string $subfixForPartition, string $startDate, string $endDate)
45+
{
46+
$this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback, $subfixForPartition, $startDate, $endDate) {
47+
$blueprint->attachPartition();
48+
$blueprint->subfixForPartition = $subfixForPartition;
49+
$blueprint->startDate = $startDate;
50+
$blueprint->endDate = $endDate;
51+
52+
$callback($blueprint);
53+
}));
54+
}
55+
3756
/**
3857
* Create a new command set with a Closure.
3958
*

src/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,22 @@ public function compileCreatePartitioned(Blueprint $blueprint, Fluent $command)
2323
$blueprint->rangeKey
2424
)], $this->compileAutoIncrementStartingValues($blueprint))));
2525
}
26+
/**
27+
* Compile a create table partition command for partitioned table
28+
*
29+
* @param Blueprint $blueprint
30+
* @param \Illuminate\Support\Fluent $command
31+
* @return array
32+
*/
33+
public function compileAttachPartition(Blueprint $blueprint, Fluent $command)
34+
{
35+
return array_values(array_filter(array_merge([sprintf('%s table %s_%s partition of %s for values from (\'%s\') to (\'%s\')',
36+
$blueprint->temporary ? 'create temporary' : 'create',
37+
str_replace("\"", "", $this->wrapTable($blueprint)),
38+
$blueprint->subfixForPartition,
39+
str_replace("\"", "", $this->wrapTable($blueprint)),
40+
$blueprint->startDate,
41+
$blueprint->endDate
42+
)], $this->compileAutoIncrementStartingValues($blueprint))));
43+
}
2644
}

src/Support/Facades/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/**
1010
* @method static createPartitioned(string $table, \Closure $callback, string $pkCompositeOne, string $pkCompositeTwo, string $rangeKey)
11-
*
11+
* @method static attachPartition (string $table, \Closure $callback, string $subfixForPartition, string $startDate, string $endDate)
1212
* @see \ORPTech\MigrationPartition\Database\Schema\Builder
1313
*/
1414
class Schema extends Facade

0 commit comments

Comments
 (0)