From 70a60feb85bd3aa98da1ce132a4c3225396d31e0 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:47:57 -0600 Subject: [PATCH] feat: foreign key constraints and comments deleted when deleting posts --- CHANGELOG.md | 2 + src/app/Http/Controllers/PostController.php | 6 +- .../2023_09_20_214931_add_foreign_keys.php | 63 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/database/migrations/2023_09_20_214931_add_foreign_keys.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a4570..526d3b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Categories are now unique and required, enforced at the database level - Various database indexes were added for common lookup patterns for quick retrieval +- Foreign key constraints were added to enforce data integrity at the database level when records are related to one another + - Due to this addition, comments of a post now get deleted when a post is deleted - Fixes various database field types and type validation on form submission to better align with the context they are used with - Fixes a bug that didn't show the dropdown chevrons for form-select fields when creating a post - Fixes a Bootstrap deprecation warning diff --git a/src/app/Http/Controllers/PostController.php b/src/app/Http/Controllers/PostController.php index 989b839..46cb7c4 100644 --- a/src/app/Http/Controllers/PostController.php +++ b/src/app/Http/Controllers/PostController.php @@ -227,7 +227,7 @@ public function update(Request $request, int $id): RedirectResponse } /** - * Delete a post. + * Delete a post and its associated comments. * * @param Request $request * @param int $id @@ -235,7 +235,9 @@ public function update(Request $request, int $id): RedirectResponse */ public function delete(Request $request, int $id): RedirectResponse { - Post::find($id)->delete(); + $post = Post::find($id); + $post->comments()->delete(); + $post->delete(); session()->flash('message', 'Post deleted.'); return redirect('/'); diff --git a/src/database/migrations/2023_09_20_214931_add_foreign_keys.php b/src/database/migrations/2023_09_20_214931_add_foreign_keys.php new file mode 100644 index 0000000..13c1747 --- /dev/null +++ b/src/database/migrations/2023_09_20_214931_add_foreign_keys.php @@ -0,0 +1,63 @@ +bigInteger('user_id')->unsigned()->change(); + $table->bigInteger('post_id')->unsigned()->change(); + + $table->foreign('user_id')->references('id')->on('users'); + $table->foreign('post_id')->references('id')->on('posts'); + + $table->renameIndex('comments_post_id_index', 'comments_post_id_foreign'); + }); + + Schema::table('posts', function (Blueprint $table) { + $table->bigInteger('user_id')->unsigned()->change(); + $table->bigInteger('category_id')->unsigned()->change(); + + $table->foreign('user_id')->references('id')->on('users'); + $table->foreign('category_id')->references('id')->on('categories'); + + $table->renameIndex('posts_category_id_index', 'posts_category_id_foreign'); + $table->renameIndex('posts_user_id_index', 'posts_user_id_foreign'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('comments', function (Blueprint $table) { + $table->dropForeign('comments_user_id_foreign'); + $table->dropForeign('comments_post_id_foreign'); + + $table->dropIndex('comments_user_id_foreign'); + $table->renameIndex('comments_post_id_foreign', 'comments_post_id_index'); + }); + + // Changes must be done separately from dropping indexes + Schema::table('comments', function (Blueprint $table) { + $table->integer('user_id')->change(); + $table->integer('post_id')->change(); + }); + + Schema::table('posts', function (Blueprint $table) { + $table->dropForeign('posts_user_id_foreign'); + $table->dropForeign('posts_category_id_foreign'); + + $table->renameIndex('posts_category_id_foreign', 'posts_category_id_index'); + $table->renameIndex('posts_user_id_foreign', 'posts_user_id_index'); + }); + } +};