views:

365

answers:

2

Hi there,

I was told that for some reason, you can't update a database schema when using rails. You can drop a table and then recreate a table with an updated schema, but this won't work if you already have content stored in the table that you want to update.

What do you recommend?

Thanks!

+8  A: 

What you were told is incorrect. You can update a DB schema when using Rails.

The way you do it is through "migrations."

A common pattern is to write a set of migrations that build your initial schema. As your app develops, you write other migrations that change the tables and columns to suit the evolving design. If the app is in production, you apply these new migrations to the production schema.

Of course some changes will mess up your existing data, but that has nothing to do with Rails. That would be true regardless of what programming language/framework you're using.

If you have a legacy DB schema and are not using migrations, you can still update your schema by interacting directly with the DB server. Again, what will work and what won't in that situation has nothing to do with Rails. It's totally up to the structure of the schema and the data itself.

Ethan
Thanks, Ethan. :)
Jess
A: 

If you drop a table in the content in it, the content will be destroyed. You can, however, include content migrations alongside db schema migrations, so it will be migrated back to the table once the schema is updated.

Goro
Please for the love of all that it good, avoid using migrations to put data into a database. Use a rake task.
Brian Hogan