Anero
Anero

Reputation: 1416

MIgration defined in gem not being executed

I added a new migration to (an existing) gem and when running the rake db:migrate command the migration is completely ignored. I tried forcing it to be run using rake db:migrate:redo VERSION=[my migration version] but it reports it cannot find the migration with that version.

I'm using ruby 1.9.2p320 with rails 3.1.3 and there shouldn't be a problem with having the wrong gem version as I'm using it directly from the file system (though I've tried installing / updating through bundle).

Also, I'm not sure if defining migrations on a gem is the right approach, but since this lib is highly coupled with the rails site and specially since it's legacy code, I won't move this away unless it's absolutely necessary.

EDIT: The migrations in the gem are defined as they're usually defined in any rails app:

gem_folder
   |
   |- db
  ...  |
       |- migrate
            |
            |- <migration files>
                     ...

Upvotes: 0

Views: 295

Answers (1)

Anero
Anero

Reputation: 1416

I found the issue: there was a rake task specifically defined in the gem to run these migrations. Including the code just in case it's useful to somebody else.

namespace :db do
  namespace :migrate do
    description = "Migrate the database through scripts in <gem name>/db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."

    desc description
    task :admin => :environment do
      ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
      if Gem.searcher.find('<gem name>')
        dir = "#{Gem.searcher.find('<gem name>').full_gem_path}/db/migrate/"
        ActiveRecord::Migrator.migrate(dir, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
      else
        raise "Unable to locate <gem name> gem to run admin migrations"
      end
      Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
    end
  end
end

Upvotes: 1

Related Questions