agentleFS
soden46soden46/syarif-laravel-ai-skills/skills/database-transactions/SKILL.md

database-transactions

Skill4 starsChanged 5 months agoMITView on GitHub

Keep Laravel writes atomic and consistent with transactions, locks, retries, idempotency, side-effect boundaries, and after-commit dispatch.

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 8 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use database transactions for write operations that must be atomic.

    line 11
  2. Very specific:

    Use DB::transaction() when a workflow:

    line 19
  3. Distinctive:

    must not partially succeed.

    line 25
  4. Distinctive:

    Use row locks for concurrent updates to shared counters, balances, or ordered state.

    line 72
  5. Distinctive:

    Keep transactions short. Make retry behavior idempotent when deadlock retries are used.

    line 85
  6. Distinctive:

    Keep lock ordering consistent across workflows to reduce deadlocks.

    line 98
  7. Distinctive:

    Use optimistic checks when conflicts should be reported rather than serialized.

    line 99
  8. Distinctive:

    Prefer explicit compensating behavior for filesystem or provider operations that cannot participate in the database transaction.

    line 100
  9. Distinctive:

    after-commit behavior for important workflows.

    line 109

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.