Skip to content

Commit

Permalink
Merge pull request jruby#200 from dryade/master
Browse files Browse the repository at this point in the history
Implements dynamic search_path management and Add support for template in PostgreSQLAdapter
  • Loading branch information
nicksieger committed Jul 28, 2012
2 parents f6ab0a0 + 0bf424b commit dc0c0ee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/arjdbc/postgresql/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ def multi_column_index_limit

# Based on postgresql_adapter.rb
def indexes(table_name, name = nil)
schema_search_path = @config[:schema_search_path] || select_rows('SHOW search_path')[0][0]
schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
result = select_rows(<<-SQL, name)
SELECT i.relname, d.indisunique, a.attname, a.attnum, d.indkey
Expand All @@ -426,7 +425,7 @@ def indexes(table_name, name = nil)
AND d.indisprimary = 'f'
AND t.oid = d.indrelid
AND t.relname = '#{table_name}'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
AND a.attrelid = t.oid
AND d.indkey[s.i]=a.attnum
ORDER BY i.relname
Expand Down Expand Up @@ -472,7 +471,10 @@ def recreate_database(name)
end

def create_database(name, options = {})
execute "CREATE DATABASE \"#{name}\" ENCODING='#{options[:encoding] || 'utf8'}'"
options = options.with_indifferent_access
create_query = "CREATE DATABASE \"#{name}\" ENCODING='#{options[:encoding] || 'utf8'}'"
create_query << " TEMPLATE=#{options[:template]}" if options[:template].present?
execute create_query
end

def drop_database(name)
Expand Down Expand Up @@ -504,8 +506,7 @@ def structure_dump
ENV['PGHOST'] = @config[:host] if @config[:host]
ENV['PGPORT'] = @config[:port].to_s if @config[:port]
ENV['PGPASSWORD'] = @config[:password].to_s if @config[:password]
search_path = @config[:schema_search_path]
search_path = "--schema=#{search_path}" if search_path
search_path = "--schema=#{schema_search_path}" if schema_search_path

@connection.connection.close
begin
Expand All @@ -519,6 +520,28 @@ def structure_dump
end
end

# Sets the schema search path to a string of comma-separated schema names.
# Names beginning with $ have to be quoted (e.g. $user => '$user').
# See: http://www.postgresql.org/docs/current/static/ddl-schemas.html
#
# This should be not be called manually but set in database.yml.
def schema_search_path=(schema_csv)
if schema_csv
execute "SET search_path TO #{schema_csv}"
@schema_search_path = schema_csv
end
end

# Returns the active schema search path.
def schema_search_path
@schema_search_path ||= exec_query('SHOW search_path', 'SCHEMA')[0]['search_path']
end

# Returns the current schema name.
def current_schema
exec_query('SELECT current_schema', 'SCHEMA')[0]["current_schema"]
end

# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
#
# PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
Expand Down
12 changes: 12 additions & 0 deletions test/postgres_simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ def test_adapter_class_name_equals_native_adapter_class_name
assert_equal 'PostgreSQLAdapter', classname
end

def test_schema_search_path
assert_equal @connection.schema_search_path, "\"$user\",public"
end

def test_schema_search_path
assert_equal @connection.schema_search_path, "\"$user\",public"
end

def test_current_schema
assert_equal @connection.current_schema, "public"
end

def test_encoding
assert_not_nil @connection.encoding
end
Expand Down

0 comments on commit dc0c0ee

Please sign in to comment.