From 22ec78579b4b4416be805b5548ff073d676eb3c3 Mon Sep 17 00:00:00 2001 From: srdante Date: Sun, 11 Sep 2022 00:07:51 -0300 Subject: [PATCH 1/5] Add with statement --- src/Schema/Blueprint/InlinesIndexes.php | 12 ++++++++++-- src/Schema/Grammar/CompilesKeys.php | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Schema/Blueprint/InlinesIndexes.php b/src/Schema/Blueprint/InlinesIndexes.php index c690a37..691cab8 100644 --- a/src/Schema/Blueprint/InlinesIndexes.php +++ b/src/Schema/Blueprint/InlinesIndexes.php @@ -81,7 +81,8 @@ protected function inlineCreateIndexStatements($statements) protected function indexCommands() { return $this->commandsNamed(array_merge( - $this->singleStoreIndexes, $this->mysqlIndexes + $this->singleStoreIndexes, + $this->mysqlIndexes )); } @@ -116,7 +117,14 @@ protected function addFluentSingleStoreIndexes() foreach ($this->columns as $column) { foreach ($this->singleStoreIndexes as $index) { if (isset($column->{$index})) { - $this->{$index}($column->name, ($column->{$index} === true ? null : $column->{$index})); + $command = $this->{$index}($column->name, ($column->{$index} === true ? null : $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 aec8ee0..871489a 100644 --- a/src/Schema/Grammar/CompilesKeys.php +++ b/src/Schema/Grammar/CompilesKeys.php @@ -17,6 +17,15 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command) public function compileSortKey(Blueprint $blueprint, Fluent $command) { + if (is_array($command->with)) { + $compiled = collect($command->with)->map( + fn ($value, $variable) => "{$variable}={$value}" + )->join(','); + + return "sort key({$this->columnize($command->columns)}) with ({$compiled})"; + } + + return "sort key({$this->columnize($command->columns)})"; } From a976895dfa9726a7703b3384ba896dca95c60d88 Mon Sep 17 00:00:00 2001 From: srdante Date: Sun, 11 Sep 2022 00:08:26 -0300 Subject: [PATCH 2/5] Add tests --- tests/Hybrid/CreateTable/SortKeysTest.php | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/Hybrid/CreateTable/SortKeysTest.php b/tests/Hybrid/CreateTable/SortKeysTest.php index 91f36b3..46444fb 100644 --- a/tests/Hybrid/CreateTable/SortKeysTest.php +++ b/tests/Hybrid/CreateTable/SortKeysTest.php @@ -67,4 +67,47 @@ public function shard_and_sort_keys() 'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name`))' ); } + + /** @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`) 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`) 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`) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))' + ); + } } From d9a0c7e81e51d7198a63ba8cc45ebbf6ee80b802 Mon Sep 17 00:00:00 2001 From: srdante Date: Sun, 11 Sep 2022 00:39:40 -0300 Subject: [PATCH 3/5] Add columnstore variables to README --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index ddb8d9a..fe9a240 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,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. From aa879a1640bdcfd6e6b9bbbb89a2d5f3ecd9bc89 Mon Sep 17 00:00:00 2001 From: srdante Date: Sat, 17 Sep 2022 00:39:13 -0300 Subject: [PATCH 4/5] Fix tests --- tests/Hybrid/CreateTable/SortKeysTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Hybrid/CreateTable/SortKeysTest.php b/tests/Hybrid/CreateTable/SortKeysTest.php index 0599646..a166a2d 100644 --- a/tests/Hybrid/CreateTable/SortKeysTest.php +++ b/tests/Hybrid/CreateTable/SortKeysTest.php @@ -105,7 +105,7 @@ public function it_adds_a_sort_key_with_with_statement() $this->assertCreateStatement( $blueprint, - 'create table `test` (`name` varchar(255) not null, sort key(`name`) with (columnstore_segment_rows=100000))' + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))' ); } @@ -118,7 +118,7 @@ public function it_adds_a_sort_key_fluent_with_with_statement() $this->assertCreateStatement( $blueprint, - 'create table `test` (`name` varchar(255) not null, sort key(`name`) with (columnstore_segment_rows=100000))' + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))' ); } @@ -134,7 +134,7 @@ public function it_adds_a_sort_key_fluent_with_dual_with_statement() $this->assertCreateStatement( $blueprint, - 'create table `test` (`name` varchar(255) not null, sort key(`name`) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))' + 'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))' ); } } From 3ad2e2785f8c11e16dbd6cec0f0982dcf52e14e6 Mon Sep 17 00:00:00 2001 From: srdante Date: Mon, 19 Sep 2022 21:00:36 -0300 Subject: [PATCH 5/5] fix compatibility for PHP 7.3 --- src/Schema/Grammar/CompilesKeys.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Schema/Grammar/CompilesKeys.php b/src/Schema/Grammar/CompilesKeys.php index 41fc206..2363675 100644 --- a/src/Schema/Grammar/CompilesKeys.php +++ b/src/Schema/Grammar/CompilesKeys.php @@ -18,9 +18,9 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command) public function compileSortKey(Blueprint $blueprint, Fluent $command) { if (is_array($command->with)) { - $compiled = collect($command->with)->map( - fn ($value, $variable) => "{$variable}={$value}" - )->join(','); + $compiled = collect($command->with)->map(function ($value, $variable) { + return "{$variable}={$value}"; + })->join(','); return "sort key({$this->columnize($command->columns)} {$command->direction}) with ({$compiled})"; }