forked from laravelio/laravel.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance optimisations (laravelio#761)
* Migrate to timestamp * Update last active on save * Update tests * Load only required models * Apply fixes from StyleCI * Rename column * Apply fixes from StyleCI
- Loading branch information
Showing
10 changed files
with
99 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\ReplyWasCreated; | ||
|
||
final class MarkLastActivity | ||
{ | ||
public function handle(ReplyWasCreated $event): void | ||
{ | ||
$replyAble = $event->reply->replyAble(); | ||
$replyAble->last_activity_at = now(); | ||
$replyAble->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2021_11_22_093555_migrate_thread_feed_to_timestamp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use App\Models\Thread; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class MigrateThreadFeedToTimestamp extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table('threads', function (Blueprint $table) { | ||
$table->timestamp('last_activity_at') | ||
->after('slug') | ||
->nullable() | ||
->index(); | ||
}); | ||
|
||
// Iterate over all the threads and update the `last_active_at` timestamp to | ||
// the latest reply `created_at` or thread `created_at` if no reply exists. | ||
if (! app()->runningUnitTests()) { | ||
Thread::with(['repliesRelation' => function ($query) { | ||
$query->latest(); | ||
}]) | ||
->lazy() | ||
->each(function ($thread) { | ||
$thread->update([ | ||
'last_activity_at' => ($reply = $thread->replies()->first()) ? $reply->created_at : $thread->created_at, | ||
]); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters