Changes 0228092025 Laptop
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void
|
||||
{
|
||||
// PostgreSQL: drop NOT NULL constraint on description
|
||||
DB::statement('ALTER TABLE segments ALTER COLUMN description DROP NOT NULL');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Ensure no NULLs before setting NOT NULL
|
||||
DB::statement("UPDATE segments SET description = '' WHERE description IS NULL");
|
||||
DB::statement('ALTER TABLE segments ALTER COLUMN description SET NOT NULL');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('field_jobs', function (Blueprint $table) {
|
||||
$table->foreignId('contract_id')
|
||||
->nullable()
|
||||
->after('user_id')
|
||||
->constrained('contracts')
|
||||
->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('field_jobs', function (Blueprint $table) {
|
||||
$table->dropForeign(['contract_id']);
|
||||
$table->dropColumn('contract_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('contract_configs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(\App\Models\ContractType::class, 'contract_type_id')->constrained('contract_types')->cascadeOnDelete();
|
||||
$table->foreignIdFor(\App\Models\Segment::class, 'initial_segment_id')->constrained('segments')->cascadeOnDelete();
|
||||
$table->boolean('active')->default(true);
|
||||
$table->timestamps();
|
||||
$table->unique('contract_type_id');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('contract_configs');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
// Rename initial_segment_id to segment_id
|
||||
if (Schema::hasColumn('contract_configs', 'initial_segment_id')) {
|
||||
$table->renameColumn('initial_segment_id', 'segment_id');
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
// Add is_initial and drop unique(contract_type_id)
|
||||
$table->boolean('is_initial')->default(false)->after('segment_id');
|
||||
});
|
||||
|
||||
// Drop existing unique index on contract_type_id if present and add composite unique
|
||||
try {
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
$table->dropUnique(['contract_type_id']);
|
||||
});
|
||||
} catch (Throwable $e) {
|
||||
// ignore if index does not exist
|
||||
}
|
||||
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
$table->unique(['contract_type_id', 'segment_id']);
|
||||
});
|
||||
|
||||
// Mark existing rows as initial
|
||||
\DB::table('contract_configs')->update(['is_initial' => true]);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Remove composite unique
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
$table->dropUnique(['contract_type_id', 'segment_id']);
|
||||
});
|
||||
|
||||
// Try to restore unique on contract_type_id
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
$table->unique('contract_type_id');
|
||||
});
|
||||
|
||||
// Drop is_initial
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
$table->dropColumn('is_initial');
|
||||
});
|
||||
|
||||
// Rename segment_id back to initial_segment_id
|
||||
Schema::table('contract_configs', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('contract_configs', 'segment_id')) {
|
||||
$table->renameColumn('segment_id', 'initial_segment_id');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// Resolve duplicates among non-deleted rows by appending the row id to later duplicates
|
||||
$dupes = DB::table('contracts')
|
||||
->select('client_case_id', 'reference', DB::raw('COUNT(*) as cnt'))
|
||||
->whereNull('deleted_at')
|
||||
->whereNotNull('reference')
|
||||
->groupBy('client_case_id', 'reference')
|
||||
->havingRaw('COUNT(*) > 1')
|
||||
->get();
|
||||
|
||||
foreach ($dupes as $d) {
|
||||
$rows = DB::table('contracts')
|
||||
->where('client_case_id', $d->client_case_id)
|
||||
->where('reference', $d->reference)
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('id')
|
||||
->get(['id', 'reference']);
|
||||
|
||||
$keepFirst = true;
|
||||
foreach ($rows as $row) {
|
||||
if ($keepFirst) { $keepFirst = false; continue; }
|
||||
$base = mb_substr($row->reference, 0, 120);
|
||||
$newRef = $base . '-' . $row->id;
|
||||
DB::table('contracts')->where('id', $row->id)->update(['reference' => $newRef]);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a partial unique index (Postgres) for non-deleted rows
|
||||
DB::statement('CREATE UNIQUE INDEX IF NOT EXISTS contracts_client_case_reference_unique ON contracts (client_case_id, reference) WHERE deleted_at IS NULL');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::statement('DROP INDEX IF EXISTS contracts_client_case_reference_unique');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user