Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage migrations when multiple apps share the same database in Ruby?

I have a Rails app and a Sinatra app, sharing the same database. The Sinatra app uses ActiveRecord.

Can I run migrations from within each app, as if they were in the same app? Will this cause any problems?

The schema.rb file in the Rails app tracks the current migration via

ActiveRecord::Schema.define(:version => 20121108154656) do

but, how does the Sinatra app know the current version the database?

Rails 3.2.2, Ruby 1.9.3.

like image 280
B Seven Avatar asked Nov 27 '12 16:11

B Seven


2 Answers

The version column in the schema_migrations table equate to the time stamp on the front of the ruby migration file example: 20130322151805_create_customers.rb So if two ore more applications are contributing to the schema_migrations table roll backs will not be possible if rails can't find the down() method (because it will not find a migration file contained in another app ie db/migrate/...)

I have a current situation that is exactly this and I have opted to have a master ActiveRecord app that manages migration and data conversions as our database evolves. Keep in mind that part of the deal is to keep models up to date as well. This has been time consuming so we are considering breaking apart the DB in to business domains and providing APIs (JSON) to query support data for another application. This way each application manages it domain and is responsible for exposing data via API.

regards.

like image 196
Dale Avatar answered Sep 28 '22 07:09

Dale


If you connect both applications to the same database you should be able to run migrations on it but I strongly suggest you use another option since you will almost surely hit a wall at one time or another:

  • split the database in two if possible with each application responsible for its own database /migrations.

  • have one application considered the "master" database and use another database for the data specific to the second application but make it connects to both database (each application still only apply migrations to one database)

If you need to share data between multiple applications another option is to implement a REST service in one and use it on the other, you can have a look at the grape gem for a simple way of doing so.

Edit: I realize I forgot to speak about the activerecord migration, there is no longer any "version" of the schema, what activerecord does is that it read all your migration filename, extract their identifier (the starting part) and check if they have already been applied so in theory you can run migrations from two applications on the same database provided they don't interfere. But if both migrations act on the same tables you will almost certainly run into big troubles at one point.

like image 39
Schmurfy Avatar answered Sep 28 '22 08:09

Schmurfy