<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Transtoyou</title>
    <description>The latest articles on DEV Community by Transtoyou (transtoyou).</description>
    <link>https://dev.to/transtoyou</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F16520%2F09d4d975-7a5d-43b5-8cc5-e6cf78f0371a.png</url>
      <title>DEV Community: Transtoyou</title>
      <link>https://dev.to/transtoyou</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/transtoyou"/>
    <language>en</language>
    <item>
      <title>Schema migrations in WordPress that never take your site down</title>
      <dc:creator>Roel van Distel</dc:creator>
      <pubDate>Sat, 28 Nov 2026 01:52:17 +0000</pubDate>
      <link>https://dev.to/transtoyou/schema-migrations-in-wordpress-that-never-take-your-site-down-2abe</link>
      <guid>https://dev.to/transtoyou/schema-migrations-in-wordpress-that-never-take-your-site-down-2abe</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The cause was four lines of code that almost every WordPress plugin has in some form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'plugins_loaded'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;get_option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'my_plugin_db_version'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'1.30.0'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;my_plugin_create_tables&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// dbDelta, ALTER TABLE&lt;/span&gt;
        &lt;span class="nf"&gt;update_option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'my_plugin_db_version'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1.30.0'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This post is about why that pattern is dangerous on a busy site, and the small runner we replaced it with. We run &lt;a href="https://www.transtoyou.com" rel="noopener noreferrer"&gt;Transtoyou&lt;/a&gt;, an online health platform in 14 languages on one WordPress multisite, so a broken deploy means patients who cannot reach a doctor. But nothing here is specific to health. If your plugin checks its database version on &lt;code&gt;plugins_loaded&lt;/code&gt;, this is about you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually happened
&lt;/h2&gt;

&lt;p&gt;The check above runs on every request. Right after a deploy, the stored version is old, so the first request starts the migration. So does the second one, a few milliseconds later, because the first one has not written the new version yet. On a site with traffic, "the first request after a deploy" is really "the first twenty requests after a deploy".&lt;/p&gt;

&lt;p&gt;Two things then went wrong at once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two requests ran the same &lt;code&gt;ALTER TABLE&lt;/code&gt;.&lt;/strong&gt; One succeeded, the other failed with &lt;code&gt;Duplicate column name&lt;/code&gt;. Annoying, but survivable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An &lt;code&gt;ALTER TABLE&lt;/code&gt; waited on a metadata lock.&lt;/strong&gt; To change a table, MySQL needs an exclusive metadata lock on it. If any other connection is in the middle of a transaction that touched that table, the &lt;code&gt;ALTER&lt;/code&gt; waits. And while it waits, every new query on that table queues behind it, because MySQL will not let new readers jump ahead of a waiting writer.&lt;/p&gt;

&lt;p&gt;How long does it wait? That is set by &lt;code&gt;lock_wait_timeout&lt;/code&gt;, and the default is 31,536,000 seconds. One year.&lt;/p&gt;

&lt;p&gt;So one PHP worker sat waiting on the &lt;code&gt;ALTER&lt;/code&gt;, and every following request that touched the table sat waiting behind it. Within a minute all workers were stuck. Nothing failed loudly. Everything just waited.&lt;/p&gt;

&lt;p&gt;When we went looking, we found the same pattern in about forty installers across our own plugins.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rules we wrote down
&lt;/h2&gt;

&lt;p&gt;Before writing any code, we agreed on what a migration is allowed to do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A visitor's request never changes the schema.&lt;/strong&gt; Not a page view, not an AJAX call. Migrations run from WP-CLI, from cron, or from someone opening wp-admin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One at a time.&lt;/strong&gt; If a migration is running, everyone else skips it and moves on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Waiting is bounded.&lt;/strong&gt; If the &lt;code&gt;ALTER&lt;/code&gt; cannot get its lock within a few seconds, it gives up and logs it, instead of holding a worker for a year.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The version is recorded only when the schema is really there.&lt;/strong&gt; Not when the installer finished, but when a check confirms the new columns exist.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  One runner for every migration
&lt;/h2&gt;

