Multiple databases in Rails - ruby-on-rails

I'm trying to setup two database for a single Rails Application. The first one is would be the main database and the second would be there in the case the main database would crash. So, I need to manage the two databases (rake db:create, migrate...) and replicate the datas to the second one.
Does anybody know how to do this ?

You should not do that on Application Layer.
Use keepalived or Amazon RDS.

Related

Rails migration strategies on horizontally scaled apps

Assume I have an OpsWorks Rails application running with load and time based scaling.
What happens if I deploy code where there are multiple application servers running, in which multiple rake db:migrate is executed across the application servers?
Does Rails have any guard against this? OR would I specifically need to specify a single server that is responsible for running the migrations?
I am also curious to hear about migration strategies for Rails + RDS (Postgresql) on AWS.
The answer is Yes, Rails had a guard against this situation
First, the RDS itself can cache your queries. So for example, 2 of your instances running one CREATE INDEX query at the same time, the RDS itself can handle it.
Second, Rails automatically create a table named schema_migrations. When one of your instances running db:migrate, the table schema_migrations can help the other instances know that the database is already migrated up ( just like a version management system )
But, there is a bad practice that you wrote some custom queries in db/migrate/***.rb file, then your query might not be handled correctly by RDS.
If you have to do something like update data by custom domain, you should write a rake task and manually execute it.
So, if you only use db:migrate to update data structure, then everything already handled for you, even you had hundreds instances.
For more information, please refer Rails's document

Ruby on Rails: is it possible to access multi posgtgresql databases of heroku with one app?

I have two Rails apps in Heroku with Postgresql Databases.
Noiw i want to access those two databases with one app ony.
Is it Possible?
You can share one app database with other (with your own risk) like this. If you want multiple database connection in single rails app, you can try (with your own risk) this this

Multiple apps, one database heroku rails

A friend and I have an idea to create multiple apps via heroku using Postgres.
Is there away we can tie in all the databases into one singular database by either pushing each one individually or just having them all indexed?
Yes use heroku config in the app with the provisioned database to get the connection string. Then use heroku config:set PG_URL=3829w:8eh8fe78hfd#ffeiwfafdsa.amazonaws.com/8fdHUfdsa to set the database in the other app. Now both apps will use the same database. They won't overwrite each other, but they will write to the same DB.
You can create multiple schemas, which will namespace the tables per-schema and avoid any name conflicts between each app's models.
https://devcenter.heroku.com/articles/heroku-postgresql#multiple-schemas
The most common use case for using multiple schemas in a database is building a software-as-a-service application wherein each customer has their own schema.
The flipside (as the link points out) is that multiple schemas can significantly reduce performance. You're also sharing connection limits, cache and disk space between apps. This might not be a problem depending on your usage, but just be aware of it.
Further reading:
CREATE SCHEMA docs: http://www.postgresql.org/docs/9.3/static/sql-createschema.html
Set schema_search_path in database.yml in each app to your individual schemas: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html

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.
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.
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.
I disagree with Schmurfy, even if his presented options are valid, its a bit of an overkill to share data through REST (granted, its pretty easy to implement with ruby / rails).
If your case is simple you could just use one database from both apps, and since you use AR in both of them you have no problems with versioning, AR takes care of that.
Also i dont know what happens if you run db:migrate from both apps simultaniously if you use a inferior dbms like mysql which does not allow DDL in a transaction, certainly nothing good..
Also it would bother me to look which app needs what column and not have the migrations in one place. You could use a shared repository to manage the migrations from both apps.
Rails migrations store current database version in schema_migrations table in the database. So both of your apps will be able to check the current version.
The version numbers are timestamps, so there shouldn't be any problem with duplicate values, as it'll be almost impossible to generate two migrations at the exact same millisecond. So you should be fine here.
The only problem I see is that when you rollback a migration in one app, it'll set the db to the previous known version and I'm not sure if it will pick the previous one from the db (which could be from the other app), or the number from the previous migration file. You may want to test that scenario to make sure.
I decided to put all migrations in the Rails app because:
Since there is only one database
Rails manages migrations
This has worked well.
This simplifies the system because all migrations are stored in one place. And, the Sinatra app doesn't need to know about them anyway.

Rails connect to an 'external' sqlite database

I am trying to connect to a database from my rails app. This database is being updated all the time for storing the results of ongoing tests. My rails app is basically a front-end to display the test results.
What is the best way to access the sqlite database without interfering with the test framework? At the moment when I start the rails server it locks the database and no read/write is allowed to it while the server is running.
What is the best way to display results from a continuously growing database, where new records are not being added by the rails app itself? Thanks for your help!
You should consider using another database engine, such as MySQL or Postgres. SQLite is a good RDBMS but does not fit well for high-concurrency scenarios. It has been designed mainly for desktop or device-embedded applications.
From http://www.sqlite.org/whentouse.html
SQLite uses reader/writer locks on the entire database file. That
means if any process is reading from any part of the database, all
other processes are prevented from writing any other part of the
database. Similarly, if any one process is writing to the database,
all other processes are prevented from reading any other part of the
database.

Resources