diff --git a/src/Database/Schema/Blueprint.php b/src/Database/Schema/Blueprint.php index e2a83ed..bfe2ce3 100644 --- a/src/Database/Schema/Blueprint.php +++ b/src/Database/Schema/Blueprint.php @@ -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; diff --git a/src/Database/Schema/Builder.php b/src/Database/Schema/Builder.php index 9eda967..9821bf5 100755 --- a/src/Database/Schema/Builder.php +++ b/src/Database/Schema/Builder.php @@ -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(); diff --git a/src/Database/Schema/Grammars/PostgresGrammar.php b/src/Database/Schema/Grammars/PostgresGrammar.php index c72b08b..497d7ca 100755 --- a/src/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Database/Schema/Grammars/PostgresGrammar.php @@ -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) : []))); } /** @@ -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) : []))); } /** @@ -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) : []))); } /** @@ -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; + } } diff --git a/src/Support/Facades/Schema.php b/src/Support/Facades/Schema.php index 32a11b6..577bf07 100755 --- a/src/Support/Facades/Schema.php +++ b/src/Support/Facades/Schema.php @@ -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()