← Back to Lara
AgentLara

Skill · Version 1.0.0

Laravel migrations and reliability

Plans schema changes and related writes around existing data, deployment order and recoverable transitions.

laravel-migrations

See how it helps

When 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.

Details…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 safe default()), backfill existing rows in batches, and apply NOT NULL only 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:reset or db:wipe on an unverified or shared database.

Keys, types and indexes

  • Use foreignId('user_id')->constrained() with an explicit cascadeOnDelete(), nullOnDelete() or restrictOnDelete() that matches the domain rule.
  • Store money and exact quantities as decimal(10, 2) or integer minor units (unsignedBigInteger), never float or double.
  • Add single or composite indexes for columns used in WHERE, JOIN and ORDER 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 or Cache::lock(), never arbitrary sleep() delays.
  • Dispatch jobs, events, mails and webhooks after the transaction commits (DB::afterCommit() or ShouldDispatchAfterCommit) so external consumers never observe rolled-back state.

Included files 2

  • LICENSE.txt
  • SKILL.md