Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicksieger/957380 to your computer and use it in GitHub Desktop.
Save nicksieger/957380 to your computer and use it in GitHub Desktop.
From e2da1eee5d20e438d8f72e83064921b4516a074e Mon Sep 17 00:00:00 2001
From: schreiber <schreiber@esda.com>
Date: Wed, 4 May 2011 16:57:57 +0200
Subject: [PATCH 1/2] support multiple schema in search_path
---
lib/arjdbc/postgresql/adapter.rb | 15 ++++++++++++++-
test/postgres_schema_search_path_test.rb | 6 +++++-
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/lib/arjdbc/postgresql/adapter.rb b/lib/arjdbc/postgresql/adapter.rb
index 945ca80..e94c76d 100644
--- a/lib/arjdbc/postgresql/adapter.rb
+++ b/lib/arjdbc/postgresql/adapter.rb
@@ -280,7 +280,20 @@ module ::ArJdbc
table_name = parts.pop
schema_name = parts.join(".")
end
- @connection.columns_internal(table_name, name, schema_name)
+ schema_list = if schema_name.nil?
+ []
+ else
+ schema_name.split(/\s*,\s*/)
+ end
+ while schema_list.size > 1
+ s = schema_list.shift
+ begin
+ return @connection.columns_internal(table_name, name, s)
+ rescue ActiveRecord::JDBCError=>ignored_for_next_schema
+ end
+ end
+ s = schema_list.shift
+ return @connection.columns_internal(table_name, name, s)
end
# From postgresql_adapter.rb
diff --git a/test/postgres_schema_search_path_test.rb b/test/postgres_schema_search_path_test.rb
index 112bc6d..a4d493a 100644
--- a/test/postgres_schema_search_path_test.rb
+++ b/test/postgres_schema_search_path_test.rb
@@ -16,7 +16,7 @@ class CreateSchema < ActiveRecord::Migration
end
class Person < ActiveRecord::Base
- establish_connection POSTGRES_CONFIG.merge(:schema_search_path => 'test')
+ establish_connection POSTGRES_CONFIG.merge(:schema_search_path => 'test,public')
end
class PostgresSchemaSearchPathTest < Test::Unit::TestCase
@@ -41,4 +41,8 @@ class PostgresSchemaSearchPathTest < Test::Unit::TestCase
Person.find_by_wrongname("Alex")
end
end
+ def test_column_information
+ assert Person.columns.map{|col| col.name}.include?("name")
+ assert !Person.columns.map{|col| col.name}.include?("wrongname")
+ end
end
--
1.7.2.5
From 4cae040daf1028836926ae886295f606e367030a Mon Sep 17 00:00:00 2001
From: Daniel Schreiber <schreiber@esda.com>
Date: Wed, 4 May 2011 17:38:12 +0200
Subject: [PATCH 2/2] Handle search_path for list of tables
This matches the behaviour of
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
---
lib/arjdbc/postgresql/adapter.rb | 9 ++++++++-
1 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/lib/arjdbc/postgresql/adapter.rb b/lib/arjdbc/postgresql/adapter.rb
index e94c76d..cf54465 100644
--- a/lib/arjdbc/postgresql/adapter.rb
+++ b/lib/arjdbc/postgresql/adapter.rb
@@ -563,7 +563,14 @@ module ::ArJdbc
end
def tables
- @connection.tables(database_name, nil, nil, ["TABLE"])
+ schema_name = @config[:schema_search_path]
+ if schema_name
+ schema_name.split(/\s*,\s*/).map{|schema|
+ @connection.tables(database_name, schema, nil, ["TABLE"])
+ }.inject([]){|result, item| result.concat(item)}
+ else
+ @connection.tables(database_name, nil, nil, ["TABLE"])
+ end
end
private
--
1.7.2.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment