diff --git a/README.md b/README.md index 4b93b2e..8a63460 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,22 @@ Schema::create('table', function (Blueprint $table) { }); ``` +### Sort Keys - Columnstore variables + +Sometimes you may want to customize your [columstore variables](https://docs.singlestore.com/managed-service/en/create-a-database/physical-database-schema-design/procedures-for-physical-database-schema-design/configuring-the-columnstore-to-work-effectively.html) individually per table. You can do it by appending `with` fluently to the `sortKey` definition. + +```php +Schema::create('table', function (Blueprint $table) { + $table->string('name'); + + $table->sortKey('name')->with(['columnstore_segment_rows' => 100000]); +}); + +Schema::create('table', function (Blueprint $table) { + $table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]); +}); +``` + ### Series Timestamps To denote a column as a series timestamp, use the `seriesTimestamp` column modifier. diff --git a/src/Schema/Blueprint/InlinesIndexes.php b/src/Schema/Blueprint/InlinesIndexes.php index e6ab94d..8550db9 100644 --- a/src/Schema/Blueprint/InlinesIndexes.php +++ b/src/Schema/Blueprint/InlinesIndexes.php @@ -118,9 +118,15 @@ protected function addFluentSingleStoreIndexes() foreach ($this->singleStoreIndexes as $index) { if (isset($column->{$index})) { if ($column->{$index} === true) { - $this->{$index}($column->name); + $command = $this->{$index}($column->name); } else { - $this->{$index}($column->name, $column->{$index}); + $command = $this->{$index}($column->name, $column->{$index}); + } + + // Forward with attributes if sortKey + if ($index === 'sortKey' && isset($column->with)) { + $command->with($column->with); + $column->with = null; } $column->{$index} = false; diff --git a/src/Schema/Grammar/CompilesKeys.php b/src/Schema/Grammar/CompilesKeys.php index 387c76a..2363675 100644 --- a/src/Schema/Grammar/CompilesKeys.php +++ b/src/Schema/Grammar/CompilesKeys.php @@ -17,6 +17,14 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command) public function compileSortKey(Blueprint $blueprint, Fluent $command) { + if (is_array($command->with)) { + $compiled = collect($command->with)->map(function ($value, $variable) { + return "{$variable}={$value}"; + })->join(','); + + return "sort key({$this->columnize($command->columns)} {$command->direction}) with ({$compiled})"; + } + return "sort key({$this->columnize($command->columns)} {$command->direction})"; } diff --git a/tests/Hybrid/CreateTable/SortKeysTest.php b/tests/Hybrid/CreateTable/SortKeysTest.php index ed22c9a..a166a2d 100644 --- a/tests/Hybrid/CreateTable/SortKeysTest.php +++ b/tests/Hybrid/CreateTable/SortKeysTest.php @@ -94,4 +94,47 @@ public function shard_and_sort_keys() 'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name` asc))' ); } + + /** @test */ + public function it_adds_a_sort_key_with_with_statement() + { + $blueprint = $this->createTable(function (Blueprint $table) { + $table->string('name'); + $table->sortKey('name')->with(['columnstore_segment_rows' => 100000]); + }); + + $this->assertCreateStatement( + $blueprint, + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))' + ); + } + + /** @test */ + public function it_adds_a_sort_key_fluent_with_with_statement() + { + $blueprint = $this->createTable(function (Blueprint $table) { + $table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]); + }); + + $this->assertCreateStatement( + $blueprint, + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))' + ); + } + + /** @test */ + public function it_adds_a_sort_key_fluent_with_dual_with_statement() + { + $blueprint = $this->createTable(function (Blueprint $table) { + $table->string('name')->sortKey()->with([ + 'columnstore_segment_rows' => 100000, + 'columnstore_flush_bytes' => 4194304, + ]); + }); + + $this->assertCreateStatement( + $blueprint, + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))' + ); + } }