dootask/database/migrations/2022_07_15_165114_add_files_pshare.php
2023-03-24 09:08:53 +08:00

47 lines
1.2 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddFilesPshare extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$isAdd = false;
Schema::table('files', function (Blueprint $table) use (&$isAdd) {
if (!Schema::hasColumn('files', 'pshare')) {
$isAdd = true;
$table->bigInteger('pshare')->nullable()->default(0)->after('share')->comment('所属分享ID');
}
});
if ($isAdd) {
\App\Models\File::whereShare(1)->chunkById(100, function ($lists) {
/** @var \App\Models\File $item */
foreach ($lists as $item) {
\App\Models\File::where("pids", "like", "%,{$item->id},%")->update(['pshare' => $item->id]);
$item->pshare = $item->id;
$item->save();
}
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('files', function (Blueprint $table) {
$table->dropColumn("pshare");
});
}
}