
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Multiple mlaster/slave helper for ActiveRecord
Based on https://github.com/r7kamura/replicat
Add this line to your application's Gemfile:
gem 'replidog'
And then execute:
$ bundle
Or install it yourself as:
$ gem install replidog
# config/database.yml
production:
adapter: mysql2
encoding: utf8
host: 192.168.24.1
port: 3306
replications:
slave1:
adapter: mysql2
encoding: utf8
host: 192.168.24.2
port: 3306
slave2:
adapter: mysql2
encoding: utf8
host: 192.168.24.3
port: 3306
slave3:
adapter: mysql2
encoding: utf8
host: 192.168.24.4
port: 3306
Now SELECT queries of User model will be sent to slave connections.
# INSERT query is sent to master.
User.create(name: "replicat")
# SELECT query is sent to slave.
User.first
using
can help you specify particular connection.
When you want to send queries to master,
you can use using(:master)
to do that (:master is reserved name for using
method).
When you want to send queries to a particular slave,
you can use the slave's name on database.yml like using(:slave1)
.
# SELECT query is sent to master.
User.using(:master).first
# INSERT query is sent to slave1.
User.using(:slave1).create(name: "replicat")
# :slave1 is used for User connection in the passed block.
User.using(:slave1) { blog.user }
slave connections are balanced by round-robin way.
User.first # sent to slave1
User.first # sent to slave2
User.first # sent to slave3
User.first # sent to slave1
User.first # sent to slave2
User.first # sent to slave3
User.first # sent to slave1
User.first # sent to slave2
User.first # sent to slave3
Pass the master's connection name to replicate
method.
# app/models/recipe.rb
class Recipe < RecipeTable
establish_connection :production_recipe
end
# config/database.yml
production_base:
adapter: mysql2
encoding: utf8
port: 3306
production:
<<: *production_base
host: 192.168.24.1
replications:
slave1:
<<: *slave
host: 192.168.24.2
slave2:
<<: *slave
host: 192.168.24.3
slave3:
<<: *slave
host: 192.168.24.4
production_recipe:
<<: *production_base
host: 192.168.24.5
replications:
slave1:
<<: *slave
host: 192.168.24.6
slave2:
<<: *slave
host: 192.168.24.7
slave3:
<<: *slave
host: 192.168.24.8
If you want to connecto to production_recipe
in multiple models, Create Abstract class and extend it.
# app/models/recipe_table.rb
class RecipeTable < ActiveRecord::Base
self.abstract_class = true
establish_connection :production_recipe
end
# app/models/recipe.rb
class Recipe < RecipeTable
end
To handle all master/slave connections togegher, the methods related with connection management and query cache are overridden. So you don't need to update middlewares and configurations for app servers.
List of overridden methods
# setup gems
bundle install
appraisal install
# setup databases
rake db:prepare
# run tests for current gemfile
rake
# run tests for all appraisals
appraisal rake
FAQs
Unknown package
We found that replidog demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.