Skip to content

Commit 67aaf0a

Browse files
author
Carl Sverre
authored
Merge pull request #24 from srdante/sort-key-desc
Implement sortKey direction argument
2 parents 96ed752 + 54595a5 commit 67aaf0a

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ Schema::create('table', function (Blueprint $table) {
217217

218218
```
219219

220+
Sort keys by default works only for `asc` sort queries. If you would like to create a sort key with `desc` order, you can set the key direction.
221+
222+
```php
223+
Schema::create('table', function (Blueprint $table) {
224+
$table->string('name');
225+
226+
$table->sortKey('name', 'desc');
227+
});
228+
229+
Schema::create('table', function (Blueprint $table) {
230+
$table->string('name')->sortKey('desc');
231+
});
232+
```
233+
220234
### Series Timestamps
221235
To denote a column as a series timestamp, use the `seriesTimestamp` column modifier.
222236

src/Schema/Blueprint/InlinesIndexes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ protected function inlineCreateIndexStatements($statements)
8181
protected function indexCommands()
8282
{
8383
return $this->commandsNamed(array_merge(
84-
$this->singleStoreIndexes, $this->mysqlIndexes
84+
$this->singleStoreIndexes,
85+
$this->mysqlIndexes
8586
));
8687
}
8788

@@ -116,7 +117,12 @@ protected function addFluentSingleStoreIndexes()
116117
foreach ($this->columns as $column) {
117118
foreach ($this->singleStoreIndexes as $index) {
118119
if (isset($column->{$index})) {
119-
$this->{$index}($column->name, ($column->{$index} === true ? null : $column->{$index}));
120+
if ($column->{$index} === true) {
121+
$this->{$index}($column->name);
122+
} else {
123+
$this->{$index}($column->name, $column->{$index});
124+
}
125+
120126
$column->{$index} = false;
121127
}
122128
}

src/Schema/Blueprint/ModifiesIndexes.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ trait ModifiesIndexes
1111
{
1212
/**
1313
* @param $columns
14-
* @param $name
1514
* @return \Illuminate\Support\Fluent
1615
*/
17-
public function shardKey($columns, $name = null)
16+
public function shardKey($columns)
1817
{
19-
return $this->indexCommand('shardKey', $columns, $name);
18+
return $this->indexCommand('shardKey', $columns, null);
2019
}
2120

2221
/**
2322
* @param $columns
24-
* @param $name
23+
* @param $direction
2524
* @return \Illuminate\Support\Fluent
2625
*/
27-
public function sortKey($columns, $name = null)
26+
public function sortKey($columns, $direction = 'asc')
2827
{
29-
return $this->indexCommand('sortKey', $columns, $name);
28+
$command = $this->indexCommand('sortKey', $columns, null);
29+
$command->direction = $direction;
30+
31+
return $command;
3032
}
3133

3234
/**

src/Schema/Grammar/CompilesKeys.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command)
1717

1818
public function compileSortKey(Blueprint $blueprint, Fluent $command)
1919
{
20-
return "sort key({$this->columnize($command->columns)})";
20+
return "sort key({$this->columnize($command->columns)} {$command->direction})";
2121
}
2222

2323
public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)

tests/Hybrid/CreateTable/SortKeysTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ public function it_adds_a_sort_key_standalone()
2323

2424
$this->assertCreateStatement(
2525
$blueprint,
26-
'create table `test` (`name` varchar(255) not null, sort key(`name`))'
26+
'create table `test` (`name` varchar(255) not null, sort key(`name` asc))'
27+
);
28+
}
29+
30+
/** @test */
31+
public function it_adds_a_sort_key_with_desc_direction_standalone()
32+
{
33+
$blueprint = $this->createTable(function (Blueprint $table) {
34+
$table->string('name');
35+
$table->sortKey('name', 'desc');
36+
});
37+
38+
$this->assertCreateStatement(
39+
$blueprint,
40+
'create table `test` (`name` varchar(255) not null, sort key(`name` desc))'
2741
);
2842
}
2943

@@ -36,7 +50,20 @@ public function it_adds_a_sort_key_fluent()
3650

3751
$this->assertCreateStatement(
3852
$blueprint,
39-
'create table `test` (`name` varchar(255) not null, sort key(`name`))'
53+
'create table `test` (`name` varchar(255) not null, sort key(`name` asc))'
54+
);
55+
}
56+
57+
/** @test */
58+
public function it_adds_a_sort_key_with_desc_direction_fluent()
59+
{
60+
$blueprint = $this->createTable(function (Blueprint $table) {
61+
$table->string('name')->sortKey('desc');
62+
});
63+
64+
$this->assertCreateStatement(
65+
$blueprint,
66+
'create table `test` (`name` varchar(255) not null, sort key(`name` desc))'
4067
);
4168
}
4269

@@ -51,7 +78,7 @@ public function it_adds_a_dual_sort_key()
5178

5279
$this->assertCreateStatement(
5380
$blueprint,
54-
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name`, `l_name`))'
81+
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name`, `l_name` asc))'
5582
);
5683
}
5784

@@ -64,7 +91,7 @@ public function shard_and_sort_keys()
6491

6592
$this->assertCreateStatement(
6693
$blueprint,
67-
'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name`))'
94+
'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name` asc))'
6895
);
6996
}
7097
}

0 commit comments

Comments
 (0)