Schema migrations in WordPress that never take your site down
One morning in October we deployed a new version of one of our plugins. A few minutes later the site stopped answering. Not slow: stopped. Every PHP worker was busy, every new request queued behind them, and the only way out was restarting PHP. The cause was four lines of code that almost every WordPress plugin has in some form: add_action ( 'plugins_loaded' , function ( ) { if ( get_option (…
In October, deploying a new version of a plugin led to the entire site becoming inoperable. Each PHP worker became occupied, requests were queued behind them, and the only recourse was to restart PHP. The root cause was four lines of code common in WordPress plugins: checking the plugin's database version on the 'plugins_loaded' hook and updating it if necessary.
This approach proved hazardous for a high-traffic website, as running the migration check on every request caused multiple instances of the ALTER TABLE operation to collide. One succeeded, while another failed due to a duplicate column name. Furthermore, an ALTER TABLE operation faced a metadata lock, preventing new queries from running on the affected table.
MySQL's default lock_wait_timeout is set to one year, causing all workers to become stuck and waiting for the deadlock to resolve. The post explains the dangers of this pattern and presents a new solution that prevents site downtime during deployments.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to