Search code examples
ruby-on-railsrubygitherokudatabase-migration

How does a migration from a Git branch get run on Heroku if a later migration from a different branch has already been run?


Say I create a branch (new_branch). In that branch, a migration (migration_1) is created.

Later, I switch back to master. I then create a migration (migration_2), push to Heroku, and run migrations on Heroku.

Later still, I merge new_branch into master, and then push master to Heroku.

When I try to run migrations on Heroku, won't migration_1 be skipped over and not run, because it was created before migration_2, which has already been run?


Solution

  • Each migration has a migration timestamp attached. The list of successfully applied migrations is stored in a schema table inside your Rails application.

    When you run migration_2, this entry is added to the database. When migration_1 is merged, Rails will detect the change has not been applied yet because the entry is missing in the schema table, and will run it.