Search code examples
ruby-on-railssidekiq

How to run rake db:migrate with sidekiq


I am trying to run the command rake db:migrate using a sidekiq worker but it seems like it just won't work and I am curious if there is a way to do this or not. I am creating a scaffold using sidekiq but cannot migrate it afterwards

This works

class ScaffoldGeneratorWorker
  include Sidekiq::Worker

    def perform(id)
      `rails g scaffold test_#{id} title:string body:text slug:string visible:boolean`
    end
end

But I cannot get this to run afterwards and work

class DatabaseMigrationWorker
  include Sidekiq::Worker

  def perform
    `rake db:migrate`
  end
end

Is this possible, and, if so, how can I get it to work. Any help is greatly appreciated.


Solution

  • First you should load rake tasks, then invoke:

    class DatabaseMigrationWorker
      include Sidekiq::Worker
    
      def perform
         Name_Of_Your_App::Application.load_tasks
         Rake::Task['db:migrate'].invoke
      end
    end