Skip to content

Commit 090b1aa

Browse files
Added support of the Laravel 10
Summary: Changed the inlining of the indexes. The previous implementation assumed that the number of commands equals the number of generated statements but this is not always true.
1 parent ef9566a commit 090b1aa

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Update VARIANT to pick a PHP version: 8, 8.1, 8.0, 7, 7.4
99
// Append -bullseye or -buster to pin to an OS version.
1010
// Use -bullseye variants on local on arm64/Apple Silicon.
11-
"VARIANT": "8"
11+
"VARIANT": "8.1"
1212
}
1313
},
1414

.github/workflows/tests.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
php: [7.3, 7.4, 8.0, 8.1]
22-
laravel: [8.*, 9.*]
22+
laravel: [8.*, 9.*, 10.*]
2323
dependency-version: [prefer-lowest, prefer-stable]
2424
include:
2525
- laravel: 8.*
@@ -28,6 +28,9 @@ jobs:
2828
- laravel: 9.*
2929
testbench: 7.*
3030

31+
- laravel: 10.*
32+
testbench: 8.*
33+
3134
exclude:
3235
# Laravel 8.0 has type incompatibilities with PHP 8.1.
3336
- laravel: 8.*
@@ -41,6 +44,16 @@ jobs:
4144
- laravel: 9.*
4245
php: 7.4
4346

47+
# Laravel 10 requires PHP 8.1
48+
- laravel: 10.*
49+
php: 7.3
50+
51+
- laravel: 10.*
52+
php: 7.4
53+
54+
- laravel: 10.*
55+
php: 8.0
56+
4457
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
4558

4659
services:

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
],
2626
"require": {
2727
"php": "^7.3|^8.0",
28-
"illuminate/container": "^8.0|^9.0",
29-
"illuminate/database": "^8.0|^9.0",
30-
"illuminate/events": "^8.0|^9.0",
31-
"illuminate/support": "^8.0|^9.0"
28+
"illuminate/container": "^8.0|^9.0|^10.0",
29+
"illuminate/database": "^8.0|^9.0|^10.0",
30+
"illuminate/events": "^8.0|^9.0|^10.0",
31+
"illuminate/support": "^8.0|^9.0|^10.0"
3232
},
3333
"require-dev": {
3434
"mockery/mockery": "^1.5",

src/Schema/Blueprint.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ public function geographyPoint($column)
2727
return $this->point($column);
2828
}
2929

30-
public function toSql(Connection $connection, Grammar $grammar)
31-
{
32-
$statements = parent::toSql($connection, $grammar);
33-
34-
return $this->creating() ? $this->inlineCreateIndexStatements($statements) : $statements;
35-
}
36-
3730
/**
3831
* Execute the blueprint against the database.
3932
*

src/Schema/Blueprint/InlinesIndexes.php

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace SingleStore\Laravel\Schema\Blueprint;
44

5+
use Illuminate\Database\Connection;
56
use Illuminate\Database\Schema\Grammars\Grammar;
7+
use Illuminate\Foundation\Application;
68
use Illuminate\Support\Arr;
79
use SingleStore\Laravel\Schema\Blueprint as SingleStoreBlueprint;
810

@@ -32,34 +34,27 @@ trait InlinesIndexes
3234
'spatialIndex',
3335
];
3436

35-
/**
36-
* The keys of the commands that are indexes
37-
*
38-
* @var int[]
39-
*/
40-
protected $indexCommandKeys = [];
41-
4237
/**
4338
* Given a set of statements from the `toSQL` method, inline all
4439
* of the indexes into the CREATE TABLE statement.
4540
*
4641
* @param array $statements
4742
* @return array
4843
*/
49-
protected function inlineCreateIndexStatements($statements)
44+
protected function inlineCreateIndexStatements($statements, $indexStatementKeys)
5045
{
5146
// In the `addImpliedCommands` method we gathered up the keys of all the commands
5247
// that are index commands. Now that we're ready to compile the SQL we'll pull
5348
// all those statements out to sneak them into the CREATE TABLE statement.
54-
$indexStatements = Arr::only($statements, $this->indexCommandKeys);
49+
$indexStatements = Arr::only($statements, $indexStatementKeys);
5550

5651
// Since we're putting the index statements inside the CREATE TABLE statement,
5752
// we pull them out of the statement list so that they don't run as ALTERs.
58-
Arr::forget($statements, $this->indexCommandKeys);
53+
Arr::forget($statements, $indexStatementKeys);
5954

6055
$search = SingleStoreBlueprint::INDEX_PLACEHOLDER;
6156

62-
if (! $indexStatements) {
57+
if (!$indexStatements) {
6358
// If there are no index statements at all, we need to replace the preceding comma as well.
6459
$search = ", $search";
6560
}
@@ -72,37 +67,18 @@ protected function inlineCreateIndexStatements($statements)
7267
}
7368

7469
/**
75-
* Get all of the index commands out of the blueprint's command queue.
70+
* Check if the command is index.
7671
*
7772
* @return \Illuminate\Support\Collection
7873
*/
79-
protected function indexCommands()
74+
protected function isIndexCommand($command)
8075
{
81-
return $this->commandsNamed(array_merge(
76+
return in_array($command->name, array_merge(
8277
$this->singleStoreIndexes,
8378
$this->mysqlIndexes
8479
));
8580
}
8681

87-
/**
88-
* @param Grammar $grammar
89-
* @return void
90-
*/
91-
protected function addImpliedCommands(Grammar $grammar)
92-
{
93-
parent::addImpliedCommands($grammar);
94-
95-
$this->addFluentSingleStoreIndexes();
96-
97-
if ($this->creating()) {
98-
// We have to pull the keys for the indexes during this method because once
99-
// compiled, the primary key's `name` attribute is set to null, meaning
100-
// that we can no longer tell what type of key it is. By hooking into
101-
// the `addImpliedCommands` method, we access it before compilation.
102-
$this->indexCommandKeys = $this->indexCommands()->keys()->toArray();
103-
}
104-
}
105-
10682
/**
10783
* @return void
10884
*/
@@ -132,4 +108,33 @@ protected function addFluentSingleStoreIndexes()
132108
}
133109
}
134110
}
111+
112+
public function toSql(Connection $connection, Grammar $grammar)
113+
{
114+
if (version_compare(Application::VERSION, '10.0.0', '>=')) {
115+
$this->addImpliedCommands($connection, $grammar);
116+
} else {
117+
$this->addImpliedCommands($grammar);
118+
}
119+
$this->addFluentSingleStoreIndexes();
120+
121+
$statements = [];
122+
$indexStatementKeys = [];
123+
124+
foreach ($this->commands as $command) {
125+
$method = 'compile' . ucfirst($command->name);
126+
$isIndex = $this->isIndexCommand($command);
127+
128+
if (method_exists($grammar, $method) || $grammar::hasMacro($method)) {
129+
if (!is_null($sql = $grammar->$method($this, $command, $connection))) {
130+
$statements = array_merge($statements, (array) $sql);
131+
if ($isIndex) {
132+
array_push($indexStatementKeys, count($statements) - 1);
133+
}
134+
}
135+
}
136+
}
137+
138+
return $this->creating() ? $this->inlineCreateIndexStatements($statements, $indexStatementKeys) : $statements;
139+
}
135140
}

0 commit comments

Comments
 (0)