<p>Experienced with Rails / ActiveRecord 2.1.1</p>

<ul>
<li>You create a first version with (for example) ruby script\generate scaffold product title:string description:text image_url:string</li>
<li>This create (for example) a migration file called 20080910122415_create_products.rb</li>
<li>You apply the migration with rake db:migrate</li>
<li>Now, you add a field to the product table with ruby script\generate migration add_price_to_product price:decimal</li>
<li>This create a migration file called 20080910125745_add_price_to_product.rb</li>
<li>If you try to run rake db:migrate, it will actually revert the first migration, not apply the next one! So your product table will get destroyed!</li>
<li>But if you ran rake alone, it would have told you that one migration was pending</li>
</ul>

<p>Pls note that applying rake db:migrate (once the table has been destroyed) will apply all migrations in order.</p>

<p>The only workaround I found is to specify the version of the new migration as in: </p>

<pre><code>rake db:migrate version=20080910125745
</code></pre>

<p>So I'm wondering: is this an expected new behavior?</p>

[{'answer_id': 70771, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 2, 'selected': True, 'text': '<p>You should be able to use </p>\n\n<pre><code>rake db:migrate:up \n</code></pre>\n\n<p>to force it to go forward, but then you risk missing interleaved migrations from other people on your team</p>\n\n<p>if you run </p>\n\n<pre><code>rake db:migrate \n</code></pre>\n\n<p>twice, it will reapply all your migrations.</p>\n\n<p>I encounter the same behavior on windows with SQLite, it might be a bug specific to such an environment.</p>\n\n<p><strong>Edit</strong> -- I found why. In the railstie database.rake task you have the following code :</p>\n\n<pre><code>desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."\ntask :migrate =&gt; :environment do\n  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true\n  ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)\n  Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby\nend\n</code></pre>\n\n<p>Then in my environment variables I have </p>\n\n<pre><code>echo %Version% #=&gt; V3.5.0f\n</code></pre>\n\n<p>in Ruby</p>\n\n<pre><code>ENV["VERSION"] # =&gt; V3.5.0f\nENV["VERSION"].to_i #=&gt;0 not nil !\n</code></pre>\n\n<p>thus the rake task calls </p>\n\n<pre><code>ActiveRecord::Migrator.migrate("db/migrate/", 0)\n</code></pre>\n\n<p>and in ActiveRecord::Migrator we have : </p>\n\n<pre><code>class Migrator#:nodoc:\n  class &lt;&lt; self\n    def migrate(migrations_path, target_version = nil)\n      case\n        when target_version.nil?              then up(migrations_path, target_version)\n        when current_version &gt; target_version then down(migrations_path, target_version)\n        else                                       up(migrations_path, target_version)\n      end\n    end\n</code></pre>\n\n<p>Yes, <code>rake db:migrate VERSION=0</code> is the long version for <code>rake db:migrate:down</code> </p>\n\n<p><strong>Edit</strong> - I would go update the lighthouse bug but I the super company proxy forbids that I connect there</p>\n\n<p>In the meantime you may try to unset Version before you call migrate ...</p>\n'}, {'answer_id': 70830, 'author': 'tomafro', 'author_id': 7126, 'author_profile': 'https://Stackoverflow.com/users/7126', 'pm_score': 0, 'selected': False, 'text': '<p>This is not the expected behaviour.  I was going to suggest reporting this as a bug on lighthouse, but I see you\'ve <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1021-rake-dbmigrate-doesnt-detect-new-migration" rel="nofollow noreferrer">already done so</a>!  If you provide some more information (including OS/database/ruby version) I will take a look at it. </p>\n'}, {'answer_id': 71016, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 0, 'selected': False, 'text': '<p>I respectfully disagree Tom! this <em>is</em> a bug !! V3.5.0f is not a valid version for rake migrations. Rake should not use it to migrate:down just because ruby chose to consider that "V3.5.0f".to_i is 0 ... </p>\n\n<p>Rake should loudly complain that VERSION is not valid so that users know what is up\n(between you and me, checking that the version is a YYYYMMDD formated timestamp by converting to integer is a bit light)</p>\n\n<p>[Damn IE6 that won\'t allow me to comment ! and no I can\'t change browser thanks corporate]</p>\n'}, {'answer_id': 71104, 'author': 'Rollo Tomazzi', 'author_id': 11477, 'author_profile': 'https://Stackoverflow.com/users/11477', 'pm_score': 0, 'selected': False, 'text': '<p>Jean,</p>\n\n<p>Thanks a lot for your investigation. You\'re right, and actually I think you\'ve uncovered a more severe bug, of species \'design bug\'.</p>\n\n<p>What\'s happening is that rake will grab whatever value you pass to the command line and store them as environment variables. The rake tasks that will eventually get called will just pull this values from the environment variable.\nWhen db:migrate queries ENV["VERSION"], it actually requests the version parameter which you set calling rake. When you call rake db:migrate, you don\'t pass any version.</p>\n\n<p>But we do have an environment variable called VERSION that has been set for other purposes by some other program (I don\'t which one yet). And the guys behind rake (or behind database.rake) haven\'t figured this would happen. That\'s a design bug. At least, they could have used more specific variable names like "RAKE_VERSION" or "RAKE_PARAM_VERSION" instead of just "VERSION".</p>\n\n<p>Tom, I will definitely not close but edit my bug report on lighthouse to reflect these new findings.</p>\n\n<p>And thanks again Jean for your help. I\'ve posted this bug on lighthouse like 5 days agao and still got no answer!</p>\n\n<p>Rollo</p>\n'}]
['https://Stackoverflow.com/questions/70318', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11477/']