Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. Ruby on Rails Connect to Multiple Databases and Migrations
  2. FEB 7TH, 2015
  3. Ruby on Rails connect to Multiple Databases And Migrations
  4.  
  5. Ruby on Rails connect to Multiple Databases and using ActiveRecord with multiple databases, it’s really simple take it easy. Let’s run through this.
  6.  
  7. Rake Tasks
  8. Well, I want to handle migrations for two databases, so I need two separate Rake tasks to handle that:
  9. ```ruby
  10.  
  11. desc "Migrate the database through scripts in db/migrate directory."
  12.  
  13. namespace :db do
  14. task :migrate do
  15. Rake::Task["db:migrate_db1"].invoke
  16. Rake::Task["db:migrate_db2"].invoke
  17. end
  18.  
  19. task :migrate_db1 do
  20. ActiveRecord::Base.establish_connection DB1_CONF
  21. ActiveRecord::Migrator.migrate("db/migrate/db1/")
  22. end
  23.  
  24. task :migrate_db2 do
  25. ActiveRecord::Base.establish_connection DB2_CONF
  26. ActiveRecord::Migrator.migrate("db/migrate/db2/")
  27. end
  28. end
  29. ```
  30. My first task is db:migrate that delegates out to db:migrate_db1 & db:migrate_db2.
  31.  
  32. Each of those establish a connection to the database and then runs the migrations from their own separate folders. This allows you to store migrations in separate folders so you can easily manage them.
  33.  
  34. The migrations are exactly the same as normal.
  35.  
  36. Database Connections
  37. In order to get those migrations to work, I need to configure the database connections. I’m going to define everything in the database.yml just like normal, but with a different naming convention:
  38.  
  39. database.yml
  40. ```ruby
  41. defaults: &defaults
  42. username: root
  43. password: 1234567
  44. adapter: mysql2
  45. encoding: utf8
  46. collation: utf8_unicode_ci
  47.  
  48. db1:
  49. development:
  50. database: db1_development
  51. host: localhost
  52. <<: *defaults
  53.  
  54. test:
  55. database: db1_test
  56. host: localhost
  57. <<: *defaults
  58.  
  59. staging:
  60. database: db1_staging
  61. host: localhost
  62. <<: *defaults
  63.  
  64. production:
  65. database: db1_production
  66. host: localhost
  67. <<: *defaults
  68.  
  69. db2:
  70. development:
  71. database: db2_development
  72. host: localhost
  73. <<: *defaults
  74.  
  75. test:
  76. database: db2_test
  77. host: localhost
  78. <<: *defaults
  79.  
  80. staging:
  81. database: db2_staging
  82. host: localhost
  83. <<: *defaults
  84.  
  85. production:
  86. database: db2_production
  87. host: localhost
  88. <<: *defaults
  89. ```
  90. I configure two separate databases db1 & db2.
  91.  
  92. Then I need to configure the app to load these now. I open application.rb or environment file(s):
  93.  
  94. application.rb
  95. ```ruby
  96. ENV['ENV'] ||= 'development'
  97.  
  98. db_conf = YAML::load(File.open(File.join(APP_PATH,'config','database.yml')))
  99.  
  100. DB1_CONF = db_conf["db1"][ENV['ENV']]
  101. DB2_CONF = db_conf["db2"][ENV['ENV']]
  102. ```
  103. Take a look at what’s going on:
  104. - I set the database configuration to use. You can just use Rails.env here instead of ENV[‘ENV’].
  105. - I load up the database.yml config and parse it with YAML.
  106. - I grab the configuration from the file for each db and the correct environment that I’m running in.
  107. Connecting Models
  108. When I’m working with multiple databases, I like to explicitly setup the connections inside the models themselves instead of inheriting from ActiveRecord::Base and using subclasses.
  109.  
  110. user.rb
  111. ```ruby
  112. class User < ActiveRecord::Base
  113. establish_connection DB1_CONF
  114. end
  115. ```
  116.  
  117. product.rb
  118. ```ruby
  119. class Product < ActiveRecord::Base
  120. establish_connection DB2_CONF
  121. end
  122. ```
  123. Well, All you really need to do is load the configurations, establish the database connections, and setup the migrations to load from a specific directory for each database.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement