Skip to content

Commit

Permalink
feat: foreign key constraints and comments deleted when deleting posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Sep 20, 2023
1 parent 2ba5cdc commit 70a60fe
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,17 @@ public function update(Request $request, int $id): RedirectResponse
}

/**
* Delete a post.
* Delete a post and its associated comments.
*
* @param Request $request
* @param int $id
* @return 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('/');
Expand Down
63 changes: 63 additions & 0 deletions src/database/migrations/2023_09_20_214931_add_foreign_keys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('comments', function (Blueprint $table) {
$table->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');
});
}
};

0 comments on commit 70a60fe

Please sign in to comment.