feat: Add migrate:all command to orchestrate data migrations and refactor existing commands for enum-based data mapping.

This commit is contained in:
Yoga Pangestu 2026-03-13 13:23:12 +07:00
parent 48f0c277d1
commit cb41a0b5d3
7 changed files with 109 additions and 18 deletions

View File

@ -0,0 +1,59 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MigrateAllCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:all';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run all migration commands in correct order';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('🚀 Starting global data migration process...');
$startTime = microtime(true);
$commands = [
'migrate:user' => '👥 Migrating users...',
'migrate:department' => '🏢 Migrating departments...',
'migrate:location' => '📍 Migrating locations & sub-locations...',
'migrate:classification' => '🏷️ Migrating classifications & sub-classifications...',
'migrate:theme' => '🎨 Migrating themes...',
'migrate:media-monitoring' => '🖥️ Migrating media monitoring...',
'migrate:company' => '🏭 Migrating companies & partner media...',
'migrate:journalist' => '📰 Migrating journalists...',
'migrate:news' => '🗞️ Migrating news...',
'migrate:announcement' => '📢 Migrating announcements...',
'migrate:cooperation' => '🤝 Migrating cooperations, proposals & reports...',
'migrate:content-recap' => '📊 Migrating content recaps...',
'migrate:issue-management' => '⚠️ Migrating issue management...',
];
foreach ($commands as $command => $description) {
$this->newLine();
$this->info($description);
$this->call($command);
}
$endTime = microtime(true);
$duration = number_format($endTime - $startTime, 2);
$this->newLine();
$this->info("✅ Global migration completed in {$duration} seconds!");
}
}

View File

@ -2,8 +2,10 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Enums\Channel;
use App\Enums\ContentType; use App\Enums\ContentType;
use App\Enums\IsActive; use App\Enums\IsActive;
use App\Enums\SocialMedia;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -48,7 +50,6 @@ public function handle()
$newId = DB::table('classifications')->insertGetId([ $newId = DB::table('classifications')->insertGetId([
'name' => $legacyClass->name, 'name' => $legacyClass->name,
'is_active' => IsActive::ACTIVE->value, 'is_active' => IsActive::ACTIVE->value,
'sort_order' => 0,
'created_at' => $legacyClass->created_at ?? now(), 'created_at' => $legacyClass->created_at ?? now(),
'updated_at' => $legacyClass->updated_at ?? now(), 'updated_at' => $legacyClass->updated_at ?? now(),
'deleted_at' => $legacyClass->deleted_at, 'deleted_at' => $legacyClass->deleted_at,
@ -91,8 +92,8 @@ public function handle()
'link' => Str::limit($legacy->link, 50, ''), 'link' => Str::limit($legacy->link, 50, ''),
'posting_date' => $legacy->posting_date === '0000-00-00' ? now()->toDateString() : $legacy->posting_date, 'posting_date' => $legacy->posting_date === '0000-00-00' ? now()->toDateString() : $legacy->posting_date,
'type' => $this->mapContentType($legacy->content_type), 'type' => $this->mapContentType($legacy->content_type),
'channel' => Str::limit($legacy->channel, 20, ''), 'channel' => $this->mapChannel($legacy->channel),
'social_media' => Str::limit($legacy->social_media ?? '-', 50, ''), 'social_media' => $this->mapSocialMedia($legacy->social_media),
'created_at' => $legacy->created_at ?? now(), 'created_at' => $legacy->created_at ?? now(),
'updated_at' => $legacy->updated_at ?? now(), 'updated_at' => $legacy->updated_at ?? now(),
'deleted_at' => $legacy->deleted_at, 'deleted_at' => $legacy->deleted_at,
@ -104,7 +105,7 @@ public function handle()
$this->info('Content recap migration completed successfully!'); $this->info('Content recap migration completed successfully!');
} }
private function mapContentType(?string $legacyType): string private function mapContentType(?string $legacyType): int
{ {
return match (strtolower($legacyType ?? '')) { return match (strtolower($legacyType ?? '')) {
'foto' => ContentType::FOTO->value, 'foto' => ContentType::FOTO->value,
@ -113,4 +114,27 @@ private function mapContentType(?string $legacyType): string
default => ContentType::FOTO->value, default => ContentType::FOTO->value,
}; };
} }
private function mapChannel(?string $legacyChannel): int
{
return match (strtolower($legacyChannel ?? '')) {
'website' => Channel::WEBSITE->value,
'tiktok' => Channel::TIKTOK->value,
'youtube' => Channel::YOUTUBE->value,
'facebook' => Channel::FACEBOOK->value,
'instagram' => Channel::INSTAGRAM->value,
'twitter' => Channel::TWITTER->value,
default => Channel::WEBSITE->value,
};
}
private function mapSocialMedia(?string $legacySocial): int
{
return match (strtolower($legacySocial ?? '')) {
'ppid diskominfo purwakarta' => SocialMedia::PPID_DISKOMINFO_PURWAKARTA->value,
'diskominfo purwakarta' => SocialMedia::DISKOMINFO_PURWAKARTA->value,
'purwakarta saber hoaks' => SocialMedia::PURWAKARTA_SABER_HOAKS->value,
default => SocialMedia::DISKOMINFO_PURWAKARTA->value,
};
}
} }

