129 lines
4.9 KiB
PHP
129 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Support\Legacy\SurveyAnswerTypes;
|
|
use App\Support\Legacy\SurveyCategories;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/**
|
|
* Menyiapkan kolom dan tabel tambahan di DB lama untuk menu operator (satu-satunya DDL ke DB `untan`):
|
|
* kode dan warna jenis kegiatan akademik, penanda hapus-lunak pesan konsultasi, serta tabel kategori kuesioner
|
|
* layanan dan jenis jawaban kuesioner (diisi data SATU saat pertama dibuat). 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, deleted_at di x_message, serta tabel kategori dan jenis jawaban kuesioner (DB untan)';
|
|
|
|
/** tabel => [kolom => tipe] */
|
|
private const array COLUMNS = [
|
|
't_kalender_jenis_kegiatan' => ['kode' => 'varchar(5)', 'warna' => 'varchar(7)'],
|
|
'x_message' => ['deleted_at' => 'timestamp'],
|
|
];
|
|
|
|
/** tabel baru => keterangan kolom (dibuat lewat Blueprint di createTable, mengikuti gaya tabel lama: id serial + audit) */
|
|
private const array TABLES = [
|
|
SurveyCategories::TABLE => 'idkategori serial, nama varchar(100), cname/cip/uname varchar(20), tanggal timestamp',
|
|
SurveyAnswerTypes::TABLE => 'idjenisjawaban serial, nama varchar(50), cname/cip/uname varchar(20), tanggal timestamp',
|
|
SurveyAnswerTypes::ANSWERS => 'idjawaban serial, idjenisjawaban int, urutan smallint, nilai smallint, jawaban varchar(100)',
|
|
];
|
|
|
|
public function handle(SurveyCategories $categories, SurveyAnswerTypes $answerTypes): 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];
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (self::TABLES as $table => $definition) {
|
|
if (! $schema->hasTable($table)) {
|
|
$missing[] = [$table, '(tabel baru)', $definition];
|
|
}
|
|
}
|
|
|
|
if ($missing === []) {
|
|
$this->info('Semua kolom dan tabel sudah ada. Tidak ada yang diubah.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->table(['Tabel', 'Kolom', 'Tipe'], $missing);
|
|
|
|
if (! $this->option('force') && ! $this->confirm('Tambahkan kolom/tabel 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]) {
|
|
if ($column === '(tabel baru)') {
|
|
$this->createTable($table);
|
|
$this->line("Tabel {$table} dibuat.");
|
|
|
|
continue;
|
|
}
|
|
|
|
DB::connection('untan')->statement("alter table {$table} add column {$column} {$type}");
|
|
$this->line("Kolom {$table}.{$column} ditambahkan.");
|
|
}
|
|
|
|
if (($seeded = $categories->seedDefaults('siapkan-portal')) > 0) {
|
|
$this->line('Tabel '.SurveyCategories::TABLE." diisi {$seeded} kategori dari SATU.");
|
|
}
|
|
if (($seeded = $answerTypes->seedDefaults('siapkan-portal')) > 0) {
|
|
$this->line('Tabel '.SurveyAnswerTypes::TABLE." diisi {$seeded} jenis jawaban dari SATU.");
|
|
}
|
|
|
|
$this->info('Selesai.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function createTable(string $table): void
|
|
{
|
|
$audit = function (Blueprint $t): void {
|
|
$t->string('cname', 20)->nullable();
|
|
$t->string('cip', 20)->nullable();
|
|
$t->string('uname', 20)->nullable();
|
|
$t->timestamp('tanggal')->nullable();
|
|
};
|
|
|
|
Schema::connection('untan')->create($table, match ($table) {
|
|
SurveyCategories::TABLE => function (Blueprint $t) use ($audit): void {
|
|
$t->increments('idkategori');
|
|
$t->string('nama', 100);
|
|
$audit($t);
|
|
},
|
|
SurveyAnswerTypes::TABLE => function (Blueprint $t) use ($audit): void {
|
|
$t->increments('idjenisjawaban');
|
|
$t->string('nama', 50);
|
|
$audit($t);
|
|
},
|
|
SurveyAnswerTypes::ANSWERS => function (Blueprint $t): void {
|
|
$t->increments('idjawaban');
|
|
$t->integer('idjenisjawaban')->index();
|
|
$t->smallInteger('urutan');
|
|
$t->smallInteger('nilai');
|
|
$t->string('jawaban', 100);
|
|
},
|
|
});
|
|
}
|
|
}
|