mirror of https://github.com/kcal-app/kcal.git
Add constraints and indexes to existing tables
This commit is contained in:
parent
fd45baa946
commit
08b8c63622
|
@ -16,11 +16,10 @@ class CreateRecipeStepsTable extends Migration
|
|||
{
|
||||
Schema::create('recipe_steps', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(Recipe::class);
|
||||
$table->unsignedInteger('number');
|
||||
$table->foreignIdFor(Recipe::class)->index()->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->unsignedInteger('number')->index();
|
||||
$table->longText('step');
|
||||
$table->timestamps();
|
||||
$table->index('recipe_id', 'number');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ class CreateJournalEntriesTable extends Migration
|
|||
{
|
||||
Schema::create('journal_entries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class);
|
||||
$table->date('date')->useCurrent();
|
||||
$table->foreignIdFor(User::class)->index()->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->date('date')->index()->useCurrent();
|
||||
$table->string('summary');
|
||||
$table->unsignedFloat('calories')->default(0);
|
||||
$table->unsignedFloat('fat')->default(0);
|
||||
|
@ -27,7 +27,6 @@ class CreateJournalEntriesTable extends Migration
|
|||
$table->unsignedFloat('protein')->default(0);
|
||||
$table->enum('meal', ['breakfast', 'lunch', 'dinner', 'snacks']);
|
||||
$table->timestamps();
|
||||
$table->index(['user_id', 'date']);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ class CreateJournalablesTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
Schema::create('journalables', function (Blueprint $table) {
|
||||
$table->foreignIdFor(JournalEntry::class)->index();
|
||||
$table->foreignIdFor(JournalEntry::class)->index()->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->unsignedInteger('journalable_id');
|
||||
$table->string('journalable_type');
|
||||
});
|
||||
|
|
|
@ -21,7 +21,7 @@ class CreateIngredientAmountsTable extends Migration
|
|||
$table->enum('unit', ['tsp', 'tbsp', 'cup', 'oz', 'gram', 'serving'])->nullable();
|
||||
$table->string('detail')->nullable();
|
||||
$table->unsignedInteger('weight');
|
||||
$table->unsignedInteger('parent_id');
|
||||
$table->unsignedInteger('parent_id')->index();
|
||||
$table->string('parent_type');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
use App\Models\JournalEntry;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddCascadeConstraints extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// Recipe steps.
|
||||
Schema::rename('recipe_steps', 'recipe_steps_old');
|
||||
Schema::create('recipe_steps', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(Recipe::class)->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->unsignedInteger('number');
|
||||
$table->longText('step');
|
||||
$table->timestamps();
|
||||
});
|
||||
DB::insert("INSERT INTO `recipe_steps` SELECT * FROM `recipe_steps_old`;");
|
||||
Schema::dropIfExists('recipe_steps_old');
|
||||
Schema::table('recipe_steps', function (Blueprint $table) {
|
||||
$table->index('recipe_id');
|
||||
$table->index('number');
|
||||
});
|
||||
|
||||
// Journal entries.
|
||||
Schema::rename('journal_entries', 'journal_entries_old');
|
||||
Schema::create('journal_entries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class)->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->date('date')->useCurrent();
|
||||
$table->string('summary');
|
||||
$table->unsignedFloat('calories')->default(0);
|
||||
$table->unsignedFloat('fat')->default(0);
|
||||
$table->unsignedFloat('cholesterol')->default(0);
|
||||
$table->unsignedFloat('sodium')->default(0);
|
||||
$table->unsignedFloat('carbohydrates')->default(0);
|
||||
$table->unsignedFloat('protein')->default(0);
|
||||
$table->enum('meal', ['breakfast', 'lunch', 'dinner', 'snacks']);
|
||||
$table->timestamps();
|
||||
});
|
||||
DB::insert("INSERT INTO `journal_entries` SELECT * FROM `journal_entries_old`;");
|
||||
Schema::dropIfExists('journal_entries_old');
|
||||
Schema::table('journal_entries', function (Blueprint $table) {
|
||||
$table->index('user_id');
|
||||
$table->index('date');
|
||||
});
|
||||
|
||||
// Journalables.
|
||||
Schema::rename('journalables', 'journalables_old');
|
||||
Schema::create('journalables', function (Blueprint $table) {
|
||||
$table->foreignIdFor(JournalEntry::class)->constrained()->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->unsignedInteger('journalable_id');
|
||||
$table->string('journalable_type');
|
||||
});
|
||||
DB::delete("DELETE FROM `journalables_old` WHERE `journal_entry_id` NOT IN (SELECT `id` FROM `journal_entries`);");
|
||||
DB::insert("INSERT INTO `journalables` SELECT * FROM `journalables_old`;");
|
||||
Schema::dropIfExists('journalables_old');
|
||||
Schema::table('journalables', function (Blueprint $table) {
|
||||
$table->index('journal_entry_id');
|
||||
});
|
||||
|
||||
// Ingredient amounts (index only).
|
||||
Schema::table('ingredient_amounts', function (Blueprint $table) {
|
||||
$table->index('parent_id');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue