feat(tenant): separate admin and storefront website types
This commit is contained in:
132
database/migrations/2026_09_17_000000_split_website_types.php
Normal file
132
database/migrations/2026_09_17_000000_split_website_types.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?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
|
||||
{
|
||||
Schema::create('storefront_website_types', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('codigo')->unique();
|
||||
$table->string('nombre');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
DB::table('website_type')->orderBy('id')->chunk(100, function ($types): void {
|
||||
foreach ($types as $type) {
|
||||
DB::table('storefront_website_types')->insert([
|
||||
'id' => $type->id,
|
||||
'codigo' => $type->codigo,
|
||||
'nombre' => $type->nombre,
|
||||
'created_at' => $type->created_at,
|
||||
'updated_at' => $type->updated_at,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->string('storefront_website_type_code')->nullable();
|
||||
});
|
||||
|
||||
DB::table('tenants')->orderBy('id')->chunk(100, function ($tenants): void {
|
||||
foreach ($tenants as $tenant) {
|
||||
$type = $tenant->website_type_code
|
||||
? DB::table('website_type')->where('codigo', $tenant->website_type_code)->first()
|
||||
: null;
|
||||
|
||||
DB::table('tenants')->where('id', $tenant->id)->update([
|
||||
'storefront_website_type_code' => $tenant->website_type_code,
|
||||
'site_title' => $tenant->site_title ?? $type?->site_title,
|
||||
'favicon_id' => $tenant->favicon_id ?? $type?->favicon_id,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
if (DB::getDriverName() !== 'sqlite') {
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->dropForeign(['website_type_code']));
|
||||
Schema::table('website_type_extras', fn (Blueprint $table) => $table->dropForeign(['website_type_code']));
|
||||
Schema::table('website_type_integrations', fn (Blueprint $table) => $table->dropForeign(['website_type_code']));
|
||||
}
|
||||
|
||||
Schema::rename('website_type', 'admin_website_types');
|
||||
Schema::rename('website_type_extras', 'storefront_website_type_extras');
|
||||
Schema::rename('website_type_integrations', 'admin_website_type_integrations');
|
||||
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->renameColumn('website_type_code', 'admin_website_type_code'));
|
||||
Schema::table('storefront_website_type_extras', fn (Blueprint $table) => $table->renameColumn('website_type_code', 'storefront_website_type_code'));
|
||||
Schema::table('admin_website_type_integrations', fn (Blueprint $table) => $table->renameColumn('website_type_code', 'admin_website_type_code'));
|
||||
|
||||
if (DB::getDriverName() === 'sqlite') {
|
||||
Schema::disableForeignKeyConstraints();
|
||||
Schema::create('storefront_website_type_extras_rebuilt', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('storefront_website_type_code');
|
||||
$table->foreign('storefront_website_type_code')->references('codigo')->on('storefront_website_types')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->string('codigo')->nullable();
|
||||
$table->string('nombre');
|
||||
$table->text('descripcion');
|
||||
$table->boolean('is_required')->default(false);
|
||||
$table->json('config_schema');
|
||||
$table->timestamps();
|
||||
$table->unique(['storefront_website_type_code', 'codigo']);
|
||||
});
|
||||
DB::table('storefront_website_type_extras')->orderBy('id')->chunk(100, function ($extras): void {
|
||||
foreach ($extras as $extra) {
|
||||
DB::table('storefront_website_type_extras_rebuilt')->insert((array) $extra);
|
||||
}
|
||||
});
|
||||
Schema::drop('storefront_website_type_extras');
|
||||
Schema::rename('storefront_website_type_extras_rebuilt', 'storefront_website_type_extras');
|
||||
Schema::enableForeignKeyConstraints();
|
||||
}
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
if (DB::getDriverName() !== 'sqlite') {
|
||||
$table->foreign('admin_website_type_code')->references('codigo')->on('admin_website_types')->cascadeOnUpdate()->nullOnDelete();
|
||||
}
|
||||
$table->foreign('storefront_website_type_code')->references('codigo')->on('storefront_website_types')->cascadeOnUpdate()->nullOnDelete();
|
||||
});
|
||||
if (DB::getDriverName() !== 'sqlite') {
|
||||
Schema::table('storefront_website_type_extras', function (Blueprint $table): void {
|
||||
$table->foreign('storefront_website_type_code')->references('codigo')->on('storefront_website_types')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
});
|
||||
Schema::table('admin_website_type_integrations', function (Blueprint $table): void {
|
||||
$table->foreign('admin_website_type_code')->references('codigo')->on('admin_website_types')->cascadeOnDelete();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (DB::getDriverName() === 'sqlite') {
|
||||
throw new RuntimeException('SQLite rollback is unsupported for this foreign-key refactor. Recreate the test database instead.');
|
||||
}
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->dropForeign(['admin_website_type_code']);
|
||||
$table->dropForeign(['storefront_website_type_code']);
|
||||
});
|
||||
Schema::table('storefront_website_type_extras', fn (Blueprint $table) => $table->dropForeign(['storefront_website_type_code']));
|
||||
Schema::table('admin_website_type_integrations', fn (Blueprint $table) => $table->dropForeign(['admin_website_type_code']));
|
||||
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->renameColumn('admin_website_type_code', 'website_type_code'));
|
||||
Schema::table('storefront_website_type_extras', fn (Blueprint $table) => $table->renameColumn('storefront_website_type_code', 'website_type_code'));
|
||||
Schema::table('admin_website_type_integrations', fn (Blueprint $table) => $table->renameColumn('admin_website_type_code', 'website_type_code'));
|
||||
|
||||
Schema::rename('admin_website_types', 'website_type');
|
||||
Schema::rename('storefront_website_type_extras', 'website_type_extras');
|
||||
Schema::rename('admin_website_type_integrations', 'website_type_integrations');
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->foreign('website_type_code')->references('codigo')->on('website_type')->cascadeOnUpdate()->nullOnDelete();
|
||||
$table->dropColumn('storefront_website_type_code');
|
||||
});
|
||||
Schema::table('website_type_extras', fn (Blueprint $table) => $table->foreign('website_type_code')->references('codigo')->on('website_type')->cascadeOnUpdate()->cascadeOnDelete());
|
||||
Schema::table('website_type_integrations', fn (Blueprint $table) => $table->foreign('website_type_code')->references('codigo')->on('website_type')->cascadeOnDelete());
|
||||
Schema::dropIfExists('storefront_website_types');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user