Add filter duplicated documents migration and set unique on uuid
This commit is contained in:
parent
3a9d671373
commit
dddd3ba356
1 changed files with 54 additions and 0 deletions
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class FilterDuplicatedDocumentsAndSetUnique extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
DB::table('documents')->get()->groupBy('uuid')
|
||||
->each(
|
||||
function (Collection $groupedDocuments) {
|
||||
$remaining = $groupedDocuments->count();
|
||||
foreach ($groupedDocuments as $document) {
|
||||
if ($remaining > 1) {
|
||||
try {
|
||||
DB::table('documents')->where(
|
||||
'id',
|
||||
$document->id
|
||||
)->delete();
|
||||
$remaining--;
|
||||
} catch (QueryException $e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Schema::table('documents', function (Blueprint $table) {
|
||||
$table->uuid('uuid')->comment('Document uuid')
|
||||
->unique()->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue