0

I'm working on upgrading my project from rails 3.0.9 to rails 3.2.5. I'm using multiple databases and when running the migration in rails 3.2.5 everything runs ok but everything it's created in the default database instead of it's corresponding database.

I thing it's a problem with the connection pool but the bug with connection pool was fixed for rails 3.1.x

These are my models:

class Account < ActiveRecord::Base
  establish_connection :accounts
end

class Patient < ActiveRecord::Base
end

:accounts it's the connection to the other database while the other classes uses the default connection:

this my database.yml (I'm using an external file so I can modify the database connection without changing the code).

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: <%= database_config_file['database_dev'] %>
  pool: 5
  username: <%= database_config_file['username'] %>
  password: <%= database_config_file['password'] %>
  socket: /var/lib/mysql/mysql.sock

accounts:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: <%= database_config_file['database_account'] %>
  pool: 5
  username: <%= database_config_file['username'] %>
  password: <%= database_config_file['password'] %>
  socket: /var/lib/mysql/mysql.sock

this are my migrations for the classes:

class CreateAccounts < ActiveRecord::Migration

  def self.connection
    Account.connection #Account model has a connection to the database I want
  end

  ...
end

Hope someone can help

Giancarlo Corzo
  • 1,976
  • 5
  • 24
  • 36
  • Am I right that you're trying to perform migration on DB specified by accounts, but actual migrations are performed on your develpment DB? – jdoe Jun 11 '12 at 16:35
  • yes, when I run the migrations 4 tables should be created in the accounts database and 12 tables in the development database but all 16 tables are created in the development database. – Giancarlo Corzo Jun 12 '12 at 06:52

1 Answers1

0

Your DB where you're planing to make migrations has to be specified under development key (unless you want to create new environment) and your old DB -- under, say, old_db in your yml-file.

Then I suggest you to create legacy_base.rb in your models/ folder with the following contents:

class LegacyBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :old_db # points to your legacy DB
end

Then for each model that belongs to your old DB you have to replace:

class SomeModel < ActiveRecord::Base

with:

class SomeModel < LegacyBase

This way your 'heritage' will use your legacy DB and newly created models/migrations will use your new DB without any tricks.

I hope I understood your intentions.

jdoe
  • 15,665
  • 2
  • 46
  • 48