Skip to content

Commit dbbba72

Browse files
authored
Merge pull request #2 from srdante/main
Rebase from main
2 parents 6dadc78 + 6f0e84c commit dbbba72

File tree

11 files changed

+197
-44
lines changed

11 files changed

+197
-44
lines changed

.devcontainer/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ FROM mcr.microsoft.com/vscode/devcontainers/php:0-${VARIANT}
88
ARG NODE_VERSION="none"
99
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
1010

11+
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
12+
RUN chmod +x /usr/local/bin/install-php-extensions && \
13+
install-php-extensions gd xdebug
14+
15+
RUN install-php-extensions pdo_mysql
16+
1117
# [Optional] Uncomment this section to install additional OS packages.
1218
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
1319
# && apt-get -y install --no-install-recommends <your-package-list-here>

.github/workflows/tests.yml

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ on:
66
- main
77

88
pull_request:
9-
types: [ opened, synchronize, reopened ]
9+
types: [opened, synchronize, reopened]
1010

1111
schedule:
12-
- cron: '0 0 * * *'
12+
- cron: "0 0 * * *"
1313

1414
jobs:
1515
test:
@@ -18,9 +18,9 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
php: [ 7.3, 7.4, 8.0, 8.1 ]
22-
laravel: [ 8.*, 9.* ]
23-
dependency-version: [ prefer-lowest, prefer-stable ]
21+
php: [7.3, 7.4, 8.0, 8.1]
22+
laravel: [8.*, 9.*]
23+
dependency-version: [prefer-lowest, prefer-stable]
2424
include:
2525
- laravel: 8.*
2626
testbench: 6.*
@@ -45,14 +45,15 @@ jobs:
4545

4646
services:
4747
singlestore:
48-
# check for new versions at https://hub.docker.com/r/singlestore/cluster-in-a-box/tags
49-
image: singlestore/cluster-in-a-box:alma-7.8.6-4f052c9b50-4.0.7-1.13.8
48+
# check for new versions at https://github.com/singlestore-labs/singlestoredb-dev-image/pkgs/container/singlestoredb-dev/versions
49+
image: ghcr.io/singlestore-labs/singlestoredb-dev:0.0.8
5050
ports:
5151
- "3306:3306"
5252
env:
53-
LICENSE_KEY: ${{ secrets.SINGLESTORE_LICENSE }}
53+
# this license key is only authorized for use in SingleStore laravel tests and is heavily restricted
54+
# if you want a free SingleStore license for your own use please visit https://www.singlestore.com/cloud-trial/
55+
SINGLESTORE_LICENSE: BGIxODZiYTg1YWUxYjRlODRhYzRjMGFmYTA1OTkxYzgyAAAAAAAAAAABAAAAAAAAACgwNQIZANx4NIXJ7CWvKYYb3wIyRXxBY7fdAnLeSwIYLy2Q0jA124GAkl04yuGrD59Zpv85DVYXAA==
5456
ROOT_PASSWORD: "test"
55-
START_AFTER_INIT: "Y"
5657

5758
steps:
5859
- name: Checkout code
@@ -80,24 +81,12 @@ jobs:
8081
- name: Execute Unit Tests
8182
run: vendor/bin/phpunit
8283

83-
- name: Wait for SingleStore to start
84-
run: |
85-
attempts=0
86-
until mysql -h 127.0.0.1 -u root -ptest -e "select 1"; do
87-
echo "waiting for mysql..."
88-
((attempts=attempts+1))
89-
if [[ $attempts -gt 5 ]]; then
90-
docker logs $(docker ps -ql)
91-
fi
92-
sleep 1
93-
done
94-
9584
- name: SingleStore Container Logs
9685
run: docker logs $(docker ps -ql)
9786

