Topic: Loading Schema Troubles

Hi Railsforum members,

I'm beginning to us RSpec and I generate a user model and run a spec on it
http://img530.imageshack.us/img530/8036/rspecuservn5.gif

I need a table called users, so I rake db:migrate and it creates a new table.
I run spec again but then I think its needs some fields.
I delete my migrate file and just manually update my schema file
http://img101.imageshack.us/img101/5451/describeschemaob6.gif
then I run db:schema:load so that it commits the changes to my database.
http://img508.imageshack.us/img508/8859/rakeschemamz1.gif
It's updated schema_info but not the users table. (in my development database)
http://img508.imageshack.us/img508/9610/describeusersxp6.gif
I check my test database and there are no tables at all.
Which is kind of important since rspec is looking at my test database.
I made sure early on to enable in my environments.rb this line because I'm suppose to have it enable for whatever reason. I forgot what the comments are trying to tell me what this line do but I remember that if it wasn't enabled that things would commit to the database running a migration
http://img527.imageshack.us/img527/6184/schemaformatut3.gif

My Problem:
why is my test database empty?
how do I get my schema to update?
what are the best practices for working with migrations?

Last edited by OmenKing (2007-05-26 16:33:55)

Re: Loading Schema Troubles

OmenKing wrote:

why is my test database empty?

You need to run this command after migrating the database to update the test database:

rake db:test:prepare

OmenKing wrote:

how do I get my schema to update?

Not sure.

OmenKing wrote:

what are the best practices for working with migrations?

Generally, you shouldn't alter a migration file after you've run the migration scripts. If you made a simple mistake and need to revert the migration you can do so like this:

rake db:migrate VERSION=x

This will run the "down" script in the latest migration to reverse any effects, then you fix the migrations then run them again.

Railscasts - Free Ruby on Rails Screencasts

Re: Loading Schema Troubles

Thanks ryanb

I haven't seen rake db:test:prepare before. Resolved the problem.

Re: Loading Schema Troubles

OmenKing wrote:

what are the best practices for working with migrations?

ryanb wrote:

Generally, you shouldn't alter a migration file after you've run the migration scripts. If you made a simple mistake and need to revert the migration you can do so like this:

rake db:migrate VERSION=x

This will run the "down" script in the latest migration to reverse any effects, then you fix the migrations then run them again.

After reversing a migration, is it advisable to delete the migration file from db/migrate or just over-write it with the next desired migration?  For example:

1. My current migration file prefix is '009_'
2. I execute 'rake db:migrate VERSION=8'

Should I now delete the migration file with prefix '009_' ?

I'm concerned that if I do not delete it, the following will occur:

A. I issue a new 'script/generate migration' command => Rails will create a '010_'-prefixed file
B. I issue 'rake db:migrate' command => Rails will re-apply migration '009_', then apply migration '010_'
C. I will have a hosed database because it will again contain whatever changes migration '009_' entailed (which I don't want)

So, it seems that deleting migration file '009_' will eliminate this possibility.  Is this correct?  Thx,

Dondi.

Re: Loading Schema Troubles

Dondi wrote:

So, it seems that deleting migration file '009_' will eliminate this possibility.  Is this correct?

Yep. If you don't want the migration file to run you'll have to delete it.

Railscasts - Free Ruby on Rails Screencasts

Re: Loading Schema Troubles

Thanks ryanb.  I really appreciate all the help that you have provided me and the rest of the Rails community!