Skip to content

Commit de76793

Browse files
authored
Merge pull request #106 from plank/fix-postgres-migration
Fix index migration on Postgres
2 parents b59bd14 + 3981931 commit de76793

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

.github/workflows/automated-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4444
COVERALLS_PARALLEL: true
4545
COVERALLS_FLAG_NAME: php-${{ matrix.php-versions }}${{ matrix.prefer-lowest }}
46-
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -vvv
46+
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
4747

4848
finish-coverage:
4949
needs: phpunit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"guzzlehttp/promises": "^1.4",
3131
"mockery/mockery": "^1.4.2",
3232
"nesbot/carbon" : "^2.62.1",
33-
"php-coveralls/php-coveralls": "^2.5.2"
33+
"php-coveralls/php-coveralls": "^2.7.0"
3434
},
3535
"autoload-dev":{
3636
"psr-4": {

migrations/2024_04_14_000000_add_meta_search_columns.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Database\Connection;
34
use Illuminate\Database\Migrations\Migration;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Support\Facades\DB;
@@ -22,15 +23,29 @@ public function up()
2223
$table->index(['key', 'metable_type', 'numeric_value']);
2324

2425
$stringIndexLength = (int)config('metable.stringValueIndexLength', 255);
25-
if ($stringIndexLength > 0 && $driver = $this->detectDriverName()) {
26+
$connection = $this->detectConnectionInUse();
27+
if ($stringIndexLength > 0 && $driver = $connection?->getDriverName()) {
28+
$grammar = $connection->getQueryGrammar();
2629
if (in_array($driver, ['mysql', 'mariadb'])) {
2730
$table->rawIndex(
28-
"`metable_type`, `key`, `value`($stringIndexLength)",
31+
sprintf(
32+
"%s, %s, %s(%d)",
33+
$grammar->wrap('metable_type'),
34+
$grammar->wrap('key'),
35+
$grammar->wrap('value'),
36+
$stringIndexLength
37+
),
2938
'value_string_prefix_index'
3039
);
3140
} elseif (in_array($driver, ['pgsql', 'sqlite'])) {
3241
$table->rawIndex(
33-
"`metable_type`, `key`, SUBSTR(`value`, 1, $stringIndexLength)",
42+
sprintf(
43+
"%s, %s, SUBSTR(%s, 1, %d)",
44+
$grammar->wrap('metable_type'),
45+
$grammar->wrap('key'),
46+
$grammar->wrap('value'),
47+
$stringIndexLength
48+
),
3449
'value_string_prefix_index'
3550
);
3651
}
@@ -48,7 +63,10 @@ public function down()
4863
Schema::table('meta', function (Blueprint $table) {
4964
$stringIndexLength = (int)config('metable.stringValueIndexLength', 255);
5065
if ($stringIndexLength > 0
51-
&& in_array($this->detectDriverName(), ['mysql', 'mariadb', 'pgsql', 'sqlite'])
66+
&& in_array(
67+
$this->detectConnectionInUse()?->getDriverName(),
68+
['mysql', 'mariadb', 'pgsql', 'sqlite']
69+
)
5270
) {
5371
$table->dropIndex('value_string_prefix_index');
5472
}
@@ -61,7 +79,7 @@ public function down()
6179
});
6280
}
6381

64-
private function detectDriverName(): ?string
82+
private function detectConnectionInUse(): ?Connection
6583
{
6684
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
6785
$migrator = app('migrator');
@@ -73,8 +91,10 @@ private function detectDriverName(): ?string
7391
$resolver = DB::getFacadeRoot();
7492
}
7593

76-
return $resolver->connection(
94+
$connection = $resolver->connection(
7795
$this->getConnection() ?? $migrator->getConnection()
78-
)->getDriverName();
96+
);
97+
98+
return $connection instanceof Connection ? $connection : null;
7999
}
80100
};

0 commit comments

Comments
 (0)