Giter Site home page Giter Site logo

rails_migrate_mutex's Introduction

Rails db:migrate:mutex

Circle CI

Runs Rails migrations in a Redis-backed mutex.

If you deploy your application to multiple servers and do not use a deployment tool like Capistrano, this gems makes sure that only one of your servers run the database migrations.

Running Rails migrations on multiple instance at the same time should not cause any real trouble, because they are run inside transactions, but can cause excessive DB load and Ruby exceptions.

Usage

Run rake db:migrate:mutex instead of rake db:migrate to ensure that the migrations are only run on one server at the same time.

Installation

Add the gem to your Gemfile:

gem 'rails_migrate_mutex'

If Redis.current does not contain a valid Redis connection, you might need to set it manually:

# config/initializers/redis_classy.rb
RedisClassy.redis = Redis.new(...)

Contributing

Feel free to fork and submit pull requests!

License

MIT, see LICENSE.txt.

rails_migrate_mutex's People

Contributors

dziemba avatar

Stargazers

Samuel Giles avatar Thomas Klemm avatar Leon Guan avatar Guillaume Hain avatar Shane O'Grady avatar  avatar Yoichi Dan avatar

Watchers

Harald Wartig avatar Helge Rausch avatar Roman avatar Martin Held avatar James Cloos avatar Gil Danziger avatar Mouaz Heron avatar Dustin Mierig avatar

Forkers

nomahnivek

rails_migrate_mutex's Issues

environment/initializers not loaded

Not sure if this is something wrong in my setup, but this wasn't working for me since I need to set the RedisClassy/Redis instance based on an initializer (as mentioned in the readme).

Got it to work by overriding task with my own, where I depend on environment:

namespace :db do
  namespace :migrate do
    desc 'Runs db:migrate in a redis-backed mutex'
    # this change let me load initializers
    task :mutex => :environment do
      TIMEOUT = 5 * 60 * 60

      RedisClassy.redis ||= Redis.current

      RedisMutex.with_lock('db-migrate-mutex', block: TIMEOUT, expire: TIMEOUT) do
        Rake::Task['db:migrate'].invoke
      end
    end
  end
end

Am I missing something or should I open a PR with this change?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.