&lt;p&gt;All forty installers now go through one function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;tty_schema_migrate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$install&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$args&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$key&lt;/code&gt; is the option that stores the version, &lt;code&gt;$version&lt;/code&gt; is the version this code needs, &lt;code&gt;$install&lt;/code&gt; is the callable that runs &lt;code&gt;dbDelta()&lt;/code&gt; or &lt;code&gt;ALTER TABLE&lt;/code&gt;. Here is what it does, in order.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Is there anything to do?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$needed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;get_site_option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nv"&gt;$version&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$needed&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'current'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the only part that runs on every request, and it is a single cached option read.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. May this request migrate?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;tty_schema_migration_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;defined&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'WP_CLI'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;WP_CLI&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;wp_doing_cron&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;is_admin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nf"&gt;wp_doing_ajax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;wp_doing_ajax()&lt;/code&gt; exclusion is the subtle one. &lt;code&gt;admin-ajax.php&lt;/code&gt; counts as &lt;code&gt;is_admin()&lt;/code&gt;, but on most sites it also serves front-end requests: dashboards, carts, anonymous pings. Treat it as a visitor.&lt;/p&gt;

&lt;p&gt;If the request may not migrate, the runner returns &lt;code&gt;'skipped'&lt;/code&gt; and the page carries on with the old schema. That means the code that uses a new column has to cope with that column not being there yet, which is a good habit anyway: during a deploy, old and new code briefly run side by side.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Take the lock
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'tty_schema_lock_'&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;md5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;get_site_transient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$lock&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'locked'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;set_site_transient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$lock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The lock is set &lt;strong&gt;before&lt;/strong&gt; the installer runs and removed only after success. If the installer fails, the lock simply expires after two minutes and the next eligible request tries again. A failed migration is retried, but never in parallel.&lt;/p&gt;

&lt;p&gt;A fair objection: a transient read followed by a write is not atomic. Two requests can, in theory, both see "no lock" in the same instant. In practice this is much less likely than before, because the window is now only the eligible requests (cron, admin, CLI) instead of every page view. If you need a strict guarantee, MySQL's &lt;code&gt;GET_LOCK()&lt;/code&gt; is atomic and disappears with the connection. We use it elsewhere for exactly that reason.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Bound the wait
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$previous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get_var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SELECT @@SESSION.lock_wait_timeout'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SET SESSION lock_wait_timeout = 5'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call_user_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$install&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SET SESSION lock_wait_timeout = '&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;intval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$previous&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the line that would have saved our morning. With a five-second limit, an &lt;code&gt;ALTER&lt;/code&gt; that cannot get its metadata lock fails with an error after five seconds. One log line, one worker freed, and the migration tries again two minutes later, when the long transaction that held the table is probably gone.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; matters: the setting is per session, and the same connection serves the rest of the request.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Check before you record
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nf"&gt;is_callable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'ready'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;call_user_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'ready'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$ready&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;update_site_option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$version&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;delete_site_transient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$lock&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'done'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;error_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s2"&gt;"[Schema] {$key}: version {$version} not in place yet, retrying later."&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;dbDelta()&lt;/code&gt; fails silently more often than you would like. We had a table that never existed on production for weeks, because one column was called &lt;code&gt;lines&lt;/code&gt;, a reserved word in MySQL, while the plugin had happily recorded its version. The &lt;code&gt;ready&lt;/code&gt; callback asks the database whether the table and the newest column are actually there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'ready'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$wpdb&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get_var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;"SHOW COLUMNS FROM {$wpdb-&amp;gt;prefix}my_table LIKE 'cycle_set_by'"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No column, no version, and the next run tries again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running it after a deploy
&lt;/h2&gt;

