Vaibhav Kaushal
Vaibhav Kaushal

Reputation: 1

How to send SQL queries to two databases simultaneously in Rails?

I have a very high-traffic Rails app. We use an older version of PostgreSQL as the backend database which we need to upgrade. We cannot use either the data-directory copy method because the formats of data files have changed too much between our existing releases and the current PostgreSQL release (10.x at the time of writing). We also cannot use the dump-restore processes for migration because we would either incur downtime of several hours or lose important customer data. Replication would not be possible as the two DB versions are incompatible for that.

The strategy so far is to have two databases and copy all the data (and functions) from existing to a new installation. However, while the copy is happening, we need data arriving at the backend to reach both servers so that once the data migration is complete, the switch becomes a matter of redeploying the code.

I have figured out the other parts of the puzzle but am unable to determine how to send all writes happening on the Rails app to both DB servers.

I am not bothered if both installations get queried for displaying data to the user (I can discard the data coming out of the new installation); so, if it is possible on driver level, or adding a line somewhere in the ActiveRecord, I am fine with it.

PS: Rails version is 4.1 and the company is not planning to upgrade that.

Upvotes: 0

Views: 95

Answers (1)

sameera207
sameera207

Reputation: 16629

you can have multiple database by adding an env for the database.yml file. After that you can have a seperate class Like ActiveRecordBase and connect that to the new env.

have a look at this post

However, as I can see, that will not solve your problem. Redirecting new data to the new DB while copying from the old one can lead to data inconsistencies.

For and example, ID of a record can be changed due to two data source feeds.

If you are upgrading the DB, I would recommend define a schedule downtime and let your users know in advance. I would say, having a small downtime is far better than fixing inconstant data down the line.

When you have a downtime,

  • Let the customers know well in advance
  • Keep the downtime minimal
  • Have a backup procedure, in an even the new site takes longer than you think, rollback to the old site.

Upvotes: 1

Related Questions