Class: ActiveTenant::PostgresAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_tenant/adapters/postgres_adapter.rb

Instance Method Summary collapse

Constructor Details

#initializePostgresAdapter

Returns a new instance of PostgresAdapter.



5
6
7
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 5

def initialize
  self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
end

Instance Method Details

#allObject



9
10
11
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 9

def all
  connection.select_values("SELECT nspname FROM pg_namespace WHERE nspname <> 'information_schema' AND nspname NOT LIKE 'pg%'") - [global]
end

#connection_settings(name) ⇒ Object



42
43
44
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 42

def connection_settings(name)
  connection_config.merge(schema_search_path: name)
end

#create(name) ⇒ Object



13
14
15
16
17
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 13

def create(name)
  unless all.include? name
    connection.execute "CREATE SCHEMA \"#{name}\""
  end
end

#globalObject



50
51
52
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 50

def global
  @global || 'public'
end

#global?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 54

def global?
  global == name
end

#nameObject



46
47
48
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 46

def name
  search_path
end

#remove(name) ⇒ Object



19
20
21
22
23
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 19

def remove(name)
  if all.include? name
    connection.execute "DROP SCHEMA \"#{name}\" CASCADE"
  end
end

#with(name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_tenant/adapters/postgres_adapter.rb', line 25

def with(name)
  return yield if name == search_path

  ex = nil
  current_schema = search_path
  search_path name
  begin
    result = yield
  rescue => e
    ex = e
  ensure
    search_path current_schema
    raise ex unless ex.nil?
    result
  end
end