Terryfrancis
Terryfrancis

Reputation: 135

Can't run a db migration on rails

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)

Upvotes: 1

Views: 1938

Answers (1)

Max Williams
Max Williams

Reputation: 32933

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.

Upvotes: 2

Related Questions