Created
November 3, 2010 07:10
-
-
Save troyk/660853 to your computer and use it in GitHub Desktop.
go go multi-tenant via schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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