Move user suppliers to account suppliers
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('supplier_user', function (Blueprint $table) {
|
||||
$table->string('supplier_ref')->nullable();
|
||||
$table->dropUnique(['id','site_id']);
|
||||
$table->dropUnique(['id','supplier_id']);
|
||||
$table->unique(['user_id','supplier_ref']);
|
||||
});
|
||||
|
||||
DB::update('UPDATE supplier_user set supplier_ref=id');
|
||||
DB::update('ALTER TABLE supplier_user ALTER COLUMN supplier_ref SET NOT NULL');
|
||||
|
||||
Schema::table('supplier_user', function (Blueprint $table) {
|
||||
$table->dropColumn('id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('supplier_user', function (Blueprint $table) {
|
||||
$table->string('id')->nullable();
|
||||
$table->unique(['id','site_id']);
|
||||
$table->unique(['id','supplier_id']);
|
||||
$table->dropUnique(['user_id','supplier_ref']);
|
||||
});
|
||||
|
||||
DB::update('UPDATE supplier_user set id=supplier_ref');
|
||||
|
||||
Schema::table('supplier_user', function (Blueprint $table) {
|
||||
$table->dropColumn('supplier_ref');
|
||||
});
|
||||
}
|
||||
};
|
52
database/migrations/2024_07_23_212746_move_supplier_user.php
Normal file
52
database/migrations/2024_07_23_212746_move_supplier_user.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('account_supplier', function (Blueprint $table) {
|
||||
$table->timestamps();
|
||||
$table->integer('supplier_id')->unsigned();
|
||||
$table->integer('account_id')->unsigned();
|
||||
$table->string('supplier_ref');
|
||||
$table->boolean('active')->default(FALSE);
|
||||
|
||||
$table->unique(['account_id','supplier_id']);
|
||||
$table->unique(['supplier_id','supplier_ref']);
|
||||
|
||||
$table->foreign('account_id')->references('id')->on('accounts');
|
||||
$table->foreign('supplier_id')->references('id')->on('suppliers');
|
||||
});
|
||||
|
||||
foreach (DB::table('supplier_user')->cursor() as $o) {
|
||||
$ao = \App\Models\Account::where('user_id',$o->user_id)->firstOrfail();
|
||||
|
||||
DB::table('account_supplier')
|
||||
->insert([
|
||||
'created_at' => $o->created_at,
|
||||
'updated_at' => $o->updated_at,
|
||||
'supplier_id' => $o->supplier_id,
|
||||
'account_id' => $ao->id,
|
||||
'supplier_ref' => $o->supplier_ref,
|
||||
'active' => $o->active,
|
||||
]);
|
||||
}
|
||||
|
||||
Schema::drop('supplier_user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
abort(500,'Cant go back');
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user