# Proper use of db:migrate and associated scripts

**URL:** <https://www.ruby-forum.com/t/proper-use-of-db-migrate-and-associated-scripts/105326>\
**Category:** Rails\
**Created:** [May 28, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/proper-use-of-db-migrate-and-associated-scripts/105326 "2007-05-28T21:14:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [May 28, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/proper-use-of-db-migrate-and-associated-scripts/105326/1 "2007-05-28T21:14:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [May 28, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/proper-use-of-db-migrate-and-associated-scripts/105326/2 "2007-05-28T21:27:07Z")

</div>

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.

Michael

---

<div class="post-metadata">

**Author:** ![Big\_Dave\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9bcb4/32.png) [@Big\_Dave\_S](https://www.ruby-forum.com/u/Big_Dave_S)\
**Post date:** [May 29, 2007, 5:09am UTC](https://www.ruby-forum.com/t/proper-use-of-db-migrate-and-associated-scripts/105326/3 "2007-05-29T05:09:43Z")

</div>

On 5/28/07, Big Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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…

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.
