Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a database, migrations, and models between Rails projects?

I know there are some questions to this effect already on StackOverflow, but they tend to be pretty outdated and don't adequately address how migrations are supposed to work in the following scenario, which should be fairly common:

  • You have some kind of application, implemented in Rails.
  • You have some kind of admin application for your data, and it is a separate application implemented in Rails.
  • Both applications operate on the same database and models.

My question is: what is the best way to factor our models such that both of these applications don't have to duplicate model code?

We are concerned with the following:

  • For the shared models, where should database migrations live?
  • What if each individual application wishes to add additional models on top of shared models? Where do these migrations live?
  • What is the best way to move existing migrations into the proposed shared migration scheme?

Thanks.

like image 341
Jake Avatar asked Mar 24 '16 14:03

Jake


People also ask

What is difference between model and migration in rails?

Rails Model (Active Record) works with SQL, and Rails Migration works with DDL. Rails Model supports ways to interact to with the database, while Rails Migration changes the database structure. A migration can change the name of a column in books table.

Where does rails store migration data?

Every Rails app has a special directory— db/migrate —where all migrations are stored. Let's start with a migration that creates the table events into our database. This command generates a timestamped file 20200405103635_create_events. rb in the db/migrate directory.

How does rails migration work internally?

Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them.

How do you add migrations in rails?

2.1 Creating a Standalone MigrationMigrations are stored as files in the db/migrate directory, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products. rb , that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration.


1 Answers

I don't know if this is THE approach, and would love to see other ideas, but what we do in one of our products that matches this model:

For the shared models, where should database migrations live?

We keep all of our migrations under the admin system. You don't need them to exist twice, so that's where they go.

What if each individual application wishes to add additional models on top of shared models? Where do these migrations live?

We share all models. It might only be relevant to one application at the moment, say - a favourited_items concept might only matter to the end user. But at a later date the admin might want to know what items are most frequently favourited.

Secondly, if you want to investigate anything via console, it's really quite helpful if you don't need to visit separate applications because they don't both have models for every table.

Functionality in shared models that differs per application detects the rails environment variable, which we have extended to include more context. E.g.: if Rails.env == 'admin_production'

What is the best way to move existing migrations into the proposed shared migration scheme?

Again, migrations should only ever exist once, and the shared database knows which have already been run, so unless you're renaming the migrates you just need to pick a location and move the files.

like image 165
Matt Avatar answered Oct 18 '22 19:10

Matt