← Back to Lara
AgentLara
Laravel migrations and reliability
Plans schema changes and related writes around existing data, deployment order and recoverable transitions.
laravel-migrations
See how it helpsWhen is it useful?
Use when adding columns, constraints or transactions to a populated Laravel application.
What does it help you do?
- Separates schema evolution from bounded data backfills and later constraints.
- Chooses precise quantities, relationship behavior and indexes from business rules.
- Keeps transactions short and external effects outside unsafe write boundaries.
Try asking this
Plan a required account-reference column for existing orders, including backfill, compatibility and recovery without resetting the database. Backfill a small retained dataset and verify every reference before adding the constraint. Explain how older application instances keep writing during the transition.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Migrations & Database Reliability
Safe migrations and deployment order
- Generate migrations with
php artisan make:migration. Never modify a migration that has already been committed and deployed; create a new migration for subsequent schema changes. - In Laravel 11+, when modifying an existing column (
->change()), repeat all attributes you want to keep (nullable, default, comment, unsigned) or they may be dropped. - Take existing rows and deployment order into account: when adding a required column to a populated table, add it as
nullable()(or with a safedefault()), backfill existing rows in batches, and applyNOT NULLonly once every row is populated. - Keep schema DDL and data transformations separate; never instantiate Eloquent models with evolving scopes or casts inside an old schema migration.
- Never run
migrate:fresh,migrate:refresh,migrate:resetordb:wipeon an unverified or shared database.
Keys, types and indexes
- Use
foreignId('user_id')->constrained()with an explicitcascadeOnDelete(),nullOnDelete()orrestrictOnDelete()that matches the domain rule. - Store money and exact quantities as
decimal(10, 2)or integer minor units (unsignedBigInteger), neverfloatordouble. - Add single or composite indexes for columns used in
WHERE,JOINandORDER BY(putting equality columns before range columns in composite indexes), and add unique constraints where business rules require uniqueness.
Transactions, locks and side effects
- Wrap related multi-table writes in
DB::transaction(fn () => ...)and keep the transaction short. Never make external HTTP calls, file uploads or slow operations while holding a transaction open. - Prevent race conditions and lost updates with row locks (
->lockForUpdate()inside a transaction), atomic increments/decrements, unique constraints orCache::lock(), never arbitrarysleep()delays. - Dispatch jobs, events, mails and webhooks after the transaction commits (
DB::afterCommit()orShouldDispatchAfterCommit) so external consumers never observe rolled-back state.
Included files 2
LICENSE.txtSKILL.md
