capistrano / rails

Official Ruby on Rails specific tasks for Capistrano
http://www.capistranorb.com/
MIT License
867 stars 270 forks source link

Mongoid indexes creation #222

Open FunkyloverOne opened 6 years ago

FunkyloverOne commented 6 years ago

Hey guys, I would like to be able to automatically create Mongoid indexes on each deploy, like we do with migrations with relational databases.

(I think that it's totally OK to do so, just not to forget to create some new indexes for some new models for example, if I'm wrong please correct me.)

I have wrote some code, based on your code for migrations, it doesn't work locally yet, and I don't really know why, but it's close to something that should work :) Maybe it could be added to this gem, take a look at what I got so far:

namespace :mongoid do
  desc 'Runs rake db:mongoid:create_indexes'
  task create_indexes: [:set_rails_env] do
    on fetch(:mongoid_servers) do
      info '[mongoid:create_indexes] Run `rake db:mongoid:create_indexes`'
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'db:mongoid:create_indexes'
        end
      end
    end
  end

  after 'deploy:updated', 'mongoid:create_indexes'
end

namespace :load do
  task :defaults do
    set :mongoid_role, fetch(:mongoid_role, :db)
    set :mongoid_servers, -> { primary(fetch(:mongoid_role)) }
  end
end
germanotm commented 2 years ago

That's how i managed to create a task to run db:mongoid:create_indexes on each deploy

Add this to your config/deploy.rb.

namespace :deploy do
  desc "Create mongoid indexes."
  task :create_indexes do
    on roles(:app) do
      within "#{release_path}" do
        with rails_env: "#{fetch(:stage)}" do
          info '[deploy:create_indexes] Run `rake db:mongoid:create_indexes'
          execute :rake, 'db:mongoid:create_indexes'
        end
      end
    end
  end
  after 'deploy:updated', 'deploy:create_indexes'
end