module Sequel::Postgres::Schemata::DatabaseMethods

Constants

RENAME_SCHEMA_SQL
SCHEMA_SCAN_RE
SCHEMA_SUB_RE
SHOW_SEARCH_PATH

Public Instance Methods

current_schemata() click to toggle source

Returns the current schemata, as returned by current_schemas(false).

# File lib/sequel/postgres/schemata.rb, line 51
def current_schemata
  extension :pg_array
  metadata_dataset.select(Sequel::function(:current_schemas, false).
    cast('varchar[]')).single_value.map(&:to_sym)
end
rename_schema(from, to) click to toggle source

Renames a schema

# File lib/sequel/postgres/schemata.rb, line 58
def rename_schema from, to
  self << RENAME_SCHEMA_SQL % [from.to_s.gsub('"', '""'), to.to_s.gsub('"', '""')]
end
schemata() click to toggle source

List all existing schematas (including the system ones). Returns a symbol list.

# File lib/sequel/postgres/schemata.rb, line 12
def schemata
  metadata_dataset.select(:nspname).from(:pg_namespace).map(:nspname).map(&:to_sym)
end
search_path(*a, prepend: false, &block) click to toggle source

Returns a symbol list containing the current search path. Note that the search path can contain non-existent schematas. If given a block and an argument, instead temporarily changes the search path inside the block. It also accepts several arguments, in which case it treats them as an array of schemata to put in search path. If you use prepend: true, it prepends any given schemata to the current search path.

# File lib/sequel/postgres/schemata.rb, line 22
def search_path *a, prepend: false, &block
  if block_given?
    a = a.flatten
    a += search_path if prepend
    run_with_search_path a, &block
  else
    get_search_path
  end
end
search_path=(search_path) click to toggle source

Sets the search path. Starting with Postgres 9.2 it can contain non-existent schematas. Accepted formats include a single symbol, a single string (split on ,) and lists of symbols or strings.

# File lib/sequel/postgres/schemata.rb, line 36
def search_path= search_path
  case search_path
  when String
    search_path = search_path.split(",").map{|s| s.strip}
  when Symbol
    search_path = [search_path]
  when Array
    # nil
  else
    raise Error, "unrecognized value for search_path: #{search_path.inspect}"
  end
  self << "SET search_path = #{search_path.map{|s| "\"#{s.to_s.gsub('"', '""')}\""}.join(',')}"
end

Private Instance Methods

get_search_path() click to toggle source
# File lib/sequel/postgres/schemata.rb, line 64
def get_search_path
  metadata_dataset.with_sql(SHOW_SEARCH_PATH).
    single_value.scan(SCHEMA_SCAN_RE).flatten.
    map{|s|s.strip.sub(SCHEMA_SUB_RE, '\1').gsub('""', '"').to_sym}
end
run_with_search_path(path) { || ... } click to toggle source
# File lib/sequel/postgres/schemata.rb, line 70
def run_with_search_path path, &block
  old_path = search_path

  begin
    self.search_path = path
    yield
  ensure
    self.search_path = old_path
  end
end