# Migrations and schema.rb - where did I go wrong?

**URL:** <https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441>\
**Category:** Rails\
**Created:** [March 20, 2008, 5:51am UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441 "2008-03-20T05:51:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [March 20, 2008, 5:51am UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441/1 "2008-03-20T05:51:00Z")

</div>

We’re a group of 3 developers and eager newcomers to Ruby  
collaborating on a project.

I started a baseline implementation with a few migrations and models a  
few weeks ago.

For the first time, we integrated some code… I did the  
integration… a few models were changed so a few migrations got  
deleted… and a few models were added, so I copied them over before  
checking into svn.

Question 1:

Now our migration numbers are not contiguous so we got 001, 003, 006,  
007, 008 and 009. Is this emblematic of a problem? Should these  
always be contiguous?

Question 2:

When I copied the new migrations over (and deleted the others), I  
ended with a schema.rb was out of date. I had to hand tweak it for  
db:migrate to work (added blocks for the new models and deleted the  
old one.

The comments on top of migrate.db read "# This file is autogenerated.  
Instead of editing this file, please use the

# migrations feature of ActiveRecord to incrementally modify your

database, and

# then regenerate this schema definition."

What did I do wrong that I had to head-tweak schema? How should this  
integration been done to regenerate the schema? Since we’re very early  
in the development process, we don’t have a database to preserve, per  
se.

Thanks.

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [March 20, 2008, 7:32am UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441/2 "2008-03-20T07:32:26Z")

</div>

when you migrate and migration change.  
You should always do:

rake db:migrate VERSION=0  
This will clean out your database.

and then

rake db:migrate  
This will update your rake to the newest version.

If you have data in your project that you would like to keep after  
this.  
Eather make a backup of the date.  
Or create a fixture, with a .yml fil, with the contense.

I would recormmned, that you renamed your files (migration) , to  
reflect the the correct order, and so that the order dosn’t have any  
gaps.  
001  
002  
003  
003  
and so on

regards  
svend

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [March 20, 2008, 7:38am UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441/3 "2008-03-20T07:38:20Z")

</div>

when you migrate and migration change.  
You should always do:

rake db:migrate VERSION=0  
This will clean out your database.

and then

rake db:migrate  
This will update your rake to the newest version.

If you have data in your project that you would like to keep after  
this.  
Eather make a backup of the date.  
Or create a fixture, with a .yml fil, with the contense.

I would recormmned, that you renamed your files (migration) , to  
reflect the the correct order, and so that the order dosn’t have any  
gaps.  
001  
002  
003  
003  
and so on

Look here  
[http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations](http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations)  
[http://wiki.rubyonrails.org/rails/pages/UsingMigrations](http://wiki.rubyonrails.org/rails/pages/UsingMigrations)

you should never change the schema.rb file.  
Rake reads online in the database the current layout version.

If for some reason you breake the basebase, you can delete all tabled.  
And that with  
rake db:migrate

regards  
svend

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [March 20, 2008, 9:11am UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441/4 "2008-03-20T09:11:48Z")

</div>

You may want to check out [Automatically — err.the\_blog](http://errtheblog.com/posts/65-automatically)

Whenever I start a project (read not being publically used), using this  
plugin seems like a good idea. Basically, it allows you to just fool  
around with the schema.rb and run a rake task and your tables will be  
automatically adjusted. This is a great way to avoid having a ton of  
migrations as you experiment with a project early on.

Once the schema have settled down and it is time to deploy for public  
use, simply load the schema onto the production server (using  
db:schema:load) and then start using traditional migrations.

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [March 20, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/migrations-and-schema-rb-where-did-i-go-wrong/132441/5 "2008-03-20T14:02:29Z")

</div>

Nathan E. wrote:

> You may want to check out [Automatically — err.the\_blog](http://errtheblog.com/posts/65-automatically)
> 
> Whenever I start a project (read not being publically used), using this  
> plugin seems like a good idea. Basically, it allows you to just fool  
> around with the schema.rb and run a rake task and your tables will be  
> automatically adjusted. This is a great way to avoid having a ton of  
> migrations as you experiment with a project early on.
> 
> Once the schema have settled down and it is time to deploy for public  
> use, simply load the schema onto the production server (using  
> db:schema:load) and then start using traditional migrations.

Hmm… I use a different approach where periodically, as the number of  
“niggle” migrations (add a field here, drop a table there) gets above X  
(largely dependent on how busy I am), I’ll rake to VERSION=0, then use  
the data in the model from annotate\_models (great plugin IMHO) to edit  
the base migration for each model up to current, collapsing the  
revisions, then just a rake db:migrate to rebuild. Keeps everything  
consistent, which I like.
