<?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: Savannah Fischer</title>
    <description>The latest articles on DEV Community by Savannah Fischer (@savfischer).</description>
    <link>https://dev.to/savfischer</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F75640%2Fe7a6f0c1-dc8c-4f62-b6e2-7c9378a16cbd.png</url>
      <title>DEV Community: Savannah Fischer</title>
      <link>https://dev.to/savfischer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/savfischer"/>
    <language>en</language>
    <item>
      <title>Fixing a Rails Migration Snafu</title>
      <dc:creator>Savannah Fischer</dc:creator>
      <pubDate>Wed, 13 Jun 2018 20:21:04 +0000</pubDate>
      <link>https://dev.to/savfischer/fixing-a-rails-migration-snafu-1fm6</link>
      <guid>https://dev.to/savfischer/fixing-a-rails-migration-snafu-1fm6</guid>
      <description>&lt;p&gt;I had been working on a huge new feature for a Rails app with existing customers for about two months. This new feature had about 10 new database migrations, including rolling back and editing the last migration that had been pushed to production and renaming those tables. That was a mistake. If, two months later, I had remembered to rollback the most recent migration in production before performing the new migrations, all would have been well.&lt;/p&gt;

&lt;p&gt;Alas, I did not. Obviously, any programming change that requires you remember to do something 2 months from now is bad, especially if you have no system to remind yourself before deploying. When I did push the feature to production, my new migrations ran fine and two days laters I started getting errors about how a database table doesn't exist when my code assumes it does. It didn't take me long to realize that it was the migration about 10 migration files back that was the source of the error where I had rolled back in development and renamed the tables, but I had not rolled back in production.&lt;/p&gt;

&lt;p&gt;Worse, I had a bunch of data live in production now, that depended on tables created in migrations after the problem migration file. I was not okay losing that data, but the only solution to my problem that I knew about required it. Basically, I was familiar with the basics of migrations through the &lt;a href="http://edgeguides.rubyonrails.org/active_record_migrations.html"&gt;Rails ActiveRecord migrations guide&lt;/a&gt;. I thought I'd have to push the old version of that migration file to production, rollback 10 migrations (losing all my new data), push the new version of the migration file and run db:migrate one more time.&lt;/p&gt;

&lt;p&gt;Thankfully, in my desperate googling for a solution, I ran into &lt;a href="https://stackoverflow.com/questions/753919/run-a-single-migration-file"&gt;this question and answer&lt;/a&gt;, which suggested a path forward. When I realized you could run a single migration up or down with the version option, it became clear. I had to push the old version of that migration to production and then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:migrate:down VERSION=20180506181554
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then push the new version of that migration and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:migrate:up VERSION=20180506181554
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The version, by the way, is just the timestamp in the filename rails generates automatically if you are using &lt;code&gt;rails generate migration&lt;/code&gt;. Knowing how to reverse and rerun a specific migration file is a huge relief. Its comforting to know getting migrations out of sync in production doesn't necessarily have to blow everything up, but has the potential to be dealt with as an isolated problem. Thanks Rails!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
