Add column tag permata and feat to function

This commit is contained in:
amirhassan
2024-03-14 08:53:48 +08:00
parent c327de1997
commit fc4c5f4e84
3 changed files with 80 additions and 4 deletions
@@ -0,0 +1,76 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
class AlterBatuPermataColumnInMarhunTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
/**
*
* @var object $db_connection
*/
public function getActiveDB()
{
$config_db = array_keys(config("database.connections"));
$db_connection = array_filter($config_db, function ($connection) {
try {
if ($connection == 'mysql7' || $connection == 'lelong')
return false;
$db = DB::connection($connection)->getPdo();
return true;
} catch (\Exception $e) {
return false;
}
});
return $db_connection;
}
public function up()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
$isColumnBatuPermataExist = Schema::connection($dbase)->hasColumn('marhun', 'batu_permata');
if ($isColumnBatuPermataExist) {
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($dbase) {
$table->dropColumn('batu_permata');
});
}
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($dbase) {
$table->integer('tag_permata')->default(0);
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($dbase) {
$table->dropColumn('tag_permata');
});
$isColumnBatuPermataExist = Schema::connection($dbase)->hasColumn('marhun', 'batu_permata');
if (!$isColumnBatuPermataExist) {
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($dbase) {
$table->string('batu_permata')->nullable();
});
}
}
}
}