惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
量子位
博客园 - 三生石上(FineUI控件)
I
InfoQ
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
D
Docker
美团技术团队
雷峰网
雷峰网
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Zero-Downtime Database Migrations in Laravel
Prajapati Paresh · 2026-06-26 · via DEV Community

The Deployment Crash

In the early stages of a B2B SaaS platform at Smart Tech Devs, deploying a database schema change is simple: you put the app in maintenance mode, run php artisan migrate, deploy the new code, and bring the app back online. But as you scale to thousands of active enterprise users, a 2-minute maintenance window becomes unacceptable. You must deploy with zero downtime.

The architectural trap occurs when you try to rename or drop a database column while the app is live. If you rename first_name to full_name, there is a physical fraction of a second where the database has the new column name, but your server is still running the old PHP code. During that window, any user attempting to register will trigger a fatal 500 SQL error because the old code is trying to insert into a column that no longer exists. To deploy safely, you must decouple database changes from code changes using the Expand and Contract Pattern.

The Solution: The Expand and Contract Pattern

Instead of mutating a column in a single destructive step, we stretch the migration across multiple independent, safe deployment phases.

Phase 1: Expand (Add without deleting)

First, we create a migration that adds the new full_name column, but we DO NOT drop or rename the old first_name column. We also update our Laravel Model to write data to both columns simultaneously.


// 1. The Migration
Schema::table('users', function (Blueprint $table) {
    $table->string('full_name')->nullable(); // Add the new column safely
});

// 2. The Model Update
class User extends Model
{
    // Write to both columns during the transition period to keep data in sync
    protected static function booted()
    {
        static::saving(function ($user) {
            if ($user->isDirty('first_name') || $user->isDirty('last_name')) {
                $user->full_name = $user->first_name . ' ' . $user->last_name;
            }
        });
    }
}

Deploy this. The application remains perfectly online. New users now have data in both columns.

Phase 2: Migrate (Backfill old data)

Now that our application is writing to both columns, we need to backfill the old historical records. We dispatch a background Queue Job or run an Artisan command to quietly update the old rows without locking the table.


// Run in the background via queues or Laravel Prompts
User::whereNull('full_name')->chunkById(500, function ($users) {
    foreach ($users as $user) {
        $user->update(['full_name' => $user->first_name . ' ' . $user->last_name]);
    }
});

Phase 3: Contract (Cleanup)

Days or weeks later, once you have confirmed the new full_name logic is working flawlessly across your entire dashboard, you write a final migration to safely drop the old columns. You also remove the duplicate writing logic from your Eloquent model.


// The Final Cleanup Migration
Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['first_name', 'last_name']);
});

The Engineering ROI

By breaking destructive schema mutations into an Expand, Migrate, and Contract workflow, you completely eliminate deployment downtime. Your database and your application code are never in a conflicting state, allowing you to execute massive architectural refactors seamlessly in the middle of peak traffic hours without dropping a single user request.