No-op meal field migrations

This allows tests to pass easier and the migrations are not entirely
necessary anyway.
This commit is contained in:
Christopher C. Wells 2021-05-29 19:41:48 -07:00
parent 8ab11d5456
commit 2eccf84fea
4 changed files with 42 additions and 40 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Models\User;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@ -19,6 +20,7 @@ class CreateUsersTable extends Migration
$table->string('username')->unique(); $table->string('username')->unique();
$table->string('password'); $table->string('password');
$table->string('name'); $table->string('name');
$table->json('meals')->default(User::getDefaultMeals());
$table->boolean('admin')->default(false); $table->boolean('admin')->default(false);
$table->rememberToken(); $table->rememberToken();
$table->timestamps(); $table->timestamps();

View File

@ -26,7 +26,7 @@ class CreateJournalEntriesTable extends Migration
$table->unsignedFloat('sodium')->default(0); $table->unsignedFloat('sodium')->default(0);
$table->unsignedFloat('carbohydrates')->default(0); $table->unsignedFloat('carbohydrates')->default(0);
$table->unsignedFloat('protein')->default(0); $table->unsignedFloat('protein')->default(0);
$table->enum('meal', JournalEntry::meals()->pluck('value')->toArray()); $table->integer('meal')->unsigned();
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -14,14 +14,14 @@ class AddMealsToUsersTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::table('users', function (Blueprint $table) { // Schema::table('users', function (Blueprint $table) {
$table->json('meals')->nullable()->after('name'); // $table->json('meals')->nullable()->after('name');
}); // });
//
User::each(function (User $user) { // User::each(function (User $user) {
$user->meals = User::getDefaultMeals(); // $user->meals = User::getDefaultMeals();
$user->save(); // $user->save();
}); // });
} }
/** /**
@ -31,8 +31,8 @@ class AddMealsToUsersTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::table('users', function (Blueprint $table) { // Schema::table('users', function (Blueprint $table) {
$table->dropColumn('meals'); // $table->dropColumn('meals');
}); // });
} }
} }

View File

@ -15,19 +15,19 @@ class ChangeMealToIntInJournalEntries extends Migration
*/ */
public function up() public function up()
{ {
Schema::table('journal_entries', function (Blueprint $table) { // Schema::table('journal_entries', function (Blueprint $table) {
$table->integer('meal_int')->unsigned()->nullable()->after('protein'); // $table->integer('meal_int')->unsigned()->nullable()->after('protein');
}); // });
DB::update('UPDATE `journal_entries` SET meal_int = // DB::update('UPDATE `journal_entries` SET meal_int =
IF(meal = "breakfast", 0, // IF(meal = "breakfast", 0,
IF(meal = "lunch", 1, // IF(meal = "lunch", 1,
IF(meal = "dinner", 2, 3) // IF(meal = "dinner", 2, 3)
) // )
)'); // )');
Schema::table('journal_entries', function (Blueprint $table) { // Schema::table('journal_entries', function (Blueprint $table) {
$table->dropColumn('meal'); // $table->dropColumn('meal');
$table->renameColumn('meal_int', 'meal'); // $table->renameColumn('meal_int', 'meal');
}); // });
} }
/** /**
@ -37,20 +37,20 @@ class ChangeMealToIntInJournalEntries extends Migration
*/ */
public function down() public function down()
{ {
Schema::table('journal_entries', function (Blueprint $table) { // Schema::table('journal_entries', function (Blueprint $table) {
$table->renameColumn('meal', 'meal_int'); // $table->renameColumn('meal', 'meal_int');
}); // });
Schema::table('journal_entries', function (Blueprint $table) { // Schema::table('journal_entries', function (Blueprint $table) {
$table->enum('meal', JournalEntry::meals()->pluck('value')->toArray())->after('protein'); // $table->enum('meal', JournalEntry::meals()->pluck('value')->toArray())->after('protein');
}); // });
DB::update('UPDATE `journal_entries` SET meal = // DB::update('UPDATE `journal_entries` SET meal =
IF(meal_int = 0, "breakfast", // IF(meal_int = 0, "breakfast",
IF(meal_int = 1, "lunch", // IF(meal_int = 1, "lunch",
IF(meal_int = 2, "dinner", "snacks") // IF(meal_int = 2, "dinner", "snacks")
) // )
)'); // )');
Schema::table('journal_entries', function (Blueprint $table) { // Schema::table('journal_entries', function (Blueprint $table) {
$table->dropColumn('meal_int'); // $table->dropColumn('meal_int');
}); // });
} }
} }