Skip to content

Commit

Permalink
Fixes 1280 (#1284)
Browse files Browse the repository at this point in the history
* Added migration.

* Fixed missing closing paranthese.
  • Loading branch information
nagmat84 authored Apr 21, 2022
1 parent db0c51f commit a537afa
Showing 1 changed file with 68 additions and 0 deletions.
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]);
}
}

0 comments on commit a537afa

Please sign in to comment.