Skip to content

Commit 28bf407

Browse files
committed
Slot the SingleStore modifiers in in the right spot, not just at the end.
1 parent d6bdbc9 commit 28bf407

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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+
'SeriesTimestamp', 'Sparse', '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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

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

8+
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Schema;
811
use SingleStore\Laravel\Schema\Blueprint;
912
use SingleStore\Laravel\Tests\BaseTest;
1013
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;
@@ -44,4 +47,23 @@ public function sparse_table()
4447
'create rowstore table `test` (`name` varchar(255) not null) compression = sparse'
4548
);
4649
}
50+
51+
/** @test */
52+
public function sparse_with_after()
53+
{
54+
// See https://github.com/singlestore-labs/singlestoredb-laravel-driver/issues/18
55+
$blueprint = new Blueprint('test');
56+
57+
$blueprint->string('two_factor_secret')
58+
->after('password')
59+
->nullable()
60+
->sparse();
61+
62+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
63+
64+
$this->assertEquals(
65+
'alter table `test` add `two_factor_secret` varchar(255) null sparse after `password`',
66+
$statements[0]
67+
);
68+
}
4769
}

0 commit comments

Comments
 (0)