Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migration Order and Git

Since doing migrations with rails + git is type of a pain, a new thorn has sprung..

Before I am doing any harm to my prod DB, would the following situation cause havoc? If so, how would I handle it?

I am working a long-term feature in a separate branch (feature/long-term). This feature is an overhaul of a lot of components and it will take awhile to complete. This feature has new migrations, which were migrated to the localhost DB.

meanwhile, I need to fix/add a migration to the prod system via another branch (feature/quick-fix). This has a migration file with date later than the feature/long-term migration.

The migrations of the quick-fix and the long-term have nothing to do with each other, they do not collide and work on separate tables. It doesn't matter what order they are run.

If I merge feature/quick-fix to master and db:migrate and in a few days/weeks merge feature/long-term the migration files order would be the long-term first.

Would this affect the DB in some way? (the prod DB is important, so I don't want to reset)

like image 548
Nick Ginanto Avatar asked Oct 18 '25 13:10

Nick Ginanto


1 Answers

What you described is a very common development workflow (especially so in teams with more members) and it's perfectly safe for your production DB.

Rails, as of version 2.1, is smart enough to keep a list of all migrations ever run, instead of just the latest migration version run. This information is stored on a separate table aptly named schema_migrations.

So, if you push a new migration today, say 20140527_quick_fix.rb, and a month after that you push a new (but with an older timestamp) one 20140101_long_term_feature.rb, Rails will still know that the latter was never run in your production environment so during rake db:migrate it will process it, as you would expect. The newest won't be run again of course as the Rails would know that it has already been processed.

From the official documentation:

Rails versions 2.0 and prior used to create a table called schema_info when using migrations. This table contained the version of the schema as of the last applied migration.

Starting with Rails 2.1, the schema_info table is (automatically) replaced by the schema_migrations table, which contains the version numbers of all the migrations applied.

As a result, it is now possible to add migration files that are numbered lower than the current schema version: when migrating up, those never-applied “interleaved” migrations will be automatically applied, and when migrating down, never-applied “interleaved” migrations will be skipped.

like image 173
Kostas Rousis Avatar answered Oct 21 '25 03:10

Kostas Rousis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!