68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/**
|
|
* Menyiapkan kolom tambahan di DB lama untuk menu Portal operator (satu-satunya DDL ke DB `untan`):
|
|
* kode dan warna jenis kegiatan akademik, serta penanda hapus-lunak pesan konsultasi. Aman dijalankan berulang.
|
|
*/
|
|
class PreparePortalCommand extends Command
|
|
{
|
|
protected $signature = 'siakad:siapkan-portal {--force : Tanpa konfirmasi}';
|
|
|
|
protected $description = 'Tambah kolom kode/warna di t_kalender_jenis_kegiatan dan deleted_at di x_message (DB untan)';
|
|
|
|
/** tabel => [kolom => tipe] */
|
|
private const array COLUMNS = [
|
|
't_kalender_jenis_kegiatan' => ['kode' => 'varchar(5)', 'warna' => 'varchar(7)'],
|
|
'x_message' => ['deleted_at' => 'timestamp'],
|
|
];
|
|
|
|
public function handle(): int
|
|
{
|
|
$schema = Schema::connection('untan');
|
|
$missing = [];
|
|
|
|
foreach (self::COLUMNS as $table => $columns) {
|
|
if (! $schema->hasTable($table)) {
|
|
$this->error("Tabel {$table} tidak ada di koneksi untan.");
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
foreach ($columns as $column => $type) {
|
|
if (! $schema->hasColumn($table, $column)) {
|
|
$missing[] = [$table, $column, $type];
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($missing === []) {
|
|
$this->info('Semua kolom sudah ada. Tidak ada yang diubah.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->table(['Tabel', 'Kolom', 'Tipe'], $missing);
|
|
|
|
if (! $this->option('force') && ! $this->confirm('Tambahkan kolom di atas ke DB untan? Kolom bersifat opsional (NULL) dan tidak mengubah data lama.')) {
|
|
$this->warn('Dibatalkan. Tidak ada yang diubah.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
foreach ($missing as [$table, $column, $type]) {
|
|
DB::connection('untan')->statement("alter table {$table} add column {$column} {$type}");
|
|
$this->line("Kolom {$table}.{$column} ditambahkan.");
|
|
}
|
|
|
|
$this->info('Selesai.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|