Module: Sequel::SearchPath

Defined in:
lib/sequel/extensions/search_path.rb,
lib/sequel/extensions/search_path/version.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Instance Method Details

#active_schemaObject

The schema that new objects will be created in.



41
42
43
# File 'lib/sequel/extensions/search_path.rb', line 41

def active_schema
  schemas.first
end

#override_schema(*new_schemas, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sequel/extensions/search_path.rb', line 11

def override_schema(*new_schemas, &block)
  synchronize do
    previous_schemas = schemas

    begin
      self.schemas = new_schemas
      yield
    ensure
      self.schemas = previous_schemas
    end
  end
end

#schemasObject



24
25
26
# File 'lib/sequel/extensions/search_path.rb', line 24

def schemas
  Thread.current[schemas_key] ||= [:public]
end

#schemas=(schemas) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sequel/extensions/search_path.rb', line 28

def schemas=(schemas)
  schemas = schemas.map(&:to_sym).uniq
  Thread.current[schemas_key] = schemas

  # Set the search_path in Postgres, unless it's in transaction rollback.
  # If it is, the search_path will be reset for us anyway, and the SQL
  # call will just raise another error.
  unless synchronize(&:transaction_status) == PG::PQTRANS_INERROR
    set_search_path(schemas)
  end
end

#search_pathObject Also known as: show_search_path



45
46
47
# File 'lib/sequel/extensions/search_path.rb', line 45

def search_path
  self["SHOW search_path"].get
end

#use_schema(*new_schemas, &block) ⇒ Object



7
8
9
# File 'lib/sequel/extensions/search_path.rb', line 7

def use_schema(*new_schemas, &block)
  synchronize { override_schema(*(new_schemas + schemas), &block) }
end