-
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added migration. * Fixed missing closing paranthese.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
database/migrations/2022_04_18_174417_fix_live_photo_short_path.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
use Doctrine\DBAL\Exception as DBALException; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class FixLivePhotoShortPath extends Migration | ||
{ | ||
private string $driverName; | ||
|
||
/** | ||
* @throws DBALException | ||
*/ | ||
public function __construct() | ||
{ | ||
$connection = Schema::connection(null)->getConnection(); | ||
$this->driverName = $connection->getDriverName(); | ||
} | ||
|
||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function up() | ||
{ | ||
// MySQL misuses the ANSI SQL concatenation operator `||` for | ||
// a logical OR and provides the proprietary `CONCAT` statement | ||
// instead. | ||
$sqlConcatLivePhotoPath = match ($this->driverName) { | ||
'mysql' => DB::raw('CONCAT(\'big/\', live_photo_short_path)'), | ||
'pgsql', 'sqlite' => DB::raw('\'big/\' || live_photo_short_path'), | ||
default => throw new \RuntimeException('Unknown DBMS') | ||
}; | ||
|
||
DB::table('photos') | ||
->whereNotNull('live_photo_short_path') | ||
->where('live_photo_short_path', 'not like', '%/%') | ||
->update(['live_photo_short_path' => $sqlConcatLivePhotoPath]); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function down() | ||
{ | ||
// In contrast to all other programming languages, the first character | ||
// of a string has index 1 (not 0) in SQL. | ||
// We want to remove `'big/'` or `'raw/'` from the string. | ||
$sqlSubstringLivePhotoPath = match ($this->driverName) { | ||
'mysql', 'pgsql' => DB::raw('SUBSTRING(live_photo_short_path FROM 5)'), | ||
'sqlite' => DB::raw('SUBSTR(live_photo_short_path, 5)'), | ||
default => throw new \RuntimeException('Unknown DBMS') | ||
}; | ||
|
||
DB::table('photos') | ||
->whereNotNull('live_photo_short_path') | ||
->where('live_photo_short_path', 'like', '%/%') | ||
->update(['live_photo_short_path' => $sqlSubstringLivePhotoPath]); | ||
} | ||
} |