feat(event): restore event model and migrate tenant event data
This commit is contained in:
124
database/migrations/2026_09_18_000500_restore_events.php
Normal file
124
database/migrations/2026_09_18_000500_restore_events.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?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('events', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('tenant_code');
|
||||
$table->string('title');
|
||||
$table->string('subtitle')->nullable();
|
||||
$table->text('description')->nullable();
|
||||
$table->string('location')->nullable();
|
||||
$table->text('date_text')->nullable();
|
||||
$table->timestamp('published_at')->nullable();
|
||||
$table->timestamps();
|
||||
$table->foreign('tenant_code')->references('codigo')->on('tenants')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->index(['tenant_code', 'published_at']);
|
||||
});
|
||||
|
||||
Schema::table('event_dates', function (Blueprint $table): void {
|
||||
$table->foreignId('event_id')->nullable()->after('tenant_code')->constrained('events')->cascadeOnDelete();
|
||||
});
|
||||
|
||||
Schema::create('event_social_media', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('event_id')->constrained('events')->cascadeOnDelete();
|
||||
$table->string('social_media_code');
|
||||
$table->string('url');
|
||||
$table->unsignedInteger('orden')->default(0);
|
||||
$table->timestamps();
|
||||
$table->foreign('social_media_code')->references('code')->on('social_media')->cascadeOnDelete();
|
||||
$table->unique(['event_id', 'social_media_code']);
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->foreignId('active_event_id')->nullable()->constrained('events')->nullOnDelete();
|
||||
});
|
||||
|
||||
DB::table('tenants')->orderBy('id')->chunk(100, function ($tenants): void {
|
||||
foreach ($tenants as $tenant) {
|
||||
$hasEvent = $tenant->event_title !== null
|
||||
|| DB::table('event_dates')->where('tenant_code', $tenant->codigo)->exists();
|
||||
|
||||
if (! $hasEvent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$eventId = DB::table('events')->insertGetId([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'title' => $tenant->event_title ?? $tenant->nombre,
|
||||
'location' => $tenant->event_location,
|
||||
'date_text' => $tenant->event_date_text,
|
||||
'published_at' => now(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('event_dates')->where('tenant_code', $tenant->codigo)->update(['event_id' => $eventId]);
|
||||
|
||||
DB::table('tenant_social_media')
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->each(function ($socialMedia) use ($eventId): void {
|
||||
DB::table('event_social_media')->insert([
|
||||
'event_id' => $eventId,
|
||||
'social_media_code' => $socialMedia->social_media_code,
|
||||
'url' => $socialMedia->url,
|
||||
'orden' => $socialMedia->orden,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
});
|
||||
|
||||
if ($tenant->storefront_website_type_code === 'onticket') {
|
||||
DB::table('tenants')->where('id', $tenant->id)->update(['active_event_id' => $eventId]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('event_dates', function (Blueprint $table): void {
|
||||
$table->foreignId('event_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->dropColumn(['event_title', 'event_location', 'event_date_text']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->string('event_title')->nullable();
|
||||
$table->string('event_location')->nullable();
|
||||
$table->text('event_date_text')->nullable();
|
||||
});
|
||||
|
||||
DB::table('tenants')->orderBy('id')->chunk(100, function ($tenants): void {
|
||||
foreach ($tenants as $tenant) {
|
||||
$event = DB::table('events')->where('tenant_code', $tenant->codigo)
|
||||
->when($tenant->active_event_id !== null, fn ($query) => $query->orderByRaw('id = ? desc', [$tenant->active_event_id]))
|
||||
->orderBy('id')->first();
|
||||
if ($event !== null) {
|
||||
DB::table('tenants')->where('id', $tenant->id)->update([
|
||||
'event_title' => $event->title,
|
||||
'event_location' => $event->location,
|
||||
'event_date_text' => $event->date_text,
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->dropConstrainedForeignId('active_event_id'));
|
||||
Schema::table('event_dates', fn (Blueprint $table) => $table->dropConstrainedForeignId('event_id'));
|
||||
Schema::dropIfExists('event_social_media');
|
||||
Schema::dropIfExists('events');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user