Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoid indexes creation #222

Open
flvrone opened this issue Jun 19, 2018 · 1 comment
Open

Mongoid indexes creation #222

flvrone opened this issue Jun 19, 2018 · 1 comment

Comments

@flvrone
Copy link

flvrone commented Jun 19, 2018

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
Copy link

germanotm commented Jan 7, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants