Skip to content

Commit 662ac36

Browse files
Merge pull request #20 from singlestore-labs/af-fix-sparse
Fix sparse modifier positioning
2 parents 077a45e + 8983530 commit 662ac36

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/Schema/Grammar.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class Grammar extends MySqlGrammar
1919
{
2020
use CompilesKeys, ModifiesColumns;
2121

22+
public function __construct()
23+
{
24+
// Before anything kicks off, we need to add the SingleStore modifiers
25+
// so that they'll get used while the columns are all compiling.
26+
$this->addSingleStoreModifiers();
27+
}
28+
2229
/**
2330
* Create the column definition for a spatial Geography type.
2431
*
@@ -54,10 +61,6 @@ public function typePoint(Fluent $column)
5461
*/
5562
protected function compileCreateTable($blueprint, $command, $connection)
5663
{
57-
// Before anything kicks off, we need to add the SingleStore modifiers
58-
// so that they'll get used while the columns are all compiling.
59-
$this->addSingleStoreModifiers();
60-
6164
// We want to do as little as possible ourselves, so we rely on the parent
6265
// to compile everything and then potentially sneak some modifiers in.
6366
return $this->insertCreateTableModifiers(

src/Schema/Grammar/ModifiesColumns.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,24 @@ trait ModifiesColumns
1919
* @var string[]
2020
*/
2121
protected $singleStoreModifiers = [
22-
'Sparse', 'Option', 'SeriesTimestamp',
22+
'Sparse', 'SeriesTimestamp', 'Option',
2323
];
2424

2525
protected function addSingleStoreModifiers()
2626
{
2727
if (! $this->singleStoreModifiersAdded) {
28-
$this->modifiers = array_merge($this->modifiers, $this->singleStoreModifiers);
28+
// We need to insert all of our modifiers before the "after" modifier,
29+
// otherwise things like "sparse" will come after "after", which is
30+
// invalid SQL. So we find the position of the "after" modifier in
31+
// the parent, and then slot our modifiers in before it.
32+
$index = array_search('After', $this->modifiers);
33+
34+
$this->modifiers = array_merge(
35+
array_slice($this->modifiers, 0, $index),
36+
$this->singleStoreModifiers,
37+
array_slice($this->modifiers, $index)
38+
);
39+
2940
$this->singleStoreModifiersAdded = true;
3041
}
3142
}

tests/Hybrid/CreateTable/SparseModifiersTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ public function sparse_table()
4444
'create rowstore table `test` (`name` varchar(255) not null) compression = sparse'
4545
);
4646
}
47+
48+
/** @test */
49+
public function sparse_with_after()
50+
{
51+
// See https://github.com/singlestore-labs/singlestoredb-laravel-driver/issues/18
52+
$blueprint = new Blueprint('test');
53+
54+
$blueprint->string('two_factor_secret')
55+
->after('password')
56+
->nullable()
57+
->sparse();
58+
59+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
60+
61+
$this->assertEquals(
62+
'alter table `test` add `two_factor_secret` varchar(255) null sparse after `password`',
63+
$statements[0]
64+
);
65+
}
4766
}

tests/Hybrid/Json/JsonWhereTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function where_null()
111111
$query = DB::table('test')->whereNull('data->value1');
112112

113113
$this->assertEquals(
114-
// @TODO check docs
114+
// @TODO check docs
115115
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') is null OR json_type(JSON_EXTRACT_JSON(data, 'value1')) = 'NULL')",
116116
$query->toSql()
117117
);
@@ -123,7 +123,7 @@ public function where_not_null()
123123
$query = DB::table('test')->whereNotNull('data->value1');
124124

125125
$this->assertEquals(
126-
// @TODO check docs
126+
// @TODO check docs
127127
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') is not null AND json_type(JSON_EXTRACT_JSON(data, 'value1')) != 'NULL')",
128128
$query->toSql()
129129
);

0 commit comments

Comments
 (0)