1 require "chef/mixin/shell_out"
5 include Chef::Mixin::ShellOut
8 :select, :insert, :update, :delete, :truncate, :references, :trigger
11 SEQUENCE_PRIVILEGES = [
12 :usage, :select, :update
15 def initialize(cluster)
20 @cluster.split("/").first.to_f
24 # Create argument array
28 args.push("--cluster")
32 args.push("--no-align") unless options.fetch(:align, true)
34 # Add any SQL command to execute
36 args.push("--command")
37 args.push(options[:command])
40 # Add any file to execute SQL commands from
43 args.push(options[:file])
46 # Add the database name
47 args.push(options[:database] || "template1")
49 # Get the user and group to run as
50 user = options[:user] || "postgres"
51 group = options[:group] || "postgres"
54 shell_out!("/usr/bin/psql", *args, :user => user, :group => group)
57 def query(sql, options = {})
59 result = execute(options.merge(:command => sql, :align => false))
61 # Split the output into lines
62 lines = result.stdout.split("\n")
64 # Remove the "(N rows)" line from the end
68 fields = lines.shift.split("|")
70 # Extract the record data
71 lines.collect do |line|
73 fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
79 @users ||= query("SELECT *, ARRAY(SELECT groname FROM pg_group WHERE usesysid = ANY(grolist)) AS roles FROM pg_user").each_with_object({}) do |user, users|
80 users[user[:usename]] = {
81 :superuser => user[:usesuper] == "t",
82 :createdb => user[:usercreatedb] == "t",
83 :createrole => user[:usecatupd] == "t",
84 :replication => user[:userepl] == "t",
85 :roles => parse_array(user[:roles] || "{}")
91 @databases ||= query("SELECT d.datname, u.usename, d.encoding, d.datcollate, d.datctype FROM pg_database AS d INNER JOIN pg_user AS u ON d.datdba = u.usesysid").each_with_object({}) do |database, databases|
92 databases[database[:datname]] = {
93 :owner => database[:usename],
94 :encoding => database[:encoding],
95 :collate => database[:datcollate],
96 :ctype => database[:datctype]
101 def extensions(database)
103 @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
104 extensions[extension[:extname]] = {
105 :version => extension[:extversion]
111 @tablespaces ||= query("SELECT spcname, usename FROM pg_tablespace AS t INNER JOIN pg_user AS u ON t.spcowner = u.usesysid").each_with_object({}) do |tablespace, tablespaces|
112 tablespaces[tablespace[:spcname]] = {
113 :owner => tablespace[:usename]
120 @tables[database] ||= query("SELECT n.nspname, c.relname, u.usename, c.relacl FROM pg_class AS c INNER JOIN pg_user AS u ON c.relowner = u.usesysid INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid WHERE n.nspname NOT IN ('pg_catalog', 'information_schema') AND c.relkind = 'r'", :database => database).each_with_object({}) do |table, tables|
121 name = "#{table[:nspname]}.#{table[:relname]}"
124 :owner => table[:usename],
125 :permissions => parse_acl(table[:relacl] || "{}")
130 def sequences(database)
132 @sequences[database] ||= query("SELECT n.nspname, c.relname, u.usename, c.relacl FROM pg_class AS c INNER JOIN pg_user AS u ON c.relowner = u.usesysid INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid WHERE n.nspname NOT IN ('pg_catalog', 'information_schema') AND c.relkind = 'S'", :database => database).each_with_object({}) do |sequence, sequences|
133 name = "#{sequence[:nspname]}.#{sequence[:relname]}"
136 :owner => sequence[:usename],
137 :permissions => parse_acl(sequence[:relacl] || "{}")
144 def parse_array(array)
145 array.sub(/^\{(.*)\}$/, "\\1").split(",")
149 parse_array(acl).each_with_object({}) do |entry, permissions|
150 entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
151 user, privileges = entry.split("=")
153 user = user.sub(/^"(.*)"$/, "\\1")
154 user = "public" if user == ""
156 permissions[user] = {
157 "r" => :select, "a" => :insert, "w" => :update, "d" => :delete,
158 "D" => :truncate, "x" => :references, "t" => :trigger,
159 "C" => :create, "c" => :connect, "T" => :temporary,
160 "X" => :execute, "U" => :usage, "s" => :set, "A" => :alter_system
161 }.values_at(*privileges.chars).compact