Refactor Prop model to use enum for data types; remove PropDataType model and update related requests and migrations

This commit is contained in:
2026-06-22 13:13:22 -03:00
parent 7075704dad
commit 55694b2048
12 changed files with 454 additions and 70 deletions

View File

@@ -1,29 +0,0 @@
<?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('props_data_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('props_data_types');
}
};

View File

@@ -17,7 +17,7 @@ return new class extends Migration
$table->string('codigo')->unique();
$table->string('nombre');
$table->boolean('is_required')->default(false);
$table->foreignId('data_type_id')->constrained('props_data_types')->cascadeOnUpdate()->restrictOnDelete();
$table->string('data_type');
$table->timestamps();
$table->index('propable_type');

View File

@@ -0,0 +1,106 @@
<?php
use App\Domains\Prop\Support\PropDataType;
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
{
/**
* Run the migrations.
*/
public function up(): void
{
if (! Schema::hasTable('props')) {
return;
}
if (Schema::hasColumn('props', 'data_type_id')) {
Schema::table('props', function (Blueprint $table) {
$table->string('data_type')->nullable()->after('is_required');
});
if (Schema::hasTable('props_data_types')) {
$typeMap = DB::table('props_data_types')
->pluck('code', 'id')
->map(static fn (mixed $code): string => in_array($code, PropDataType::values(), true) ? (string) $code : PropDataType::String->value)
->all();
DB::table('props')
->select(['id', 'data_type_id'])
->orderBy('id')
->get()
->each(function (object $prop) use ($typeMap): void {
DB::table('props')
->where('id', $prop->id)
->update([
'data_type' => $typeMap[$prop->data_type_id] ?? PropDataType::String->value,
]);
});
} else {
DB::table('props')->update([
'data_type' => PropDataType::String->value,
]);
}
Schema::table('props', function (Blueprint $table) {
$table->dropForeign(['data_type_id']);
$table->dropColumn('data_type_id');
});
Schema::table('props', function (Blueprint $table) {
$table->string('data_type')->nullable(false)->change();
});
}
if (Schema::hasTable('props_data_types')) {
Schema::drop('props_data_types');
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
if (! Schema::hasTable('props_data_types')) {
Schema::create('props_data_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code')->unique();
$table->timestamps();
});
DB::table('props_data_types')->insert([
[
'id' => 1,
'name' => 'String',
'code' => PropDataType::String->value,
'created_at' => now(),
'updated_at' => now(),
],
]);
}
if (Schema::hasTable('props') && Schema::hasColumn('props', 'data_type') && ! Schema::hasColumn('props', 'data_type_id')) {
Schema::table('props', function (Blueprint $table) {
$table->foreignId('data_type_id')->nullable()->after('is_required')->constrained('props_data_types')->cascadeOnUpdate()->restrictOnDelete();
});
$stringTypeId = DB::table('props_data_types')
->where('code', PropDataType::String->value)
->value('id');
DB::table('props')->update([
'data_type_id' => $stringTypeId,
]);
Schema::table('props', function (Blueprint $table) {
$table->foreignId('data_type_id')->nullable(false)->change();
$table->dropColumn('data_type');
});
}
}
};