Overview
My current work involves writing a handful of Rails plugins. These plugins provide additional functionality that includes ActiveRecord models that need to be persisted to the database. I originally created a migration class for one of the plugins that re-used much of the ActiveRecord logic (in fact, just overrides schema management). This worked fine, but as we started creating new plugins that needed the same functionality, we decided to pull the migration logic into a separate Rails plugin.
And hence, the PluginMigrator was born!
In order to use the PluginMigrator, your plugin must simply extend the PluginMigrator::Migrator class:
module MyPlugin
class Migrator < PluginMigrator::Migrator
set_schema_table_override "my_plugin_schema_info"
set_migration_directory(File.dirname(__FILE__) + "/../../db/migrate")
end
end
The set_schema_table_override method tells the plugin where the version info for your plugin should be stored. For rails apps, this schema information lives in a table named “schema_info”. You’ll need to specify a different table name for your plugin, so that your plugin migrations can be managed separately from the Rails app. Don’t worry if it doesn’t exist yet – the migration system will automatically create it.
The set_migration_directory method tells the plugin where to find the migrations. The migrations for your plugin should probably be stored in the same way as the main rails app. In your plugin root, it’s easy to just have a db/migrate structure:

To actually migrate, simply create a Rake task in your tasks directory inside of your plugin or in the main Rakefile for your plugin, depending on from where you want to run the migrate task:
namespace :myplugin do
desc "Run the migrations for my plugin"
task :migrate => :environment do
MyPlugin::Migrator.migrate(ENV['VERSION'],false)
end
end
And then:
% rake myplugin:migrate
Typical VERSION behavior is also supported:
% rake myplugin:migrate VERSION=1
Installation
The PluginMigrator project is hosted at GitHub.
From the Terminal:
~/testapp $ cd vendor/plugins ~/testapp/vendor/plugins $ git clone git://github.com/oculardisaster/plugin_migrator.git
If you don’t have Git on your system, you can simply go to the main project page and click the download button.
Known Issues and Planned Features
- Would like to be able to optionally exclude having the main rails app include plugin tables when writing out the schema.rb file during a Rails app migration.
- Would like to have the plugin migrator automatically create rake tasks to migrate the plugin





