Skip to content

Commit d7e7128

Browse files
author
Carl Sverre
authored
Merge pull request #29 from srdante/sort-key-per-column-direction
Implement Sort Key per-column direction feature
2 parents 221bef8 + 4a81212 commit d7e7128

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ Schema::create('table', function (Blueprint $table) {
248248
});
249249
```
250250

251+
You may also define the sort key direction per-column using the following syntax:
252+
253+
```php
254+
Schema::create('table', function (Blueprint $table) {
255+
$table->string('f_name');
256+
$table->string('l_name');
257+
258+
$table->sortKey([['f_name', 'asc'], ['l_name', 'desc']]);
259+
});
260+
```
261+
251262
### Unique Keys
252263

253264
You can add an `unique key` to your tables using the standalone `unique` method, or fluently by appending `unique` to the column definition.

src/Schema/Blueprint/ModifiesIndexes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait ModifiesIndexes
1515
*/
1616
public function shardKey($columns)
1717
{
18-
return $this->indexCommand('shardKey', $columns, null);
18+
return $this->indexCommand('shardKey', $columns, 'shardKeyDummyName');
1919
}
2020

2121
/**
@@ -25,7 +25,7 @@ public function shardKey($columns)
2525
*/
2626
public function sortKey($columns, $direction = 'asc')
2727
{
28-
$command = $this->indexCommand('sortKey', $columns, null);
28+
$command = $this->indexCommand('sortKey', $columns, 'sortKeyDummyName');
2929
$command->direction = $direction;
3030

3131
return $command;

src/Schema/Grammar.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
1212
use Illuminate\Support\Fluent;
1313
use Illuminate\Support\Str;
14+
use InvalidArgumentException;
1415
use SingleStore\Laravel\Schema\Blueprint as SingleStoreBlueprint;
1516
use SingleStore\Laravel\Schema\Grammar\CompilesKeys;
1617
use SingleStore\Laravel\Schema\Grammar\ModifiesColumns;
@@ -185,6 +186,24 @@ protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
185186
*/
186187
protected function columnizeWithDirection(array $columns, string $direction)
187188
{
189+
if ($columns === array_filter($columns, 'is_array')) {
190+
$columnNames = array_map(function ($column) {
191+
return $this->wrap($column[0]);
192+
}, $columns);
193+
194+
$columnDirections = array_map(function ($column) {
195+
return $column[1];
196+
}, $columns);
197+
198+
return implode(', ', array_map(function ($column, $direction) {
199+
return "$column $direction";
200+
}, $columnNames, $columnDirections));
201+
}
202+
203+
if (array_filter($columns, 'is_array') !== []) {
204+
throw new InvalidArgumentException('You must set the direction for each sort key column or use the second parameter to set the direction for all sort key columns');
205+
}
206+
188207
$wrapped = array_map([$this, 'wrap'], $columns);
189208

190209
return implode(', ', array_map(function ($column) use ($direction) {

tests/Hybrid/CreateTable/SortKeysTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace SingleStore\Laravel\Tests\Hybrid\CreateTable;
77

8+
use InvalidArgumentException;
89
use SingleStore\Laravel\Schema\Blueprint;
910
use SingleStore\Laravel\Tests\BaseTest;
1011
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;
@@ -82,6 +83,64 @@ public function it_adds_a_dual_sort_key()
8283
);
8384
}
8485

86+
/** @test */
87+
public function it_adds_a_dual_sort_key_with_desc_direction()
88+
{
89+
$blueprint = $this->createTable(function (Blueprint $table) {
90+
$table->string('f_name');
91+
$table->string('l_name');
92+
$table->sortKey(['f_name', 'l_name'], 'desc');
93+
});
94+
95+
$this->assertCreateStatement(
96+
$blueprint,
97+
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name` desc, `l_name` desc))'
98+
);
99+
}
100+
101+
/** @test */
102+
public function it_adds_a_dual_sort_key_with_different_directions()
103+
{
104+
$blueprint = $this->createTable(function (Blueprint $table) {
105+
$table->string('f_name');
106+
$table->string('l_name');
107+
$table->sortKey([['f_name', 'asc'], ['l_name', 'desc']]);
108+
});
109+
110+
$this->assertCreateStatement(
111+
$blueprint,
112+
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name` asc, `l_name` desc))'
113+
);
114+
}
115+
116+
/** @test */
117+
public function it_cannot_add_a_dual_sort_key_with_only_one_direction()
118+
{
119+
$this->expectException(InvalidArgumentException::class);
120+
121+
$blueprint = $this->createTable(function (Blueprint $table) {
122+
$table->string('f_name');
123+
$table->string('l_name');
124+
$table->sortKey(['f_name', ['l_name', 'desc']]);
125+
});
126+
127+
$blueprint->toSql($this->getConnection(), $this->getGrammar());
128+
}
129+
130+
/** @test */
131+
public function it_cannot_add_a_dual_sort_key_with_only_one_direction_desc()
132+
{
133+
$this->expectException(InvalidArgumentException::class);
134+
135+
$blueprint = $this->createTable(function (Blueprint $table) {
136+
$table->string('f_name');
137+
$table->string('l_name');
138+
$table->sortKey(['f_name', ['l_name', 'asc']], 'desc');
139+
});
140+
141+
$blueprint->toSql($this->getConnection(), $this->getGrammar());
142+
}
143+
85144
/** @test */
86145
public function shard_and_sort_keys()
87146
{

0 commit comments

Comments
 (0)