1 require "chef/mixin/shell_out"
 
   5     include Chef::Mixin::ShellOut
 
   8       :select, :insert, :update, :delete, :truncate, :references, :trigger
 
  11     def initialize(cluster)
 
  16       # Create argument array
 
  20       args.push("--cluster")
 
  24       args.push("--no-align") unless options.fetch(:align, true)
 
  26       # Add any SQL command to execute
 
  28         args.push("--command")
 
  29         args.push(options[:command])
 
  32       # Add any file to execute SQL commands from
 
  35         args.push(options[:file])
 
  38       # Add the database name
 
  39       args.push(options[:database] || "template1")
 
  41       # Get the user and group to run as
 
  42       user = options[:user] || "postgres"
 
  43       group = options[:group] || "postgres"
 
  46       shell_out!("/usr/bin/psql", *args, :user => user, :group => group)
 
  49     def query(sql, options = {})
 
  51       result = execute(options.merge(:command => sql, :align => false))
 
  53       # Split the output into lines
 
  54       lines = result.stdout.split("\n")
 
  56       # Remove the "(N rows)" line from the end
 
  60       fields = lines.shift.split("|")
 
  62       # Extract the record data
 
  63       lines.collect do |line|
 
  65         fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
 
  71       @users ||= query("SELECT * FROM pg_user").each_with_object({}) do |user, users|
 
  72         users[user[:usename]] = {
 
  73           :superuser => user[:usesuper] == "t",
 
  74           :createdb => user[:usercreatedb] == "t",
 
  75           :createrole => user[:usecatupd] == "t",
 
  76           :replication => user[:userepl] == "t"
 
  82       @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|
 
  83         databases[database[:datname]] = {
 
  84           :owner => database[:usename],
 
  85           :encoding => database[:encoding],
 
  86           :collate => database[:datcollate],
 
  87           :ctype => database[:datctype]
 
  92     def extensions(database)
 
  94       @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
 
  95         extensions[extension[:extname]] = {
 
  96           :version => extension[:extversion]
 
 102       @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|
 
 103         tablespaces[tablespace[:spcname]] = {
 
 104           :owner => tablespace[:usename]
 
 111       @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", :database => database).each_with_object({}) do |table, tables|
 
 112         name = "#{table[:nspname]}.#{table[:relname]}"
 
 115           :owner => table[:usename],
 
 116           :permissions => parse_acl(table[:relacl] || "{}")
 
 124       acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
 
 125         entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
 
 126         user, privileges = entry.split("=")
 
 128         user = user.sub(/^"(.*)"$/, "\\1")
 
 129         user = "public" if user == ""
 
 131         permissions[user] = {
 
 132           "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
 
 133           "D" => :truncate, "x" => :references, "t" => :trigger
 
 134         }.values_at(*privileges.chars).compact