Skip to content

Commit

Permalink
Update migrations to be agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Nov 8, 2024
1 parent ebe6a65 commit e6566cb
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
Expand All @@ -12,7 +11,7 @@
public function up(): void
{
Schema::table('nodes', function (Blueprint $table) {
DB::statement('ALTER TABLE nodes MODIFY COLUMN network varchar(255) AFTER backup_storage');
$table->string('network')->after('backup_storage')->change();
});
}

Expand All @@ -22,7 +21,7 @@ public function up(): void
public function down(): void
{
Schema::table('nodes', function (Blueprint $table) {
DB::statement('ALTER TABLE nodes MODIFY COLUMN network varchar(255) AFTER vm_storage');
$table->string('network')->after('vm_storage')->change();
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
Expand All @@ -12,7 +11,8 @@
public function up(): void
{
Schema::table('template_groups', function (Blueprint $table) {
DB::statement('ALTER TABLE template_groups MODIFY COLUMN node_id bigint unsigned AFTER id');

$table->unsignedBigInteger('node_id')->after('id')->change();
});
}

Expand All @@ -22,7 +22,7 @@ public function up(): void
public function down(): void
{
Schema::table('template_groups', function (Blueprint $table) {
DB::statement('ALTER TABLE template_groups MODIFY COLUMN node_id bigint unsigned AFTER updated_at');
$table->unsignedBigInteger('node_id')->after('updated_at')->change();
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
return new class extends Migration
{
public function up(): void
{
DB::statement('ALTER TABLE nodes MODIFY COLUMN port int AFTER fqdn');
Schema::table('nodes', function (Blueprint $table) {
// Explicitly include all desired attributes for Laravel 11
$table->integer('port')->nullable(false)->after('fqdn')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('ALTER TABLE nodes MODIFY COLUMN port int AFTER secret');
Schema::table('nodes', function (Blueprint $table) {
// Move the column back with all necessary attributes specified
$table->integer('port')->nullable(false)->after('secret')->change();
});
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
Expand All @@ -15,7 +15,12 @@ public function up(): void
$table->char('uuid', 36)->nullable()->after('id');
});

DB::statement('UPDATE users SET uuid=(select UUID())');
User::chunk(100, function ($users) {
foreach ($users as $user) {
$user->uuid = Str::uuid()->toString();
$user->save();
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER server_id');
Schema::table('ip_addresses', function (Blueprint $table) {
// Modify the 'type' column to be after 'server_id' with all necessary attributes
$table->string('type', 255)->after('server_id')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER mac_address');
Schema::table('ip_addresses', function (Blueprint $table) {
// Move the 'type' column to be after 'mac_address' with all necessary attributes
$table->string('type', 255)->after('mac_address')->change();
});
}
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php

use App\Models\Address;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
DB::statement(
'UPDATE ip_addresses SET address = LOWER(address), gateway = LOWER(gateway), mac_address = LOWER(mac_address)',
);
Address::chunkById(100, function ($ipAddresses) {
foreach ($ipAddresses as $ip) {
$ip->address = strtolower($ip->address);
$ip->gateway = strtolower($ip->gateway);
$ip->mac_address = strtolower($ip->mac_address);
$ip->save();
}
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,39 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
return new class extends Migration
{
public function up(): void
{
// Delete records based on conditions
DB::statement(
'DELETE FROM backups WHERE completed_at IS NOT NULL AND is_successful = 0;',
);
// Delete records based on conditions without raw SQL
DB::table('backups')
->whereNotNull('completed_at')
->where('is_successful', 0)
->delete();

Schema::table('backups', function (Blueprint $table) {
$table->string('description')->nullable()->after('name');
$table->string('errors')->nullable()->after('description');

$table->dropSoftDeletes();
$table->dropColumn(['is_successful', 'updated_at']);
});

// Modify the 'is_locked' column to be not nullable with a default of false (0)
DB::statement(
'ALTER TABLE backups MODIFY COLUMN is_locked TINYINT(1) NOT NULL DEFAULT 0 AFTER description',
);
// Explicitly define 'is_locked' attributes to prevent unintended behavior
$table->boolean('is_locked')->default(false)->nullable(false)->after('description')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('backups', function (Blueprint $table) {
$table->dropColumn(['description', 'errors']);

$table->boolean('is_locked')->nullable()->change();
// Re-apply all attributes to 'is_locked' in down migration
$table->boolean('is_locked')->nullable()->default(null)->after('is_successful')->change();

$table->boolean('is_successful')->default(false)->after('server_id');
$table->timestamp('updated_at')->nullable()->after('created_at');
$table->softDeletes()->after('updated_at');
});

// Modify the 'is_locked' column to be nullable in the down migration
DB::statement(
'ALTER TABLE backups MODIFY COLUMN is_locked TINYINT(1) NULL AFTER is_successful',
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function down(): void
$table->after('name', fn (Blueprint $table) => $table->timestamps());
});

DB::statement('UPDATE address_pools SET created_at = NOW()');
DB::table('address_pools')->update(['created_at' => now()]);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,54 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
return new class extends Migration
{
public function up(): void
{
DB::statement(
'ALTER TABLE servers MODIFY COLUMN snapshot_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN backup_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN bandwidth_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN bandwidth_usage INT UNSIGNED NOT NULL DEFAULT 0;',
);

DB::statement('UPDATE servers SET snapshot_limit = -1 WHERE snapshot_limit IS NULL;');
DB::statement('UPDATE servers SET backup_limit = -1 WHERE backup_limit IS NULL;');
DB::statement('UPDATE servers SET bandwidth_limit = -1 WHERE bandwidth_limit IS NULL;');
Schema::table('servers', function (Blueprint $table) {
$table->integer('snapshot_limit')->nullable()->change();
$table->integer('backup_limit')->nullable()->change();
$table->integer('bandwidth_limit')->nullable()->change();
$table->unsignedInteger('bandwidth_usage')->default(0)->change();
});

DB::statement(
'ALTER TABLE servers CHANGE COLUMN snapshot_limit snapshot_count_limit INT NOT NULL;',
);
DB::statement(
'ALTER TABLE servers CHANGE COLUMN backup_limit backup_count_limit INT NOT NULL;',
);
DB::statement(
'ALTER TABLE servers CHANGE COLUMN bandwidth_limit bandwidth_limit INT NOT NULL;',
);
DB::table('servers')->whereNull('snapshot_limit')->update(['snapshot_limit' => -1]);
DB::table('servers')->whereNull('backup_limit')->update(['backup_limit' => -1]);
DB::table('servers')->whereNull('bandwidth_limit')->update(['bandwidth_limit' => -1]);

Schema::table('servers', function (Blueprint $table) {
$table->renameColumn('snapshot_limit', 'snapshot_count_limit');
$table->renameColumn('backup_limit', 'backup_count_limit');
$table->renameColumn('bandwidth_limit', 'bandwidth_limit');
$table->integer('snapshot_size_limit')->after('snapshot_count_limit');
$table->integer('backup_size_limit')->after('backup_count_limit');
});

Schema::table('servers', function (Blueprint $table) {
$table->integer('snapshot_count_limit')->nullable(false)->change();
$table->integer('backup_count_limit')->nullable(false)->change();
$table->integer('bandwidth_limit')->nullable(false)->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement(
'ALTER TABLE servers MODIFY COLUMN snapshot_count_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN backup_count_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN bandwidth_limit INT NULL;',
);
DB::statement(
'ALTER TABLE servers MODIFY COLUMN bandwidth_usage INT NOT NULL DEFAULT 0;',
);
Schema::table('servers', function (Blueprint $table) {
$table->integer('snapshot_count_limit')->nullable()->change();
$table->integer('backup_count_limit')->nullable()->change();
$table->integer('bandwidth_limit')->nullable()->change();
$table->integer('bandwidth_usage')->default(0)->change();
});

DB::statement(
'UPDATE servers SET snapshot_count_limit = NULL WHERE snapshot_count_limit = -1;',
);
DB::statement(
'UPDATE servers SET backup_count_limit = NULL WHERE backup_count_limit = -1;',
);
DB::statement('UPDATE servers SET bandwidth_limit = NULL WHERE bandwidth_limit = -1;');
DB::table('servers')->where('snapshot_count_limit', -1)->update(['snapshot_count_limit' => null]);
DB::table('servers')->where('backup_count_limit', -1)->update(['backup_count_limit' => null]);
DB::table('servers')->where('bandwidth_limit', -1)->update(['bandwidth_limit' => null]);

Schema::table('servers', function (Blueprint $table) {
$table->dropColumn('snapshot_size_limit', 'backup_size_limit');
$table->renameColumn('snapshot_count_limit', 'snapshot_limit');
$table->renameColumn('backup_count_limit', 'backup_limit');
$table->renameColumn('bandwidth_limit', 'bandwidth_limit');
});

DB::statement(
'ALTER TABLE servers CHANGE COLUMN snapshot_count_limit snapshot_limit INT UNSIGNED NULL;',
);
DB::statement(
'ALTER TABLE servers CHANGE COLUMN backup_count_limit backup_limit INT UNSIGNED NULL;',
);
DB::statement(
'ALTER TABLE servers CHANGE COLUMN bandwidth_limit bandwidth_limit INT UNSIGNED NULL;',
);
}
};
Loading

0 comments on commit e6566cb

Please sign in to comment.