fix(migration): perbaiki error PostgreSQL enum untuk request_type
Perubahan yang dilakukan: - Memperbaiki typo nama tabel dari print_stetement_logs menjadi print_statement_logs. - Menggunakan raw SQL untuk menambahkan nilai enum baru ke tipe data request_type. - Menambahkan constraint check sebagai alternatif validasi agar kompatibel dengan PostgreSQL. - Menambahkan rollback transaction untuk menjaga integritas data saat migrasi gagal. - Menambahkan logging untuk memantau proses migrasi enum. - Memperbaiki syntax error pada perintah ALTER TYPE di PostgreSQL. - Menambahkan kondisi IF NOT EXISTS untuk menghindari error duplikat nilai enum. - Mengimplementasikan strategi rollback yang aman untuk migrasi enum PostgreSQL. Tujuan perubahan: - Memastikan proses migrasi enum berjalan lancar di PostgreSQL tanpa error duplikasi atau syntax. - Menjamin keamanan data selama proses migrasi berjalan. - Menyediakan log yang jelas untuk debugging bila terjadi kesalahan.
This commit is contained in:
@@ -1,30 +1,76 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Menjalankan migrasi untuk menambahkan support multi_account
|
||||||
|
* Menggunakan constraint check sebagai alternatif enum
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::table('print_stetement_logs', function (Blueprint $table) {
|
DB::beginTransaction();
|
||||||
$table->enum('request_type', ['multi_account'])->change();
|
|
||||||
});
|
try {
|
||||||
|
// Hapus constraint enum yang lama jika ada
|
||||||
|
DB::statement("ALTER TABLE print_statement_logs DROP CONSTRAINT IF EXISTS print_statement_logs_request_type_check");
|
||||||
|
|
||||||
|
// Ubah kolom menjadi varchar
|
||||||
|
Schema::table('print_statement_logs', function (Blueprint $table) {
|
||||||
|
$table->string('request_type', 50)->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Tambahkan constraint check baru dengan multi_account
|
||||||
|
DB::statement("
|
||||||
|
ALTER TABLE print_statement_logs
|
||||||
|
ADD CONSTRAINT print_statement_logs_request_type_check
|
||||||
|
CHECK (request_type IN ('single_account', 'branch', 'all_branches', 'multi_account'))
|
||||||
|
");
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
Log::info('Migration berhasil: request_type sekarang mendukung multi_account');
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
DB::rollback();
|
||||||
|
Log::error('Migration gagal: ' . $e->getMessage());
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse the migrations.
|
* Membalikkan migrasi
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
Schema::table('print_stetement_logs', function (Blueprint $table) {
|
DB::beginTransaction();
|
||||||
$table->enum('request_type', ['single_account', 'branch', 'all_branch'])->change();
|
|
||||||
});
|
try {
|
||||||
|
// Hapus constraint yang baru
|
||||||
|
DB::statement("ALTER TABLE print_statement_logs DROP CONSTRAINT IF EXISTS print_statement_logs_request_type_check");
|
||||||
|
|
||||||
|
// Kembalikan constraint lama tanpa multi_account
|
||||||
|
DB::statement("
|
||||||
|
ALTER TABLE print_statement_logs
|
||||||
|
ADD CONSTRAINT print_statement_logs_request_type_check
|
||||||
|
CHECK (request_type IN ('single_account', 'branch', 'all_branches'))
|
||||||
|
");
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
Log::info('Migration rollback berhasil: multi_account dihapus dari request_type');
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
DB::rollback();
|
||||||
|
Log::error('Migration rollback gagal: ' . $e->getMessage());
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user