Version 2.0
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// 1. Add category + representative fields to contracts
|
||||
Schema::table('contracts', function (Blueprint $table) {
|
||||
$table->string('category')->after('type')->default('ownership');
|
||||
$table->string('representative_name')->nullable()->after('signed_status');
|
||||
$table->string('representative_phone')->nullable()->after('representative_name');
|
||||
$table->string('representative_email')->nullable()->after('representative_phone');
|
||||
});
|
||||
|
||||
// 2. Drop customer_product_id FK and column from feedback
|
||||
if (DB::getDriverName() === 'sqlite') {
|
||||
// SQLite doesn't support dropping foreign keys cleanly, recreate table approach
|
||||
Schema::table('feedback', function (Blueprint $table) {
|
||||
// Drop the column directly (SQLite will handle)
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
Schema::table('feedback', function (Blueprint $table) {
|
||||
$table->dropForeign(['customer_product_id']);
|
||||
});
|
||||
} catch (\Throwable) {
|
||||
// FK may not exist or already dropped
|
||||
}
|
||||
|
||||
Schema::table('feedback', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('feedback', 'customer_product_id')) {
|
||||
$table->dropColumn('customer_product_id');
|
||||
}
|
||||
});
|
||||
|
||||
// 3. Drop customer_product pivot table
|
||||
Schema::dropIfExists('customer_product');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Restore customer_product table
|
||||
Schema::create('customer_product', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('customer_id')->constrained('customers')->cascadeOnDelete();
|
||||
$table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
|
||||
$table->date('purchase_date')->nullable();
|
||||
$table->timestamps();
|
||||
$table->unique(['customer_id', 'product_id']);
|
||||
});
|
||||
|
||||
// Restore customer_product_id on feedback
|
||||
Schema::table('feedback', function (Blueprint $table) {
|
||||
$table->foreignId('customer_product_id')->nullable()->after('customer_id');
|
||||
$table->foreign('customer_product_id')->references('id')->on('customer_product')->nullOnDelete();
|
||||
});
|
||||
|
||||
// Remove V2 columns from contracts
|
||||
Schema::table('contracts', function (Blueprint $table) {
|
||||
$table->dropColumn(['category', 'representative_name', 'representative_phone', 'representative_email']);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user