Skip to content

Commit ab7fada

Browse files
author
Carl Sverre
authored
Merge pull request #25 from srdante/with-variables
Implement with statement for sortKey
2 parents 4afaa50 + 3ad2e27 commit ab7fada

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ Schema::create('table', function (Blueprint $table) {
270270
});
271271
```
272272

273+
### Sort Keys - Columnstore variables
274+
275+
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.
276+
277+
```php
278+
Schema::create('table', function (Blueprint $table) {
279+
$table->string('name');
280+
281+
$table->sortKey('name')->with(['columnstore_segment_rows' => 100000]);
282+
});
283+
284+
Schema::create('table', function (Blueprint $table) {
285+
$table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]);
286+
});
287+
```
288+
273289
### Series Timestamps
274290
To denote a column as a series timestamp, use the `seriesTimestamp` column modifier.
275291

src/Schema/Blueprint/InlinesIndexes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ protected function addFluentSingleStoreIndexes()
118118
foreach ($this->singleStoreIndexes as $index) {
119119
if (isset($column->{$index})) {
120120
if ($column->{$index} === true) {
121-
$this->{$index}($column->name);
121+
$command = $this->{$index}($column->name);
122122
} else {
123-
$this->{$index}($column->name, $column->{$index});
123+
$command = $this->{$index}($column->name, $column->{$index});
124+
}
125+
126+
// Forward with attributes if sortKey
127+
if ($index === 'sortKey' && isset($column->with)) {
128+
$command->with($column->with);
129+
$column->with = null;
124130
}
125131

126132
$column->{$index} = false;

src/Schema/Grammar/CompilesKeys.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command)
1717

1818
public function compileSortKey(Blueprint $blueprint, Fluent $command)
1919
{
20+
if (is_array($command->with)) {
21+
$compiled = collect($command->with)->map(function ($value, $variable) {
22+
return "{$variable}={$value}";
23+
})->join(',');
24+
25+
return "sort key({$this->columnize($command->columns)} {$command->direction}) with ({$compiled})";
26+
}
27+
2028
return "sort key({$this->columnize($command->columns)} {$command->direction})";
2129
}
2230

tests/Hybrid/CreateTable/SortKeysTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,47 @@ public function shard_and_sort_keys()
9494
'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name` asc))'
9595
);
9696
}
97+
98+
/** @test */
99+
public function it_adds_a_sort_key_with_with_statement()
100+
{
101+
$blueprint = $this->createTable(function (Blueprint $table) {
102+
$table->string('name');
103+
$table->sortKey('name')->with(['columnstore_segment_rows' => 100000]);
104+
});
105+
106+
$this->assertCreateStatement(
107+
$blueprint,
108+
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))'
109+
);
110+
}
111+
112+
/** @test */
113+
public function it_adds_a_sort_key_fluent_with_with_statement()
114+
{
115+
$blueprint = $this->createTable(function (Blueprint $table) {
116+
$table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]);
117+
});
118+
119+
$this->assertCreateStatement(
120+
$blueprint,
121+
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))'
122+
);
123+
}
124+
125+
/** @test */
126+
public function it_adds_a_sort_key_fluent_with_dual_with_statement()
127+
{
128+
$blueprint = $this->createTable(function (Blueprint $table) {
129+
$table->string('name')->sortKey()->with([
130+
'columnstore_segment_rows' => 100000,
131+
'columnstore_flush_bytes' => 4194304,
132+
]);
133+
});
134+
135+
$this->assertCreateStatement(
136+
$blueprint,
137+
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))'
138+
);
139+
}
97140
}

0 commit comments

Comments
 (0)