Skip to content

Instantly share code, notes, and snippets.

@troyk
Created November 3, 2010 07:10
Show Gist options
  • Save troyk/660853 to your computer and use it in GitHub Desktop.
Save troyk/660853 to your computer and use it in GitHub Desktop.
go go multi-tenant via schema
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def ensure_search_path!
return true if existing_path = Thread.current[:search_path] and @schema_search_path == Thread.current[:search_path]
if existing_path
@schema_search_path = Thread.current[:search_path] = existing_path
else
puts "WARNING::SETTING SEARCH_PATH TO SUB1,PUBLIC CUZ Thread.current[:search_path] is nil"
@schema_search_path = Thread.current[:search_path]='sub1,public'
end
@connection.async_exec "SET search_path TO #{@schema_search_path}"
end
def configure_connection
@async = true
@connection.async_exec "SET standard_conforming_strings = on"
@connection.async_exec "SET time zone 'UTC'"
ensure_search_path!
end
# Executes an SQL statement, returning a PGresult object on success
# or raising a PGError exception otherwise.
def execute(sql, name = nil)
ensure_search_path!
log(sql, name) do
@connection.async_exec(sql)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment