Rails and ActiveRecord are not meant to deal with several databases just out of the box.
Shiva extends Rake tasks (like db:migrate, or schema:dump) to work with several databases.
This way you can set up a separate folder in db/migrate, a separate schema.rb, and so on, for each database.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself with:
$ gem install shiva
You will need to configure Shiva to explain the databases it is supposed to handle. You absolutely need to use the class name using a string, we had some weird issues using the class directly with RSpec ... no sure why for now.
Shiva.configure do database 'Databases::Archive' # The name will be guessed from the class_name, here: archive database 'Databases::Legacy', :old_db # Explicitly define the name end
Now your migrations for Databases::Archive are defined in
db/migrate/archive and those for Databases::Legacy in
You will also have several schemas in
db/schema. In our example these would be
rails generate shiva:migration database_name migration_name
This generator descends from ActiveRecord generator, so you can do all fancy stuff you are already used to.
The only thing you need to do is to provide database_name for Shiva migrator, so it knows which database should it use.
To run the migrations for all your databases just run
If you want to run the migrations for a specific database use
To dump the schema you can use the same principle
rake shiva:dump rake shiva:dump[old_db]
Bare list or Rake tasks
All Rake tasks accept one optional argument, which is a database to work on. When no arguments are passed, work is done on all databases defined by Shiva configurator.
rake shiva:migrate rake shiva:rollback rake shiva:dump rake shiva:abort_if_pending_migrations rake shiva:seeds rake shiva:schema:load rake shiva:structure:load
(Needs updates when using different plaforms)
If you're familiar with
.travis.yml, you can prepare your own test environment just fine by looking at what's done there.
jRuby & SQLite
You will need to download Ant to build JDBC adapters. After that add Ant's
bin folder into your
git clone email@example.com:jruby/activerecord-jdbc-adapter.git cd activerecord-jdbc-adapter bundle install rake build adapters:build cd pkg gem install activerecord-jdbc-adapter-1.3.0.DEV.gem gem install activerecord-jdbcsqlite3-adapter-1.3.0.DEV.gem
Since SQLite does not support alterations on databases, some of our specs fail while using it (namely migration rollback and schema load). We advise you checking out, if you need these features, or to migrate your database to something more reliable than SQLite.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Include tests (we are using rspec)
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request