Skip to content

Commit ca2ee04

Browse files
authored
Merge branch 'main' into sort-key-improvement
2 parents 31d527f + ab7fada commit ca2ee04

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

README.md

Lines changed: 30 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

@@ -294,6 +310,20 @@ Schema::create('test', function (Blueprint $table) {
294310
});
295311
```
296312

313+
### Increment Columns without Primary Key
314+
315+
Sometimes you may want to set a custom primary key. However if your table has an int `increment` column,
316+
Laravel, by default, always sets this column as the primary key. Even if you manually set another one. This behavior can be disabled using the `withoutPrimaryKey` method.
317+
318+
```php
319+
Schema::create('test', function (Blueprint $table) {
320+
$table->id()->withoutPrimaryKey();
321+
$table->uuid('uuid');
322+
323+
$table->primaryKey(['id', 'uuid']);
324+
});
325+
```
326+
297327
## Testing
298328

299329
Execute the tests using PHPUnit

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.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,21 @@ protected function columnizeWithDirection(array $columns, string $direction)
190190
return implode(", ", array_map(function ($column) use ($direction) {
191191
return $column . ' ' . $direction;
192192
}, $wrapped));
193+
}
194+
195+
/**
196+
* Get the SQL for an auto-increment column modifier.
197+
*
198+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
199+
* @param \Illuminate\Support\Fluent $column
200+
* @return string|null
201+
*/
202+
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
203+
{
204+
if (in_array($column->type, $this->serials) && $column->autoIncrement) {
205+
return ($column->withoutPrimaryKey === true)
206+
? ' auto_increment'
207+
: ' auto_increment primary key';
208+
}
193209
}
194210
}

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->columnizeWithDirection($command->columns, $command->direction)}) with ({$compiled})";
26+
}
27+
2028
return "sort key({$this->columnizeWithDirection($command->columns, $command->direction)})";
2129
}
2230

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* @author https://github.com/srdante
4+
*/
5+
6+
namespace SingleStore\Laravel\Tests\Hybrid\CreateTable;
7+
8+
use SingleStore\Laravel\Schema\Blueprint;
9+
use SingleStore\Laravel\Tests\BaseTest;
10+
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;
11+
12+
class IncrementWithoutPrimaryKeyTest extends BaseTest
13+
{
14+
use HybridTestHelpers;
15+
16+
/** @test */
17+
public function it_adds_a_big_increments_without_primary_key()
18+
{
19+
$blueprint = $this->createTable(function (Blueprint $table) {
20+
$table->bigIncrements('id')->withoutPrimaryKey();
21+
$table->uuid('uuid');
22+
23+
$table->primary(['id', 'uuid']);
24+
});
25+
26+
$this->assertCreateStatement(
27+
$blueprint,
28+
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
29+
);
30+
}
31+
32+
/** @test */
33+
public function it_adds_an_id_without_primary_key()
34+
{
35+
$blueprint = $this->createTable(function (Blueprint $table) {
36+
$table->id()->withoutPrimaryKey();
37+
$table->uuid('uuid');
38+
39+
$table->primary(['id', 'uuid']);
40+
});
41+
42+
$this->assertCreateStatement(
43+
$blueprint,
44+
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
45+
);
46+
}
47+
}

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)