diff --git a/database/migrations/2020_12_22_051133_create_recipe_steps_table.php b/database/migrations/2020_12_22_051133_create_recipe_steps_table.php index 3a89613..acfcf8e 100644 --- a/database/migrations/2020_12_22_051133_create_recipe_steps_table.php +++ b/database/migrations/2020_12_22_051133_create_recipe_steps_table.php @@ -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'); }); } diff --git a/database/migrations/2020_12_31_180016_create_journal_entries_table.php b/database/migrations/2020_12_31_180016_create_journal_entries_table.php index d896c19..01836f4 100644 --- a/database/migrations/2020_12_31_180016_create_journal_entries_table.php +++ b/database/migrations/2020_12_31_180016_create_journal_entries_table.php @@ -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']); }); } diff --git a/database/migrations/2021_01_01_170724_create_journalables_table.php b/database/migrations/2021_01_01_170724_create_journalables_table.php index a3c0cf7..ea3be1b 100644 --- a/database/migrations/2021_01_01_170724_create_journalables_table.php +++ b/database/migrations/2021_01_01_170724_create_journalables_table.php @@ -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'); }); diff --git a/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php b/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php index c994f32..602ca54 100644 --- a/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php +++ b/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php @@ -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(); }); diff --git a/database/migrations/2021_02_19_080648_add_cascade_constraints.php b/database/migrations/2021_02_19_080648_add_cascade_constraints.php new file mode 100644 index 0000000..a339f3d --- /dev/null +++ b/database/migrations/2021_02_19_080648_add_cascade_constraints.php @@ -0,0 +1,88 @@ +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() + { + // + } +}