-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add company_id column to media tables
- Loading branch information
1 parent
bdb51a2
commit d13e41f
Showing
7 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
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,57 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Update\V21; | ||
|
||
use App\Abstracts\Listeners\Update as Listener; | ||
use App\Events\Install\UpdateFinished as Event; | ||
use App\Models\Common\Media; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class Version2114 extends Listener | ||
{ | ||
const ALIAS = 'core'; | ||
|
||
const VERSION = '2.1.14'; | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param $event | ||
* | ||
* @return void | ||
*/ | ||
public function handle(Event $event) | ||
{ | ||
if ($this->skipThisUpdate($event)) { | ||
return; | ||
} | ||
|
||
$this->updateMediaTables(); | ||
} | ||
|
||
public function updateMediaTables() | ||
{ | ||
$company_ids = []; | ||
|
||
foreach (Media::withTrashed()->cursor() as $media) { | ||
$company_id = null; | ||
|
||
if (preg_match('/\d{4}(\/\d{2}){2}\/(\d+)\//', $media->directory, $matches) && isset($matches[2])) { // 2021/04/09/34235/invoices | ||
$company_id = $matches[2]; | ||
} elseif (preg_match('/^(\d+)\//', $media->directory, $matches) && isset($matches[1])) { // 34235/invoices | ||
$company_id = $matches[1]; | ||
} | ||
|
||
if (null === $company_id) { | ||
continue; | ||
} | ||
|
||
$company_ids[$company_id][] = $media->id; | ||
} | ||
|
||
foreach ($company_ids as $company_id => $media_ids) { | ||
DB::table('media')->whereIn('id', $media_ids)->update(['company_id' => $company_id]); | ||
DB::table('mediables')->whereIn('media_id', $media_ids)->update(['company_id' => $company_id]); | ||
} | ||
} | ||
} |
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
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
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
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
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
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,44 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CoreV2114 extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('media', function (Blueprint $table) { | ||
$table->unsignedInteger('company_id')->after('id'); | ||
|
||
$table->index('company_id'); | ||
}); | ||
|
||
Schema::table('mediables', function (Blueprint $table) { | ||
$table->unsignedInteger('company_id')->after('media_id'); | ||
|
||
$table->index('company_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('media', function (Blueprint $table) { | ||
$table->dropColumn('company_id'); | ||
}); | ||
|
||
Schema::table('mediables', function (Blueprint $table) { | ||
$table->dropColumn('company_id'); | ||
}); | ||
} | ||
} |