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 * 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"
90 @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|
91 databases[database[:datname]] = {
92 :owner => database[:usename],
93 :encoding => database[:encoding],
94 :collate => database[:datcollate],
95 :ctype => database[:datctype]
100 def extensions(database)
102 @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
103 extensions[extension[:extname]] = {
104 :version => extension[:extversion]
110 @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|
111 tablespaces[tablespace[:spcname]] = {
112 :owner => tablespace[:usename]
119 @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 c.relkind = 'r'", :database => database).each_with_object({}) do |table, tables|
120 name = "#{table[:nspname]}.#{table[:relname]}"
123 :owner => table[:usename],
124 :permissions => parse_acl(table[:relacl] || "{}")
129 def sequences(database)
131 @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 c.relkind = 'S'", :database => database).each_with_object({}) do |sequence, sequences|
132 name = "#{sequence[:nspname]}.#{sequence[:relname]}"
135 :owner => sequence[:usename],
136 :permissions => parse_acl(sequence[:relacl] || "{}")
144 acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
145 entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
146 user, privileges = entry.split("=")
148 user = user.sub(/^"(.*)"$/, "\\1")
149 user = "public" if user == ""
151 permissions[user] = {
152 "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
153 "D" => :truncate, "x" => :references, "t" => :trigger
154 }.values_at(*privileges.chars).compact