Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving seeder performance #765

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
wip
  • Loading branch information
driesvints committed Nov 24, 2021
commit ae161d618d90f9d6d00e67129e276e50894e3f9e
2 changes: 2 additions & 0 deletions database/seeders/NotificationSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function run()
$replies = Reply::with(['authorRelation.notifications', 'replyAbleRelation'])->get();

DB::beginTransaction();

foreach ($replies as $reply) {
$reply->author()->notifications()->create([
'id' => Uuid::uuid4()->toString(),
Expand All @@ -33,6 +34,7 @@ public function run()
'updated_at' => $reply->createdAt(),
]);
}

DB::commit();
}
}
4 changes: 4 additions & 0 deletions database/seeders/ReplySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function run()
$threads = Thread::all();

DB::beginTransaction();

// Create 5 replies for each thread from random users.
foreach ($threads as $thread) {
Reply::factory()
Expand All @@ -28,13 +29,16 @@ public function run()
))
->createQuietly();
}

DB::commit();

DB::beginTransaction();

// Give 10 random threads a solution.
foreach ($threads->random(20) as $thread) {
$thread->markSolution($thread->repliesRelation()->get()->load('replyAbleRelation')->random(), $thread->author());
}

DB::commit();
}
}
2 changes: 2 additions & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function run()
]);

DB::beginTransaction();

User::factory()
->count(100)
->has(Thread::factory()->count(2), 'threadsRelation')
Expand All @@ -42,6 +43,7 @@ public function run()
),
)
->createQuietly();

DB::commit();

Article::published()
Expand Down