Short form: How can I convince migrate to create or modify tables in a
database other than the "current" one?  In other words, how can I get
migrate to honor establish_connection :external in a model definition?

Details: I have a large "almost static" dataset that never changes as a
result of my Rails app, so it's residing in a separate database.  (It
gets refreshed nightly with external data.)  I'd like to maintain its
schema using the Rails migration mechanism, but despite my efforts,
migrate only updates tables for the current environment (e.g.
_development or _test).

Here's a stripped down example:

=== MODELS (in app/models):
class Hat < ActiveRecord::Base
  establish_connection :external # use external database
end

=== SCHEMA (in db/schema.rb, lightly edited):
ActiveRecord::Schema.define(:version => 20100812225348) do
  create_table "hats", :force => true do |t|
    t.string "style"
  end
end

=== TABLE DEFINITIONS (in config/database.yml, only showing first two
entries):
development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: dbtest_development
  pool: 5
  username: root
  password: XYZZY
  socket: /tmp/mysql.sock

external:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: dbtest_external
  pool: 5
  username: root
  password: XYZZY
  socket: /tmp/mysql.sock

=== SYNOPSIS:
$ rails new dbtest
$ cd dbtest
$ <edit config/database.yml to add external table spec>
$ rake db:create:all
$ rails generate model Hat style:string
$ <edit models/hat.rb to include establish_connection as shown above>
$ rake db:migrate

At this point, I peeked at the db using mysql, and noticed that the
dbtest_development db had a table defined for 'hats', but
dbtest_external did not.  Since Hat was defined to establish_connection
to dbtest_external, I was already in trouble.  I tried adding an
explicit establish_connection for the migration process like this:

$ rake db:rollback
$ cat > db/migration/xxxx_create_hats.rb
class CreateHats < ActiveRecord::Migration
  def self.up
    Hat.establish_connection :external    # added this line
    create_table :hats do |t|
      t.string :style
    end
  end
  def self.down
    Hat.establish_connection :external    # added this line
    drop_table :hats
  end
end
^D
$ rake db:migrate

But after the rollback and migration, the dbtest_development db STILL
had a tables defined for 'hats' and dbtest_external had no 'hats' table.

I've pored over this forum, googled the web, and glanced at the sources,
but I still don't see how to get migrate to manage tables in a database
other than the "current" one.

Ideas?  Pointers?

Thanks as always...

- ff
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to