0

I'm doing the Michael Hartl Ruby on Rails Tutorial, and ran into this problem at the end of Chapter 11.

I did a migration to add activation_digest, activated, and activated_at columns to my data model.

$ rails generate migration add_activation_to_users \
> activation_digest:string activated:boolean activated_at:datatime

As you can see, I wrote datatime instead of datetime. Now my migration file looks like this:

class AddActivationToUsers < ActiveRecord::Migration[5.1]
  def change
    add_column :users, :activation_digest, :string
    add_column :users, :activated, :boolean, default: false
    add_column :users, :activated_at, :datatime
  end
end

Can I simply fix this error by editing the migration file? Or should I re-run the migration at the command line? Is there a better way to do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

2 Answers2

1
rake db:rollback 

will rollback the migration, then you can fix the typo and run

rake db:migrate 

again to re run the migration.

Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • Essentially correct, but if they are using a later version: it's: `rails db:rollback` and `rails db:migrate`. Corrected to say whichever command they are using is the best. – Greg Mar 12 '18 at 00:58
  • Really? I still use `rake` – Rockwell Rice Mar 12 '18 at 00:59
  • 1
    According this [this answer](https://stackoverflow.com/questions/38403533/rails-dbmigrate-vs-rake-dbmigrate), it seems like `rails db:migrate` is in fact delegated to `rake db:migrate`. So, why is `rake db:migrate` "essentially correct" and not "correct"? – jvillian Mar 12 '18 at 01:03
  • https://www.sitepoint.com/onwards-to-rails-5-additions-changes-and-deprecations/ "Another cool feature of Rails 5 is that all `rake` commands now live in `rails` (for example, `rake db:migrate` becomes `rails db:migrate`)." Not mandatory yet (or ever?), but since this was confusing and apparently an unneeded distinction it's all `rails` now. – Greg Mar 12 '18 at 01:07
  • 1
    If I'm reading it correctly (and I may not be), it seems that rails is essentially *wrapping* rake tasks (via the `Rake Proxy`). So, IMO it's a little misleading to say that all rake commands now "live" in rails. Rake tasks live where they have always lived. I appreciate that this is meant to make things less confusing (and it may or may not, I don't know). I guess I don't see how that all makes the answer less correct. Perhaps less fashionable... – jvillian Mar 12 '18 at 01:18
  • This fixed it. Your help is much appreciated. – Mia Genovese Mar 12 '18 at 17:06
0

You can edit the migration file before you run the migration.

jljohnstone
  • 1,202
  • 1
  • 10
  • 14
  • 1
    Are you sure the migration ran successfully? With an invalid datatype `:datatime`, it should not have been able to run and you shouldn't have to rollback the migration. Rolling back undoes the last successful migration and I doubt that's what you actually want. You should be able to edit the migration file and try to run the migration again. – jljohnstone Mar 12 '18 at 01:51