Skip to content

Commit

Permalink
fix migration for Laravel > 9
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Apr 8, 2024
1 parent 4a81aff commit 3ed4c4d
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions updates/v2.0.1/rename_indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,30 @@ public function down()

public function updateIndexNames($from, $to, $table)
{
$sm = Schema::getConnection()->getDoctrineSchemaManager();
Schema::table($table, function ($blueprint) use ($from, $to) {
foreach ($this->getIndexes($blueprint) as $index) {
if (is_object($index) ? $index->isPrimary() : $index['primary']) {
continue;
}

$table = $sm->listTableDetails($table);
$old = is_object($index) ? $index->getName() : $index['name'];
$new = str_replace($from, $to, $old);

foreach ($table->getIndexes() as $index) {
if ($index->isPrimary()) {
continue;
$blueprint->renameIndex($old, $new);
}
});
}

$old = $index->getName();
$new = str_replace($from, $to, $old);

$table->renameIndex($old, $new);
public function getIndexes($blueprint)
{
$connection = Schema::getConnection();
$table = $blueprint->getTable();

if (method_exists($connection, 'getDoctrineSchemaManager')) {
$sm = $connection->getDoctrineSchemaManager();
return $sm->listTableDetails($table)->getIndexes();
} else {
return $connection->getSchemaBuilder()->getIndexes($table);
}
}
}

0 comments on commit 3ed4c4d

Please sign in to comment.