Class: Prodder::PG

Inherits:
Object
  • Object
show all
Defined in:
lib/prodder/pg.rb

Constant Summary collapse

PGDumpError =
Class.new(StandardError)
ACL_GRANT =

From pg_dump

/^GRANT /
ACL_REVOKE =
/^REVOKE /
DEFAULT_PRIVILEGES =
/^ALTER DEFAULT PRIVILEGES /
SET_OBJECT_OWNERSHIP =
/.* OWNER TO /

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}) ⇒ PG

Returns a new instance of PG.



11
12
13
# File 'lib/prodder/pg.rb', line 11

def initialize(credentials = {})
  @credentials = credentials
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



9
10
11
# File 'lib/prodder/pg.rb', line 9

def credentials
  @credentials
end

Instance Method Details

#create_db(db_name, sql = nil) ⇒ Object



32
33
34
35
# File 'lib/prodder/pg.rb', line 32

def create_db(db_name, sql = nil)
  run ['createdb', db_name]
  psql db_name, sql if sql
end

#create_role(role, opts = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/prodder/pg.rb', line 15

def create_role(role, opts = [])
  arguments = [
    'createuser',
    '--no-password',
    '--no-superuser',
    '--no-createrole',
    '--no-createdb'
  ]

  arguments.push *opts, role
  run arguments
end

#drop_db(db_name) ⇒ Object



37
38
39
# File 'lib/prodder/pg.rb', line 37

def drop_db(db_name)
  run ['dropdb', db_name]
end

#drop_role(role) ⇒ Object



28
29
30
# File 'lib/prodder/pg.rb', line 28

def drop_role(role)
  run ['dropuser', role]
end

#dump_db_access_control(db_name, user_list, options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/prodder/pg.rb', line 130

def dump_db_access_control(db_name, user_list, options)
  perm_out_sql = ""
  arguments = [
    '--schema-only',
    '--host', credentials['host'],
    '--username', credentials['user']
  ]

  if options[:exclude_schemas].respond_to? :map
    arguments.concat options[:exclude_schemas].map { |schema| ['--exclude-schema', schema] }.flatten
  end

  if options[:exclude_tables].respond_to? :map
    arguments.concat options[:exclude_tables].map { |table| ['--exclude-table', table] }.flatten
  end

  arguments.push db_name

  white_list = options[:included_users] || []
   = (db_name, white_list).map { |user| user['rolname'] }

  run ['pg_dump', *arguments] do |out, err, success|
    out.each_line do |line|
      if line.match(ACL_GRANT)           ||
         line.match(ACL_REVOKE)          ||
         line.match(DEFAULT_PRIVILEGES)  ||
         line.match(SET_OBJECT_OWNERSHIP)

        unless .include?(line.match(/ (\S*);$/)[1])
          perm_out_sql << line
          user_list << (line.match(/ (\S*);$/)[1]).gsub(/"/, '')
        end
      end
    end
    raise PGDumpError.new(err) if !success
  end

  user_list.uniq!
  perm_out_sql
end

#dump_permissions(db_name, filename, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/prodder/pg.rb', line 106

def dump_permissions(db_name, filename, options = {})
  perm_out_sql = ""
  user_list = []

  perm_out_sql << dump_db_access_control(db_name, user_list, options)
  perm_out_sql.prepend pg_dumpall db_name, user_list, options

  perm_out_sql.prepend(alter_role_function)
  perm_out_sql.prepend(create_role_function)
  perm_out_sql.prepend(granted_by_function)
  perm_out_sql.prepend("SET client_min_messages TO WARNING;\n")
  perm_out_sql << drop_role_create_function
  perm_out_sql << drop_role_alter_function
  perm_out_sql << drop_granted_by_function

  File.open(filename, 'w') { |f| f.write perm_out_sql }
end

#dump_settings(db_name, filename) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prodder/pg.rb', line 45

def dump_settings(db_name, filename)
  db_settings = pg_conn(db_name) { |conn| conn.exec("    select unnest(setconfig) as config\n    from pg_catalog.pg_db_role_setting\n    join pg_database on pg_database.oid = setdatabase\n    -- 0 = default, for all users\n    where setrole = 0\n    and datname = '\#{db_name}'\n  SQL\n\n  File.open(filename, 'w') do |f|\n    db_settings.each do |setting|\n      # wipe out all spaces\n      setting.gsub!(/\\s+/, '')\n\n      # if the setting is empty, ignore it\n      unless setting.empty?\n        # else, drop carriage returns/new lines\n        setting.chomp!\n        # and append an empty string if the setting was being assigned a value of nothing\n        setting += \"''\" if setting.match(/=$/)\n        # using the magic of psql variables through :DBNAME\n        f.puts \"ALTER DATABASE :DBNAME SET \#{setting};\"\n      end\n    end\n  end\nend\n").map { |setting| setting['config'] } }

#dump_structure(db_name, filename, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/prodder/pg.rb', line 73

def dump_structure(db_name, filename, options = {})
  arguments = [
    '--schema-only',
    '--no-privileges',
    '--no-owner',
    '--host', credentials['host'],
    '--username', credentials['user']
  ]

  if options[:exclude_schemas].respond_to? :map
    arguments.concat options[:exclude_schemas].map { |schema| ['--exclude-schema', schema] }.flatten
  end

  if options[:exclude_tables].respond_to? :map
    arguments.concat options[:exclude_tables].map { |table| ['--exclude-table', table] }.flatten
  end

  pg_dump filename, arguments.push(db_name)
end

#dump_tables(db_name, tables, filename) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/prodder/pg.rb', line 93

def dump_tables(db_name, tables, filename)
  pg_dump filename, [
    '--data-only',
    '--no-privileges',
    '--no-owner',
    '--disable-triggers',
    '--host',     credentials['host'],
    '--username', credentials['user'],
    *tables.map { |table| ['--table', table] }.flatten,
    db_name
  ]
end

#psql(db_name, sql) ⇒ Object



41
42
43
# File 'lib/prodder/pg.rb', line 41

def psql(db_name, sql)
  run ['psql', db_name], sql
end