Keeping models and migrations in sync

512 Views Asked by At

My first time with Padrino and DataMapper. If I understood correctly,

  • padrino g model Foo will make models/foo.rb as well as db/migrate/001_create_foos.rb.

  • In order to create an index, I need to specify it in the model, not in the migration.

  • padrino rake dm:auto:{upgrade|migrate} will generate the database from models, without paying attention to db/migrate folder.

So, it seems that migrations are only kept as "just in case" for upgrading production databases. Is there a way to generate a migration from the model? If not, does everyone else also have a headache trying to keep migrations up-to-date as you change your model? How do people write their migrations?

1

There are 1 best solutions below

3
On

According Padrino documentation i guess you're right. The Orm section states that the auto namespace won't use the generated migrations.

Basically, instead of writing migrations you can directly edit your schema.rb and perform a non destructive migration with padrino rake ar:auto:upgrade

I guess you can track the database with migrations removing the auto namespace as:

rake dm:migrate                # Migrates the database to the latest version
rake dm:migrate:down[version]  # Migrates down using migrations
rake dm:migrate:up[version]    # Migrates up using migrations

And generate migrations as described in the Migration Generator section