Why this matters
Prevents partial writes and keeps data consistent under failures.
Use transactions for multi-step writes; on Throwable, rollback and rethrow with context.
Prevents partial writes and keeps data consistent under failures.
Side-by-side examples engineers can pattern-match during review.
$pdo->exec('INSERT ...'); $pdo->exec('UPDATE ...'); // no transactiontry {
$pdo->beginTransaction();
// ... multiple statements ...
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) { $pdo->rollBack(); }
throw new RuntimeException('DB transaction failed', 0, $e);
}$pdo->exec('A'); $pdo->exec('B');$pdo->beginTransaction(); /*...*/ $pdo->commit();From the same buckets as this rule.
Review SQL/database migrations for operations that can lock large tables or cause downtime. Examples: creating indexes without CONCURRENTLY (Postgres), ALTER COLUMN TYPE on big tables, adding NOT NULL without backfill, long-running updates without batching. Require an online migration strategy (CONCURRENTLY, backfill in batches, dual-write/expand-contract) and a rollback plan.