Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to create partitioned tables without primary key #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
class Blueprint extends IlluminateBlueprint
{
/**
* Column key one for creating a composite key for a range partitioned table.
* Column key one for creating a composite key for a range partitioned table. Use NULL if table has no primary key
*
* @var string
* @var string|null
*/
public $pkCompositeOne;

/**
* Column key two for creating a composite key for range partitioned table.
* Column key two for creating a composite key for range partitioned table. Use NULL if table has no primary key
*
* @var string
* @var string|null
*/
public $pkCompositeTwo;

Expand Down
6 changes: 3 additions & 3 deletions src/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public function __construct(Connection $connection)
*
* @param string $table
* @param Closure $callback
* @param string $pkCompositeOne
* @param string $pkCompositeTwo
* @param string|null $pkCompositeOne
* @param string|null $pkCompositeTwo
* @param string $rangeKey
* @return void
* @throws BindingResolutionException
*/
public function createRangePartitioned(string $table, Closure $callback, string $pkCompositeOne, string $pkCompositeTwo, string $rangeKey)
public function createRangePartitioned(string $table, Closure $callback, ?string $pkCompositeOne = null, ?string $pkCompositeTwo = null, string $rangeKey)
{
$this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback, $pkCompositeOne, $pkCompositeTwo, $rangeKey) {
$blueprint->createRangePartitioned();
Expand Down
33 changes: 28 additions & 5 deletions src/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ class PostgresGrammar extends IlluminatePostgresGrammar
*/
public function compileCreateRangePartitioned(Blueprint $blueprint, Fluent $command)
{
$columns = implode(', ', $this->getColumns($blueprint));

if ($primaryKey = $this->shouldUsePrimaryKey($blueprint)) {
$columns = sprintf('%s, %s', $columns, sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo));
}

return array_values(array_filter(array_merge([sprintf('create table %s (%s) partition by range (%s)',
$this->wrapTable($blueprint),
sprintf('%s, %s', implode(', ', $this->getColumns($blueprint)), sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo)),
$columns,
$blueprint->rangeKey
)], $this->compileAutoIncrementStartingValues($blueprint))));
)], $primaryKey ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
}

/**
Expand All @@ -39,7 +45,7 @@ public function compileCreateRangePartition(Blueprint $blueprint, Fluent $comman
str_replace("\"", "", $this->wrapTable($blueprint)),
$blueprint->startDate,
$blueprint->endDate
)], $this->compileAutoIncrementStartingValues($blueprint))));
)], $this->shouldUsePrimaryKey($blueprint) ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
}

/**
Expand Down Expand Up @@ -117,11 +123,17 @@ public function compileAttachListPartition(Blueprint $blueprint, Fluent $command
*/
public function compileCreateHashPartitioned(Blueprint $blueprint, Fluent $command)
{
$columns = implode(', ', $this->getColumns($blueprint));

if ($primaryKey = $this->shouldUsePrimaryKey($blueprint)) {
$columns = sprintf('%s, %s', $columns, sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo));
}

return array_values(array_filter(array_merge([sprintf('create table %s (%s) partition by hash(%s)',
$this->wrapTable($blueprint),
sprintf('%s, %s', implode(', ', $this->getColumns($blueprint)), sprintf('primary key (%s, %s)', $blueprint->pkCompositeOne, $blueprint->pkCompositeTwo)),
$columns,
$blueprint->hashPartitionKey
)], $this->compileAutoIncrementStartingValues($blueprint))));
)], $primaryKey ? $this->compileAutoIncrementStartingValues($blueprint, $command) : [])));
}

/**
Expand Down Expand Up @@ -215,4 +227,15 @@ public function compileDetachPartition(Blueprint $blueprint, Fluent $command)
$blueprint->partitionTableName
);
}

/**
* Does the table have a primary key
*
* @param Blueprint $blueprint
* @return bool
*/
public function shouldUsePrimaryKey(Blueprint $blueprint)
{
return $blueprint->pkCompositeOne && $blueprint->pkCompositeTwo;
}
}
2 changes: 1 addition & 1 deletion src/Support/Facades/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Range partitioning related methods.
* @method static createRangePartitioned(string $table, \Closure $callback, string $pkCompositeOne, string $pkCompositeTwo, string $rangeKey)
* @method static createRangePartitioned(string $table, \Closure $callback, ?string $pkCompositeOne = null, ?string $pkCompositeTwo = null, string $rangeKey)
* @method static createRangePartition(string $table, \Closure $callback, string $suffixForPartition, string $startDate, string $endDate)
* @method static attachRangePartition(string $table, \Closure $callback, string $partitionTableName, string $startDate, string $endDate)
* @method static getAllRangePartitionedTables()
Expand Down