Penambahan Menu Setting
This commit is contained in:
@@ -2,19 +2,23 @@
|
||||
|
||||
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 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.
|
||||
* 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 dan deleted_at di x_message (DB untan)';
|
||||
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 = [
|
||||
@@ -22,7 +26,14 @@ class PreparePortalCommand extends Command
|
||||
'x_message' => ['deleted_at' => 'timestamp'],
|
||||
];
|
||||
|
||||
public function handle(): int
|
||||
/** 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 = [];
|
||||
@@ -41,27 +52,77 @@ class PreparePortalCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
foreach (self::TABLES as $table => $definition) {
|
||||
if (! $schema->hasTable($table)) {
|
||||
$missing[] = [$table, '(tabel baru)', $definition];
|
||||
}
|
||||
}
|
||||
|
||||
if ($missing === []) {
|
||||
$this->info('Semua kolom sudah ada. Tidak ada yang diubah.');
|
||||
$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 di atas ke DB untan? Kolom bersifat opsional (NULL) dan tidak mengubah data lama.')) {
|
||||
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);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user