Skip to content

Commit 4fcf6e5

Browse files
author
Carl Sverre
committed
Added unique keys test and notes in README
fixes #15
1 parent 67aaf0a commit 4fcf6e5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This package is currently in a pre-release beta, please use with caution and ope
1515
- [Sparse Tables](#sparse-tables)
1616
- [Shard Keys](#shard-keys)
1717
- [Sort Keys](#sort-keys)
18+
- [Unique Keys](#unique-keys)
1819
- [Series Timestamps](#series-timestamps)
1920
- [Computed Columns](#computed-columns)
2021
- [Testing](#testing)
@@ -214,7 +215,6 @@ Schema::create('table', function (Blueprint $table) {
214215

215216
$table->sortKey(['f_name', 'l_name']);
216217
});
217-
218218
```
219219

220220
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.
@@ -231,6 +231,28 @@ Schema::create('table', function (Blueprint $table) {
231231
});
232232
```
233233

234+
### Unique Keys
235+
236+
You can add an `unique key` to your tables using the standalone `unique` method, or fluently by appending `unique` to the column definition.
237+
238+
> **Note:**
239+
> SingleStore requires that the shard key is contained within an unique key. This means that in most cases you can't use the fluent api as you will likely need to specify more than one column. This restriction does not apply to reference tables.
240+
241+
```php
242+
Schema::create('table', function (Blueprint $table) {
243+
$table->string('key');
244+
$table->string('val');
245+
246+
$table->shardKey('key');
247+
$table->unique(['key', 'val']);
248+
});
249+
250+
Schema::create('table', function (Blueprint $table) {
251+
$table->reference();
252+
$table->string('name')->unique();
253+
});
254+
```
255+
234256
### Series Timestamps
235257
To denote a column as a series timestamp, use the `seriesTimestamp` column modifier.
236258

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
5+
*/
6+
7+
namespace SingleStore\Laravel\Tests\Hybrid\CreateTable;
8+
9+
use SingleStore\Laravel\Schema\Blueprint;
10+
use SingleStore\Laravel\Tests\BaseTest;
11+
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;
12+
13+
class UniqueKeysTest extends BaseTest
14+
{
15+
use HybridTestHelpers;
16+
17+
/** @test */
18+
public function it_adds_a_unique_key()
19+
{
20+
$blueprint = $this->createTable(function (Blueprint $table) {
21+
$table->string('key');
22+
$table->string('val');
23+
24+
$table->shardKey('key');
25+
$table->unique(['key', 'val']);
26+
});
27+
28+
$this->assertCreateStatement(
29+
$blueprint,
30+
'create table `test` (`key` varchar(255) not null, `val` varchar(255) not null, shard key(`key`), unique `test_key_val_unique`(`key`, `val`))'
31+
);
32+
}
33+
34+
/** @test */
35+
public function it_adds_a_unique_key_reference_fluent()
36+
{
37+
$blueprint = $this->createTable(function (Blueprint $table) {
38+
$table->reference();
39+
$table->string('name')->unique();
40+
});
41+
42+
$this->assertCreateStatement(
43+
$blueprint,
44+
'create reference table `test` (`name` varchar(255) not null, unique `test_name_unique`(`name`))'
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)