Class: Posgra::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/posgra/client.rb

Constant Summary collapse

DEFAULT_EXCLUDE_SCHEMA =
/\A(?:pg_.*|information_schema)\z/
DEFAULT_EXCLUDE_ROLE =
/\A\z/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/posgra/client.rb', line 5

def initialize(options = {})
  if options[:exclude_schema]
    options[:exclude_schema] = Regexp.union(
      options[:exclude_schema],
      DEFAULT_EXCLUDE_SCHEMA
    )
  else
    options[:exclude_schema] = DEFAULT_EXCLUDE_SCHEMA
  end

  if options[:exclude_role]
    options[:exclude_role] = Regexp.union(
      options[:exclude_role],
      DEFAULT_EXCLUDE_SCHEMA
    )
  else
    options[:exclude_role] = DEFAULT_EXCLUDE_ROLE
  end

  @options = options
  @client = connect(options)
  @driver = Posgra::Driver.new(@client, options)
end

Instance Method Details

#apply_grants(file, options = {}) ⇒ Object



59
60
61
62
# File 'lib/posgra/client.rb', line 59

def apply_grants(file, options = {})
  options = @options.merge(options)
  walk_for_grants(file, options)
end

#apply_roles(file, options = {}) ⇒ Object



54
55
56
57
# File 'lib/posgra/client.rb', line 54

def apply_roles(file, options = {})
  options = @options.merge(options)
  walk_for_roles(file, options)
end

#closeObject



64
65
66
# File 'lib/posgra/client.rb', line 64

def close
  @client.close
end

#export_grants(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/posgra/client.rb', line 35

def export_grants(options = {})
  options = @options.merge(options)
  exported = Posgra::Exporter.export_grants(@driver, options)

  if options[:split]
    dsl_h = Hash.new {|hash, key| hash[key] = {} }


    exported.each do |role, schemas|
      dsl = Posgra::DSL.convert_grants({role => schemas}, options)
      dsl_h[role] = dsl
    end

    dsl_h
  else
    Posgra::DSL.convert_grants(exported, options)
  end
end

#export_roles(options = {}) ⇒ Object



29
30
31
32
33
# File 'lib/posgra/client.rb', line 29

def export_roles(options = {})
  options = @options.merge(options)
  exported = Posgra::Exporter.export_roles(@driver, options)
  Posgra::DSL.convert_roles(exported, options)
end