Proper use of db:migrate and associated scripts

Hi community,

I'm a fairly new developer. I created a project in rails to learn with, and created a model for 'computer'. I edited the appropriate 001_create_computer.rb migration file, and as part of it, included a line: t.column :cost, :decimal

Later, learning more about rails, I went back and changed the line to look like this: t.column :cost, :decimal, :precision => 8, :scale => 2, :default => 0

What I can't figure out is, what do I do to rebuild the database? rake db:migrate seems to ignore the old 001 file. I assume I need to do something like rake db:rebuild, to go back and start from scratch and apply all the migration scripts, but I can't find the appropriate command. If anyone knows what step or steps I need to take, I would greatly appreciate it.

Also, is this the proper way to make such a change? I think I could add another migration file, 005-blah-blah.rb, and change the column there, but I see no value in keeping the original column declaration since it is essentially incomplete. Thank you for your advice!

When you are in development you can edit the migrations and rebuild the database. For production you should always create new migrations that modify the existing database of course.

rake db:migrate VERSION=0 rake db:migrate

The above sequence will drop all your tables and rebuild the database. You can also use variations on this to backup and restore part of the schema to test upgrades of production databases.


Dave, here a a couple of tasks from my template lib/tasks/db.rake file. Hope they help you out.

namespace :db do   desc "Empty the current database"   task :purge => :environment do     return unless %w[development test staging].include? RAILS_ENV     config = ActiveRecord::Base.configurations[RAILS_ENV]     ActiveRecord::Base.connection.recreate_database(config['database'])     ActiveRecord::Base.establish_connection   end

  desc "Migrate with catlike tread"   task :migrate_quietly => :environment do     ActiveRecord::Migration.verbose = false     Rake::Task['db:migrate'].invoke   end

  desc "Empty and recreate the current database"   task :remigrate => [:purge, :migrate_quietly] end

A few of the tasks recently committed to edge would make these easier/obsolete, too.

~ j.