1

I have had ongoing trouble migrating a database in rails via rake db:migrate.

My migration currently looks like this:

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
    t.string :title
    t.text :subtitle
    t.string :slug
    t.text :body
    t.integer :publish, limit: 1, default: 0
    t.timestamps
    end
  end
end

However if ever I delete a column from this or even add or modify one the rake db:migrate command does nothing. The only way I can migrate it is sometimes running something like:

rake db:migrate VERSION=20080906120000

But even that is temperamental so most of the time I need to reset the db using

db:drop
db:create

then running the migration again as normal. basically db:migrate only ever works the first time after dropping and creating the db.

I've also tried rollback before running the migration.

This is far from ideal so I'd appreciate any help.

(I realise there are similar questions but all of their issues are solved after a db reset)

Terryfrancis
  • 135
  • 1
  • 12
  • see the link http://stackoverflow.com/questions/23563978/migration-to-create-default-value/23564092#23564092 it may be helpful – Shamsul Haque Jun 10 '14 at 14:26
  • Thanks Shamsul, I've also tried that and it works sometimes but I'm looking for a more permanent fix so I can run db:migrate normally. – Terryfrancis Jun 10 '14 at 14:29
  • Are you running the migration mulutiple times? Do you need to db:rollback and then db:migrate? – Bob McCown Jun 10 '14 at 14:22
  • Hi Bob, yes I run it multiple times as in whenever I add something or delete something from the migration. I've tried rollback before I run it a couple of times but it doesn't seem to make a difference. – Terryfrancis Jun 10 '14 at 14:24

1 Answers1

2

You need to run the migration down and then run it up again, which is done with the redo task. try this:

rake db:migrate:redo VERSION=20080906120000

EDIT: if you have data you want to keep in this table, it's better to make a new migration to remove the column or whatever you want to do, as the above will drop and recreate the table, wiping the data in the process.

Max Williams
  • 32,435
  • 31
  • 130
  • 197