Module: Sequel::Postgres::Schemata::DatabaseMethods

Included in:
DatabaseMethods
Defined in:
lib/sequel/postgres/schemata.rb

Instance Method Summary collapse

Instance Method Details

#current_schemataObject

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



51
52
53
54
55
# File 'lib/sequel/postgres/schemata.rb', line 51

def current_schemata
  extension :pg_array
  .select(Sequel::function(:current_schemas, false).
    cast('varchar[]')).single_value.map(&:to_sym)
end

#rename_schema(from, to) ⇒ Object

Renames a schema



58
59
60
# 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

#schemataObject

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



12
13
14
# File 'lib/sequel/postgres/schemata.rb', line 12

def schemata
  .select(:nspname).from(:pg_namespace).map(:nspname).map(&:to_sym)
end

#search_path(*a, prepend: false, &block) ⇒ Object

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.



22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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