<?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: Benveta</title>
    <description>The latest articles on DEV Community by Benveta (@benveta).</description>
    <link>https://dev.to/benveta</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%2Fuser%2Fprofile_image%2F4246108%2Fe753e3b9-1fa0-4377-be4c-884090325b9b.png</url>
      <title>DEV Community: Benveta</title>
      <link>https://dev.to/benveta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benveta"/>
    <language>en</language>
    <item>
      <title>Five database migrations that pass code review and still break production</title>
      <dc:creator>Benveta</dc:creator>
      <pubDate>Thu, 03 Dec 2026 08:08:51 +0000</pubDate>
      <link>https://dev.to/benveta/five-database-migrations-that-pass-code-review-and-still-break-production-38db</link>
      <guid>https://dev.to/benveta/five-database-migrations-that-pass-code-review-and-still-break-production-38db</guid>
      <description>&lt;p&gt;Code review reads the diff. Outages come from what the diff doesn't show: how big the table is, which app version is still running, and who else reads the data. Here are five changes that look harmless in a pull request, and the safe way to ship each one. The examples use Postgres, but the patterns apply to most databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Adding a NOT NULL column without a default
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On an empty dev database this works. On production, where &lt;code&gt;orders&lt;/code&gt; has rows, it fails, and your deploy stops halfway through.&lt;/p&gt;

&lt;p&gt;The obvious fix, adding a default, is fine only when the default is a constant. Since Postgres 11, &lt;code&gt;ADD COLUMN ... DEFAULT 'eu' NOT NULL&lt;/code&gt; with a constant just updates metadata and is instant. A volatile default such as &lt;code&gt;DEFAULT gen_random_uuid()&lt;/code&gt; or &lt;code&gt;clock_timestamp()&lt;/code&gt; rewrites the whole table while holding an exclusive lock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safe way:&lt;/strong&gt; add the column as nullable, or with a constant default. Backfill it in batches. Add a &lt;code&gt;CHECK (region IS NOT NULL) NOT VALID&lt;/code&gt; constraint, run &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt;, then set &lt;code&gt;NOT NULL&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Renaming or dropping a column while the old code is still running
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="no"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;full_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The migration runs, and then the rolling deploy starts. For several minutes, pods running the previous version keep sending &lt;code&gt;SELECT name FROM users&lt;/code&gt;, and every one of those queries fails. Code review looked at the new code, which is correct. The bug is in the code that is still running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safe way:&lt;/strong&gt; expand, migrate, contract. Add &lt;code&gt;full_name&lt;/code&gt; and write to both columns. Backfill it. Switch reads to &lt;code&gt;full_name&lt;/code&gt;. Stop writing &lt;code&gt;name&lt;/code&gt;. Only in a later release, drop &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating an index without CONCURRENTLY
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_orders_customer&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&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;A plain &lt;code&gt;CREATE INDEX&lt;/code&gt; takes a lock that blocks every &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; on the table until the index is built. On a large, busy table that can take minutes, so checkout stops working while reads carry on as normal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safe way:&lt;/strong&gt; &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;. It can't run inside a transaction, so your migration tool needs to run it outside one. If it fails it leaves an &lt;code&gt;INVALID&lt;/code&gt; index behind, so check for that and drop it before you retry.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Changing a column's type
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="no"&gt;TYPE&lt;/span&gt; &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most type changes rewrite the whole table while holding an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock, which blocks reads as well as writes. A few changes don't need a rewrite, such as making a &lt;code&gt;varchar&lt;/code&gt; longer or changing &lt;code&gt;varchar&lt;/code&gt; to &lt;code&gt;text&lt;/code&gt;. Many others do, including &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;bigint&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is a second trap. Even a fast &lt;code&gt;ALTER&lt;/code&gt; has to wait for that lock. If a long report query is running, your &lt;code&gt;ALTER&lt;/code&gt; waits behind it, and every new query on the table waits behind your &lt;code&gt;ALTER&lt;/code&gt;. That's an outage caused by a statement that never ran.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safe way:&lt;/strong&gt; add a new column, backfill it, then swap reads and writes over, the same way as in #2. For any DDL, set &lt;code&gt;lock_timeout&lt;/code&gt; (for example &lt;code&gt;SET lock_timeout = '3s'&lt;/code&gt;) so a blocked migration fails quickly and can be retried, instead of queuing up all your traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Removing a field that someone else consumes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; message OrderPlaced {
   string order_id = 1;
&lt;span class="gd"&gt;-  string coupon_code = 2;&lt;/span&gt;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The producer's tests pass, and the reviewer approves a tidy cleanup. But the analytics consumer, the email service and a partner webhook all read &lt;code&gt;coupon_code&lt;/code&gt;. Some of them crash, and others quietly fill reports with nulls for a week before anyone notices. The same applies to public API responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safe way:&lt;/strong&gt; treat the event or API schema as a contract. Deprecate the field first, find out who reads it, and remove it only when nothing does. In protobuf, &lt;code&gt;reserve&lt;/code&gt; the field number so it is never reused.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pattern
&lt;/h2&gt;

&lt;p&gt;All five share the same cause: the change is safe on its own, but unsafe given the state of production at that moment. That state means the table size, the app versions still running, the locks being held, and the consumers downstream. Reviewers can't see any of that in a diff, and expecting them to remember it every time doesn't scale.&lt;/p&gt;

&lt;p&gt;That's why these checks belong in the pipeline, next to your tests. A machine should be the one that says "this rewrites a 40 GB table" or "three services still read this field".&lt;/p&gt;




&lt;p&gt;&lt;em&gt;We built &lt;a href="https://benveta.com/release?utm_source=devto&amp;amp;utm_campaign=five-migrations" rel="noopener noreferrer"&gt;BenVeta Release Safety&lt;/a&gt; to do exactly this. It checks database, API and event changes in your pipeline, blocks the risky ones and explains why. The trial lasts 11 days and needs no card. We'd especially like to hear where it gets things wrong.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>devops</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
