Database transactions
Skill soden46/syarif-laravel-ai-skills/skills/database-transactions
Keep Laravel writes atomic and consistent with transactions, locks, retries, idempotency, side-effect boundaries, and after-commit dispatch.From its SKILL.md
npx -y skills add soden46/syarif-laravel-ai-skills --skill database-transactionsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.8 KB, as published. Nobody here has run it
Database Transactions
Use database transactions for write operations that must be atomic.
This is the canonical transaction skill. It consolidates the former transactions-and-consistency topic.
Transaction boundaries usually belong inside an Action or Service, not spread across controllers.
Required Transaction Cases
Use DB::transaction() when a workflow:
- writes multiple related records;
- updates counters, balances, inventory, or state machines;
- coordinates audit records with domain writes;
- creates records and related child rows;
- must not partially succeed.
final class CreateRecord
{
public function handle(User $actor, array $data): Record
{
return DB::transaction(function () use ($actor, $data) {
$record = Record::create([
'owner_id' => $actor->id,
'name' => $data['name'],
]);
$record->items()->createMany($data['items'] ?? []);
return $record->fresh(['items']);
});
}
}
Filesystem Side Effects
Database rollbacks do not roll back files. Track stored paths and clean them up when the database write fails.
$storedPaths = [];
try {
DB::transaction(function () use ($request, &$storedPaths) {
$record = Record::create([...]);
foreach ($request->file('attachments', []) as $file) {
$storedPaths[] = $file->store("records/{$record->id}", 'public');
}
});
} catch (Throwable $exception) {
Storage::disk('public')->delete($storedPaths);
throw $exception;
}
For delete flows, prefer committing database/audit changes first, then deleting files after the successful transaction unless the product explicitly requires the opposite failure mode.
Locks And Retries
Use row locks for concurrent updates to shared counters, balances, or ordered state.
DB::transaction(function () use ($recordId) {
$record = Record::query()
->whereKey($recordId)
->lockForUpdate()
->firstOrFail();
$record->increment('sequence');
});
Keep transactions short. Make retry behavior idempotent when deadlock retries are used.
After-Commit Work
Dispatch queued jobs/events after commit when they depend on committed records.
Do not hold a database transaction open during slow HTTP calls, mail delivery, document generation, or other remote side effects. Persist the state needed to continue, commit it, and dispatch after commit. Use an outbox or equivalent durable handoff when losing the side effect would be unacceptable.
Idempotency And Consistency
- Protect retried commands, webhooks, and queued jobs with a stable idempotency key or a domain state check.
- Enforce uniqueness in the database when duplicate prevention is a data invariant.
- Make retry behavior return or recover the original result instead of repeating side effects.
- Keep lock ordering consistent across workflows to reduce deadlocks.
- Use optimistic checks when conflicts should be reported rather than serialized.
- Prefer explicit compensating behavior for filesystem or provider operations that cannot participate in the database transaction.
Test:
- success path;
- rollback path;
- duplicate or retried execution;
- concurrent updates when locking matters;
- filesystem cleanup when applicable;
- after-commit behavior for important workflows.
Context Efficiency
Layer: 3 (Implementation)
Load this skill only when writes need atomicity or consistency. Do not load with unrelated skills. Keep transactions short, idempotent, and after-commit for side effects. No open transaction during slow HTTP calls or mail delivery.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.
Gives 0 of the 12 instructions most databases sql skills give
Counted across 609 of the 712 authors here whose files we hold, read 2026-09-06
- Index all foreign key columnsin 26 of 609
- Use cursor pagination instead of offsetin 25 of 609, across 20 files
- Use timestamptz for timestampsin 21 of 609
- Specify columns instead of using select starin 20 of 609, across 10 files
- Use parameterized queries for all database interactionsin 20 of 609, across 19 files
- Use Enum for categorical datain 17 of 609, across 7 files
- Order by frequently filtered columnsin 17 of 609, across 7 files
- Batch data insertsin 17 of 609, across 7 files
- Use expand-contract pattern for schema changesin 17 of 609
- Use materialized views for real-time aggregationsin 16 of 609, across 6 files
- Partition tables by timein 16 of 609, across 6 files
- Use smallest appropriate data typesin 16 of 609, across 6 files
Said here and by no other author read
- Use DB::transaction for atomic write operations
- Delete files manually if transaction fails
- Use row locks for concurrent updates
- Dispatch jobs only after transaction commit
- Use idempotency keys for retried commands
- Maintain consistent lock ordering across workflows
Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once.