Skip to content

Commit

Permalink
Merge pull request #106 from plank/fix-postgres-migration
Browse files Browse the repository at this point in the history
Fix index migration on Postgres
  • Loading branch information
frasmage authored May 16, 2024
2 parents b59bd14 + 3981931 commit de76793
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/automated-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: php-${{ matrix.php-versions }}${{ matrix.prefer-lowest }}
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -vvv
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v

finish-coverage:
needs: phpunit
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"guzzlehttp/promises": "^1.4",
"mockery/mockery": "^1.4.2",
"nesbot/carbon" : "^2.62.1",
"php-coveralls/php-coveralls": "^2.5.2"
"php-coveralls/php-coveralls": "^2.7.0"
},
"autoload-dev":{
"psr-4": {
Expand Down
34 changes: 27 additions & 7 deletions migrations/2024_04_14_000000_add_meta_search_columns.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

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

$stringIndexLength = (int)config('metable.stringValueIndexLength', 255);
if ($stringIndexLength > 0 && $driver = $this->detectDriverName()) {
$connection = $this->detectConnectionInUse();
if ($stringIndexLength > 0 && $driver = $connection?->getDriverName()) {
$grammar = $connection->getQueryGrammar();
if (in_array($driver, ['mysql', 'mariadb'])) {
$table->rawIndex(
"`metable_type`, `key`, `value`($stringIndexLength)",
sprintf(
"%s, %s, %s(%d)",
$grammar->wrap('metable_type'),
$grammar->wrap('key'),
$grammar->wrap('value'),
$stringIndexLength
),
'value_string_prefix_index'
);
} elseif (in_array($driver, ['pgsql', 'sqlite'])) {
$table->rawIndex(
"`metable_type`, `key`, SUBSTR(`value`, 1, $stringIndexLength)",
sprintf(
"%s, %s, SUBSTR(%s, 1, %d)",
$grammar->wrap('metable_type'),
$grammar->wrap('key'),
$grammar->wrap('value'),
$stringIndexLength
),
'value_string_prefix_index'
);
}
Expand All @@ -48,7 +63,10 @@ public function down()
Schema::table('meta', function (Blueprint $table) {
$stringIndexLength = (int)config('metable.stringValueIndexLength', 255);
if ($stringIndexLength > 0
&& in_array($this->detectDriverName(), ['mysql', 'mariadb', 'pgsql', 'sqlite'])
&& in_array(
$this->detectConnectionInUse()?->getDriverName(),
['mysql', 'mariadb', 'pgsql', 'sqlite']
)
) {
$table->dropIndex('value_string_prefix_index');
}
Expand All @@ -61,7 +79,7 @@ public function down()
});
}

private function detectDriverName(): ?string
private function detectConnectionInUse(): ?Connection
{
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
$migrator = app('migrator');
Expand All @@ -73,8 +91,10 @@ private function detectDriverName(): ?string
$resolver = DB::getFacadeRoot();
}

return $resolver->connection(
$connection = $resolver->connection(
$this->getConnection() ?? $migrator->getConnection()
)->getDriverName();
);

return $connection instanceof Connection ? $connection : null;
}
};

0 comments on commit de76793

Please sign in to comment.