&lt;p&gt;Because a visitor never migrates, something else has to. We added one WP-CLI command to our deploy routine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wp tty-schema run --all-sites
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It starts a fresh process per site of the network. Each process boots WordPress normally, so every plugin calls the runner exactly as it would in wp-admin, and the command prints what each migration did: &lt;code&gt;current&lt;/code&gt;, &lt;code&gt;done&lt;/code&gt;, &lt;code&gt;locked&lt;/code&gt; or &lt;code&gt;failed&lt;/code&gt;. It exits with a non-zero code when anything failed, so the deploy notices.&lt;/p&gt;

&lt;p&gt;If nobody runs it, the schema still arrives the first time an administrator opens wp-admin or cron runs. It just never arrives in the middle of a patient's checkout.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one exception
&lt;/h2&gt;

&lt;p&gt;Some code needs a table right now. For example: "store this event, and create the log table first if it does not exist". Waiting for cron is not an option there. For those paths the runner takes &lt;code&gt;'gate' =&amp;gt; false&lt;/code&gt;: it skips the "may this request migrate" check, but still uses the lock, the five-second wait and the &lt;code&gt;ready&lt;/code&gt; check. Lazy, but safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed
&lt;/h2&gt;

&lt;p&gt;Since every migration goes through the runner, a deploy that changes a table is boring. The migration runs once, from the command line, with a bounded wait, and we see the outcome. A visitor never waits on an &lt;code&gt;ALTER TABLE&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;If you maintain a plugin, three things are worth checking today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does your version check run its installer on &lt;code&gt;plugins_loaded&lt;/code&gt; or &lt;code&gt;init&lt;/code&gt; for every request?&lt;/li&gt;
&lt;li&gt;Do you record the new version without checking that the schema is really there?&lt;/li&gt;
&lt;li&gt;What is &lt;code&gt;lock_wait_timeout&lt;/code&gt; on your production database?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The third one surprised us most.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;We build &lt;a href="https://www.transtoyou.com" rel="noopener noreferrer"&gt;Transtoyou&lt;/a&gt;, an online doctor and pharmacy service for patients across Europe and the UK. Patients answer a questionnaire or book a video call, an independent doctor reviews it, and a licensed partner pharmacy delivers (&lt;a href="https://www.transtoyou.com/how-we-work/" rel="noopener noreferrer"&gt;how it works&lt;/a&gt;). Questions about the runner are welcome in the comments.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>php</category>
      <category>database</category>
      <category>devops</category>
    </item>
    <item>
      <title>Running a 14-language health platform from one codebase: what broke and what we changed</title>
      <dc:creator>Roel van Distel</dc:creator>
      <pubDate>Fri, 27 Nov 2026 22:53:06 +0000</pubDate>
      <link>https://dev.to/transtoyou/running-a-14-language-health-platform-from-one-codebase-what-broke-and-what-we-changed-27am</link>
      <guid>https://dev.to/transtoyou/running-a-14-language-health-platform-from-one-codebase-what-broke-and-what-we-changed-27am</guid>
      <description>&lt;p&gt;&lt;a href="https://www.transtoyou.com" rel="noopener noreferrer"&gt;Transtoyou&lt;/a&gt; is an online health platform for patients across Europe and the UK. Patients answer a medical questionnaire or book a video consultation, an independent doctor reviews the request (&lt;a href="https://www.transtoyou.com/how-we-work/" rel="noopener noreferrer"&gt;how it works&lt;/a&gt;), and a licensed partner pharmacy delivers the treatment. It runs in 14 languages, on one WordPress multisite with our own theme and plugins.&lt;/p&gt;

&lt;p&gt;This post is not about which plugin to install. It is about the rules we had to learn the hard way when one codebase has to behave correctly for 14 languages, a dozen delivery countries and several currencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. One rule, one place
&lt;/h2&gt;

&lt;p&gt;The most expensive bugs we had were never in a single function. They were two places that answered the same question differently.&lt;/p&gt;

