Skip to content

Commit

Permalink
fix: Migrations rollback values too long
Browse files Browse the repository at this point in the history
  • Loading branch information
IceToast committed Nov 24, 2024
1 parent c26d81f commit a951e01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -14,7 +15,7 @@
public function up()
{
Schema::table('nests', function (Blueprint $table) {
$table->text('description')->change();
$table->text(column: 'description')->change();
});
}

Expand All @@ -26,7 +27,13 @@ public function up()
public function down()
{
Schema::table('nests', function (Blueprint $table) {
$table->string('description')->change();
$oldvalues = DB::table('nests')->pluck('description');
foreach ($oldvalues as $value) {
if (strlen($value) > 255) {
DB::table('nests')->update(['description' => substr($value, 0, 255)]);
}
}
$table->string(column: 'description', length: 255)->change();
});
}
};
12 changes: 10 additions & 2 deletions database/migrations/2023_01_12_135936_settings_to_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -14,6 +15,7 @@
public function up()
{
Schema::table('settings', function (Blueprint $table) {

$table->text('value')->change();
});
}
Expand All @@ -25,8 +27,14 @@ public function up()
*/
public function down()
{
Schema::table('nests', function (Blueprint $table) {
$table->string('value')->change();
Schema::table('settings', function (Blueprint $table) {
$oldvalues = DB::table('settings')->pluck('value');
foreach ($oldvalues as $value) {
if (strlen($value) > 255) {
DB::table('settings')->update(['value' => substr($value, 0, 255)]);
}
}
$table->string('value', 255)->change();
});
}
};

0 comments on commit a951e01

Please sign in to comment.