Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ irb(main):001:0> User.pgpool_nlb.all
/*NO LOAD BALANCE*/ SELECT "users".* FROM "users" LIMIT $1 [["LIMIT", 11]]
```

### blocks

Use `PgpoolNoLoadBalance.force` block to force all read queries to run with `pgpool_nlb` enabled inside the block

```rb
irb(main):001:0> PgpoolNoLoadBalance.force { User.all }
/*NO LOAD BALANCE*/ SELECT "users".* FROM "users" LIMIT $1 [["LIMIT", 11]]
```

### unscope

Can remove the scope with the unscope method.
Expand Down
11 changes: 11 additions & 0 deletions lib/pgpool_no_load_balance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ module PgpoolNoLoadBalance

class PostgreSQLAdapterMissing < StandardError; end

def self.force
Thread.current[:pgpool_nlb_force] = true
yield
ensure
Thread.current[:pgpool_nlb_force] = false
end

def self.force?
!!Thread.current[:pgpool_nlb_force]
end

def self.setup!
unless ::ActiveRecord::Base.respond_to?(:postgresql_connection)
raise PostgreSQLAdapterMissing, "No postgresql adapter specified by 'config/database.yml', or 'ActiveRecord::Base.establish_connection' method is not called."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def execute(sql, name = nil, pgpool_nlb: false)

def to_sql_and_binds(arel_or_sql_string, binds = [], preparable = nil) # :nodoc:
sql, binds, preparable = super
if arel_or_sql_string.respond_to?(:pgpool_nlb?) && arel_or_sql_string.pgpool_nlb?
if PgpoolNoLoadBalance.force? || (arel_or_sql_string.respond_to?(:pgpool_nlb?) && arel_or_sql_string.pgpool_nlb?)
sql = "#{NLB_COMMENT} #{sql}"
end
[sql.freeze, binds, preparable]
Expand Down
2 changes: 1 addition & 1 deletion lib/pgpool_no_load_balance/arel/select_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def pgpool_nlb(value = true)
end

def pgpool_nlb?
@pgpool_nlb_flag
@pgpool_nlb_flag || PgpoolNoLoadBalance.force?
end
end
end
Expand Down