&lt;p&gt;Example: "can this medicine be delivered to this country?" (our &lt;a href="https://www.transtoyou.com/shipping-info/" rel="noopener noreferrer"&gt;delivery countries and times&lt;/a&gt; depend on it) At one point the product page, the cart, the checkout, the search and the pharmacy assignment each had their own version of that answer. Four places, four slightly different rules. A patient could add a product to the cart that the checkout later refused.&lt;/p&gt;

&lt;p&gt;We now have one reader per question. The product page, the cart, the checkout and the search all call the same function, and a test fails when a new call site tries to answer the question itself. It sounds obvious. It is the single most valuable rule in our codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Never derive meaning from a translated string
&lt;/h2&gt;

&lt;p&gt;On a multilingual site, text is not data. We had code that matched a menu item by its title. It worked in English and silently failed in ten other languages, because every language site translates its menu.&lt;/p&gt;

&lt;p&gt;The fix is always the same: match on something that does not get translated. An icon class, a slug from the main site, an ID in a translation group. If you ever write &lt;code&gt;if ( $title === 'Something' )&lt;/code&gt; on a multilingual site, stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Counts break grammar
&lt;/h2&gt;

&lt;p&gt;"30 days" looks harmless until you translate it. In Romanian you write "30 de zile" but "14 zile". In Polish and Lithuanian the noun changes with the number. A single stored string like &lt;code&gt;%d days&lt;/code&gt; cannot be right for every number in every language.&lt;/p&gt;

&lt;p&gt;What worked for us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put the number where no noun depends on it: "Days: 30" instead of "30 days".&lt;/li&gt;
&lt;li&gt;Use real plural forms (&lt;code&gt;_n()&lt;/code&gt; in WordPress) where you can.&lt;/li&gt;
&lt;li&gt;For the few cases where a number sits in the middle of a sentence, a small helper that knows the rule for that language.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Caches and language are a dangerous mix
&lt;/h2&gt;

&lt;p&gt;A page cache serves the same HTML to everyone. If anything on the page depends on the visitor's country, currency or language, the first visitor decides what everyone else sees.&lt;/p&gt;

&lt;p&gt;Our rule: a page from the cache never reads a cookie or a session on the server. It renders for the country the site stands for, and a small script corrects prices and availability for the visitor afterwards. Anything else ends up showing a Danish price to a German visitor.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Hreflang only works if every language agrees
&lt;/h2&gt;

&lt;p&gt;We had pages that announced their language versions correctly on one site and not on another, which search engines read as conflicts. The lesson: build the list of language versions from one shared source (a translation group table), and print it only on the clean URL of a page, never on a filtered or parameterised view.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Make migrations boring
&lt;/h2&gt;

&lt;p&gt;Schema changes used to run on the first request after a deploy. Once, two requests started the same &lt;code&gt;ALTER TABLE&lt;/code&gt; at the same moment, and the migration waited on a lock long enough to fill every PHP worker.&lt;/p&gt;

&lt;p&gt;Now every migration goes through one runner: it only runs from the command line, cron or the admin, takes a lock, sets a short lock wait, and records its version only after a check that the new columns really exist. A visitor's request never migrates anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell my younger self
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Write the rule once and make the tests guard that it stays once.&lt;/li&gt;
&lt;li&gt;Treat every translated string as something that will be different tomorrow.&lt;/li&gt;
&lt;li&gt;Assume the cache will show your page to the wrong person, and design for it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this is specific to health. But in health, a wrong answer on a product page is not just a bug. It is a promise to a patient that a pharmacy cannot keep.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm Roel, founder of &lt;a href="https://www.transtoyou.com" rel="noopener noreferrer"&gt;Transtoyou&lt;/a&gt;. Happy to answer questions in the comments.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>php</category>
      <category>softwaredevelopment</category>
      <category>wordpress</category>
    </item>
  </channel>
</rss>
