class MultitenancyTools::SchemaSwitcher
{SchemaSwitcher} can be used to switch between PostgreSQL schemas on a connection. It uses PostgreSQL search_path to achieve this functionality.
Public Class Methods
new(schema, connection = ActiveRecord::Base.connection)
click to toggle source
@param schema [String] schema name @param connection [ActiveRecord::ConnectionAdapters::PostgreSQLAdapter] connection adapter
# File lib/multitenancy_tools/schema_switcher.rb, line 7 def initialize(schema, connection = ActiveRecord::Base.connection) @connection = connection @schema = @connection.quote(schema) end
Public Instance Methods
run() { || ... }
click to toggle source
This sets the connection search_path to use only the current schema, yields the block and then change search_path back to its previous value.
# File lib/multitenancy_tools/schema_switcher.rb, line 14 def run(&block) original_path = @connection.schema_search_path set_path_if_required(@schema) yield ensure set_path_if_required(original_path) end
Protected Instance Methods
set_path_if_required(schema)
click to toggle source
Change search_path only if the current search_path is different. This avoids unnecessary queries to change the connection search_path when it already has the desired value.
# File lib/multitenancy_tools/schema_switcher.rb, line 27 def set_path_if_required(schema) return if @connection.schema_search_path == schema @connection.schema_search_path = schema end