9887
- name: Create Test Database
9988
run: |
100-
mysql -h 127.0.0.1 -u root -ptest -e "create database test"
89+
mysql -h 127.0.0.1 -u root -ptest -e "create database test"
10190
10291
- name: Execute Integration Tests
10392
run: vendor/bin/phpunit

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
# SingleStore Driver for Laravel
1+
# SingleStore Driver for Laravel <!-- omit in toc -->
2+
3+
[![Latest Stable Version](http://poser.pugx.org/singlestoredb/singlestoredb-laravel/v)](https://packagist.org/packages/singlestoredb/singlestoredb-laravel) [![Total Downloads](http://poser.pugx.org/singlestoredb/singlestoredb-laravel/downloads)](https://packagist.org/packages/singlestoredb/singlestoredb-laravel) [![License](http://poser.pugx.org/singlestoredb/singlestoredb-laravel/license)](https://packagist.org/packages/singlestoredb/singlestoredb-laravel) [![PHP Version Require](http://poser.pugx.org/singlestoredb/singlestoredb-laravel/require/php)](https://packagist.org/packages/singlestoredb/singlestoredb-laravel) [![Github Actions status image](https://github.com/singlestore-labs/singlestoredb-laravel-driver/actions/workflows/tests.yml/badge.svg)](https://github.com/singlestore-labs/singlestoredb-laravel-driver/actions)
24

35
This repository contains a SingleStore Driver for Laravel.
46

7+
This package is currently in a pre-release beta, please use with caution and open any issues that you run into.
8+
9+
- [Install](#install)
10+
- [Usage](#usage)
11+
- [Issues connecting to SingleStore Managed Service](#issues-connecting-to-singlestore-managed-service)
12+
- [PHP Versions before 8.1](#php-versions-before-81)
13+
- [Migrations](#migrations)
14+
- [Rowstore Tables](#rowstore-tables)
15+
- [Reference Tables](#reference-tables)
16+
- [Global Temporary Tables](#global-temporary-tables)
17+
- [Sparse Columns](#sparse-columns)
18+
- [Sparse Tables](#sparse-tables)
19+
- [Shard Keys](#shard-keys)
20+
- [Sort Keys](#sort-keys)
21+
- [Unique Keys](#unique-keys)
22+
- [Series Timestamps](#series-timestamps)
23+
- [Computed Columns](#computed-columns)
24+
- [Testing](#testing)
25+
- [License](#license)
26+
- [Resources](#resources)
27+
- [User agreement](#user-agreement)
28+
529
## Install
630

731
You can install the package via composer:
@@ -10,6 +34,8 @@ You can install the package via composer:
1034
composer require singlestoredb/singlestoredb-laravel
1135
```
1236

37+
**This package requires pdo_mysql** to be installed. If you aren't sure check to see if `pdo_mysql` is listed when you run `php -i`.
38+
1339
## Usage
1440

1541
To enable the driver, head to your `config/database.php` file and create a new entry for SingleStore in your `connections`, and update your `default` to point to that new connection:
@@ -55,6 +81,20 @@ In case you want to store failed jobs in SingleStore, then make sure you also se
5581
],
5682
```
5783

84+
## Issues connecting to SingleStore Managed Service
85+
86+
If you are encountering issues connecting to the SingleStore Managed Service, it may be due to your environment not being able to verify the SSL certificate used to secure connections. You can fix this by downloading and manually specifying the SingleStore certificate file.
87+
88+
* [Download the file here](https://portal.singlestore.com/static/ca/singlestore_bundle.pem)
89+
* In the Laravel SingleStore connection configuration, point the variable `PDO::MYSQL_ATTR_SSL_CA` at `singlestore_bundle.pem`:
90+
91+
```php
92+
'options' => extension_loaded('pdo_mysql') ? array_filter([
93+
PDO::MYSQL_ATTR_SSL_CA => 'path/to/singlestore_bundle.pem',
94+
PDO::ATTR_EMULATE_PREPARES => true,
95+
]) : [],
96+
```
97+
5898
## PHP Versions before 8.1
5999

60100
In PHP versions before 8.1, the flag `PDO::ATTR_EMULATE_PREPARES` results in a bug by which all attributes returned by MySQL (and
@@ -192,7 +232,42 @@ Schema::create('table', function (Blueprint $table) {
192232

193233
$table->sortKey(['f_name', 'l_name']);
194234
});
235+
```
236+
237+
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.
238+
239+
```php
240+
Schema::create('table', function (Blueprint $table) {
241+
$table->string('name');
242+
243+
$table->sortKey('name', 'desc');
244+
});
245+
246+
Schema::create('table', function (Blueprint $table) {
247+
$table->string('name')->sortKey('desc');
248+
});
249+
```
250+
251+
### Unique Keys
252+
253+
You can add an `unique key` to your tables using the standalone `unique` method, or fluently by appending `unique` to the column definition.
254+
255+
> **Note**
256+
> 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.
257+
258+
```php
259+
Schema::create('table', function (Blueprint $table) {
260+
$table->string('key');
261+
$table->string('val');
262+
263+
$table->shardKey('key');
264+
$table->unique(['key', 'val']);
265+
});
195266

267+
Schema::create('table', function (Blueprint $table) {
268+
$table->reference();
269+
$table->string('name')->unique();
270+
});
196271
```
197272

198273
### Series Timestamps

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
45
*/

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
}
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)