Database Migration
I need to add a phone column to our user table, but production already has tens of thousands of rows. How do we update the schema without losing data or causing downtime?
I need to add a phone column to our user table, but production already has tens of thousands of rows. How do we update the schema without losing data or causing downtime?
NULLNULLNULLmigration history: 0001_initInspect existing data before choosing the migration order; a rejected change is not a database crash.
Existing rows may not satisfy a new required constraint: Old rows in users have no status value. Enforcing status NOT NULL immediately can fail while those rows remain empty. Even if the database supports a default during the change, the value must reflect a real business rule rather than a guess.
Stay compatible, backfill, then tighten: First add a nullable status column so old and new application versions can keep running. Next backfill valid values under an explicit rule and record the affected rows. Only after NULL count reaches zero should the migration enforce NOT NULL.
Verify data results and old/new application behavior: Check the migration version, log, affected-row count, and remaining NULL values; any NULL means enforcement must wait. In staging, also confirm existing users can be read, new users receive valid status values, and both application versions work during transition. Migration history is not a backup, so destructive changes still need a recovery plan.
The users table already has live rows and needs a required status column. First inspect the migration tool, current values, and old-app compatibility. Then add a nullable column, backfill valid values, verify NULL count is zero, and finally enforce NOT NULL. Run it in staging and report the version, affected rows, NULL check, and old/new app read-write results. Do not treat a down migration as a backup.