ActiveRecord migration merge broken

Let's say I work in git in my Rails application, and I have two branches, each of which has its own migration.

1) branch001 creates a table called tableA using migration 20160101000000_create_table_A

2) branch002 creates a table named tableB using migration 20160101000001_create_table_B

Obviously, a timestamp for the second migration was created after the first.

But let me say that I first branch002 to master first, because it is ready first. My schema file becomes -

 ActiveRecord::Schema.define(version: 20160101000001) do .... end 

The schema version tells activerecord that it has already been patched to a level / version larger than my first branch.

What happens when I finally get to merge my first branch?

  • Will the schema version return in 20160101000000 ?
  • Will there be any problems associated with the first branch migration because the circuit sees that it is already “patched” and skips it?
  • In general, what is the best practice for such things? Should I rename the first branch with a new, newer timestamp?

Thanks!

EDIT -

It is really interesting what I have to do to resolve the merge conflict when I merge the 2nd branch into master . Should I leave it as a later timestamp or return it to an earlier timestamp?

 <<<<<<< HEAD (master) ActiveRecord::Schema.define(version: 20160101000001) do ======= ActiveRecord::Schema.define(version: 20160101000000) do >>>>>>> 282cda7... Adding Table B 
+6
source share
2 answers

In case of conflict, you must choose the larger number of the two. But everything that you choose does not affect the actual migration.

If you select a smaller number, then the next time you run rake db:migrate it will change that number (to a higher one), and you will change your schema.rb and not go over. This is not a problem - only your commit will be a little weird.

The raeds rake task runs all the migrations found and which do not matter in the schema_migrations table. And then he takes the highest timestamp of the migration and puts that timestamp in schema.rb . The whole idea of ​​migration is not based on some “latest timestamp” (the version of the scheme), but based on the contents of the schema_migrations table containing all the migration timestamps that it has already completed. Thus, in this table, it does not skip migration.

+10
source

The very short version is that everything is fine: assuming that the migrations do not interfere with each other (for example, one packs the table and the other assumes that it still exists), you should not run into problems.

While your schema file has one version number in it, when starting migrations, the rails compare the list of migration files with the contents of the schema_migrations table and start any migrations that have not yet been performed, even if there are later migrations that have already been started.

As for the schema conflict, I personally just run migrations that will regenerate schema.rb.

+2
source

All Articles