View File

@ -63,7 +63,7 @@ public function handle()
$this->info('Issue management migration completed successfully!'); $this->info('Issue management migration completed successfully!');
} }
private function mapSentiment(string $legacyValue): string private function mapSentiment(string $legacyValue): int
{ {
return match (strtolower($legacyValue)) { return match (strtolower($legacyValue)) {
'positif' => IssueSentiment::POSITIVE->value, 'positif' => IssueSentiment::POSITIVE->value,

View File

@ -2,6 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Enums\Channel;
use App\Enums\IsActive; use App\Enums\IsActive;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -49,7 +50,7 @@ public function handle()
'code' => Str::limit($legacy->code, 10, ''), 'code' => Str::limit($legacy->code, 10, ''),
'media_name' => Str::limit($legacy->media_name, 50, ''), 'media_name' => Str::limit($legacy->media_name, 50, ''),
'title' => $legacy->title, 'title' => $legacy->title,
'channel' => Str::limit($legacy->channel, 20, ''), 'channel' => $this->mapChannel($legacy->channel),
'writter' => Str::limit($legacy->writter, 50, ''), 'writter' => Str::limit($legacy->writter, 50, ''),
'link' => $legacy->link, 'link' => $legacy->link,
'news_page' => Str::limit($legacy->news_page, 20, ''), 'news_page' => Str::limit($legacy->news_page, 20, ''),
@ -74,7 +75,6 @@ public function handle()
['name' => $themeName], ['name' => $themeName],
[ [
'is_active' => IsActive::ACTIVE->value, 'is_active' => IsActive::ACTIVE->value,
'sort_order' => 0,
'created_at' => now(), 'created_at' => now(),
'updated_at' => now(), 'updated_at' => now(),
] ]
@ -97,4 +97,17 @@ public function handle()
$this->info('Media monitoring migration completed successfully!'); $this->info('Media monitoring migration completed successfully!');
} }
private function mapChannel(?string $legacyChannel): int
{
return match (strtolower($legacyChannel ?? '')) {
'website' => Channel::WEBSITE->value,
'tiktok' => Channel::TIKTOK->value,
'youtube' => Channel::YOUTUBE->value,
'facebook' => Channel::FACEBOOK->value,
'instagram' => Channel::INSTAGRAM->value,
'twitter' => Channel::TWITTER->value,
default => Channel::WEBSITE->value,
};
}
} }

View File

@ -52,6 +52,7 @@ public function handle()
// Using DB facade directly to insert ensures the legacy password hash // Using DB facade directly to insert ensures the legacy password hash
// is NOT double-hashed by the model's "hashed" cast. // is NOT double-hashed by the model's "hashed" cast.
$userId = DB::table('users')->insertGetId([ $userId = DB::table('users')->insertGetId([
'id' => $legacy->id,
'name' => Str::limit($legacy->name, 100), 'name' => Str::limit($legacy->name, 100),
'email' => $legacy->email, 'email' => $legacy->email,
'username' => $username, 'username' => $username,

View File

@ -4,6 +4,7 @@
use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
@ -16,7 +17,10 @@ public function run(): void
{ {
$this->call([ $this->call([
ShieldSeeder::class, ShieldSeeder::class,
UserSeeder::class,
]); ]);
Artisan::call('migrate:all', [], $this->command->getOutput());
(new UserSeeder)->run();
} }
} }

View File

@ -23,15 +23,5 @@ public function run(): void
]); ]);
$developer->assignRole(RoleEnum::DEVELOPER->value); $developer->assignRole(RoleEnum::DEVELOPER->value);
$administrator = User::create([
'name' => 'Administrator',
'email' => 'admin@gmail.com',
'username' => 'admin',
'password' => Hash::make(config('auth.password_default')),
'email_verified_at' => now(),
]);
$administrator->assignRole(RoleEnum::ADMINISTRATOR->value);
} }
} }