Class: Posgra::Client

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

Constant Summary collapse

DEFAULT_EXCLUDE_SCHEMA =
/\A(?:pg_.*|information_schema)\z/
DEFAULT_EXCLUDE_ROLE =
/\A\z/
DEFAULT_EXCLUDE_DATABASE =
/\A(?:template\d+|postgres)\z/

Instance Method Summary collapse

Methods included from Utils::Helper

#matched?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/posgra/client.rb', line 8

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_ROLE
    )
  else
    options[:exclude_role] = DEFAULT_EXCLUDE_ROLE
  end

  if options[:exclude_database]
    options[:exclude_database] = Regexp.union(
      options[:exclude_database],
      DEFAULT_EXCLUDE_DATABASE
    )
  else
    options[:exclude_database] = DEFAULT_EXCLUDE_DATABASE
  end

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

Instance Method Details

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



93
94
95
96
# File 'lib/posgra/client.rb', line 93

def apply_databases(file, options = {})
  options = @options.merge(options)
  walk_for_database_grants(file, options)
end

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



88
89
90
91
# File 'lib/posgra/client.rb', line 88

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

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



83
84
85
86
# File 'lib/posgra/client.rb', line 83

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

#closeObject



98
99
100
# File 'lib/posgra/client.rb', line 98

def close
  @client.close
end

#export_databases(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/posgra/client.rb', line 65

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

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

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

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

#export_grants(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/posgra/client.rb', line 47

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



41
42
43
44
45
# File 'lib/posgra/client.rb', line 41

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