I came across a scenario where my existing application database which was residing in MySql wanted to create/communicate to tables present in PostgreSql. Though it is not advisable but i had no other option. so i googled a lot, went through n number of blogs and finally found a solution which suited my requirement this worked for me.
Install PostgreSql has explained in my previous post.
Configure your database.yml
development:
adapter: mysql2
encoding: utf8
database: test_development
username: root
password: *******
host: 127.0.0.1
development_postgres:
adapter: postgresql
database: test_dev
username: pgs_root
password: ********
host: 127.0.0.1
Create the database
rake db:create RAILS_ENV=development_postgres
This will create a database named test_dev in PostgreSQL
Specify the connection in model so that the application understands where the table resides
establish_connection :development_postgres
if you wish to have it environment specific, write
establish_connection Rails.env+"_postgres"
In migration file, specify the connection
def self.connection
modelname.connection
end
def self.up
end
This self.connection allows the application to communicate with database specified in the model. This has to be written only if you want to run migration belonging to postgres, failing to specify migration will run in default database i.e mysql
Establishing relationship between the tables present in two databases
for ex. table named "foo" exists in postgres and it belongs to "boo" present in mysql.
In foo.rb
establish_connection Rails.env (relationship database connection)
belongs_to :boo
establish_connection Rails.env+"_postgres" (database connection)
In boo.rb
establish_connection Rails.env+"_postgres"
has_many :foo
establish_connection Rails.env
This establishes HasManyBelongsTo relationship between boo & foo.
Note:Always Specify the relationship database connection first & table connection at the end. All the migration no. will be stored in schema_migration table present in mysql.
This is the only approach which worked for me, if there is a better solution other than the above please let me know.
Install PostgreSql has explained in my previous post.
Configure your database.yml
development:
adapter: mysql2
encoding: utf8
database: test_development
username: root
password: *******
host: 127.0.0.1
development_postgres:
adapter: postgresql
database: test_dev
username: pgs_root
password: ********
host: 127.0.0.1
Create the database
rake db:create RAILS_ENV=development_postgres
This will create a database named test_dev in PostgreSQL
Specify the connection in model so that the application understands where the table resides
establish_connection :development_postgres
if you wish to have it environment specific, write
establish_connection Rails.env+"_postgres"
In migration file, specify the connection
def self.connection
modelname.connection
end
def self.up
end
This self.connection allows the application to communicate with database specified in the model. This has to be written only if you want to run migration belonging to postgres, failing to specify migration will run in default database i.e mysql
Establishing relationship between the tables present in two databases
for ex. table named "foo" exists in postgres and it belongs to "boo" present in mysql.
In foo.rb
establish_connection Rails.env (relationship database connection)
belongs_to :boo
establish_connection Rails.env+"_postgres" (database connection)
In boo.rb
establish_connection Rails.env+"_postgres"
has_many :foo
establish_connection Rails.env
This establishes HasManyBelongsTo relationship between boo & foo.
Note:Always Specify the relationship database connection first & table connection at the end. All the migration no. will be stored in schema_migration table present in mysql.
This is the only approach which worked for me, if there is a better solution other than the above please let me know.