1 Puppet::Type.newtype(:postgresql_psql) do
4 desc "An arbitrary tag for your own reference; the name of the message."
8 newproperty(:command) do
9 desc 'The SQL command to execute via psql.'
11 defaultto { @resource[:name] }
13 # If needing to run the SQL command, return a fake value that will trigger
14 # a sync, else return the expected SQL command so no sync takes place
16 if @resource.should_run_sql
24 output, status = provider.run_sql_command(value)
25 self.fail("Error executing SQL; psql returned #{status}: '#{output}'") unless status == 0
30 desc "An optional SQL command to execute prior to the main :command; " +
31 "this is generally intended to be used for idempotency, to check " +
32 "for the existence of an object in the database to determine whether " +
33 "or not the main SQL command needs to be executed at all."
35 # Return true if a matching row is found
37 output, status = provider.run_unless_sql_command(value)
38 self.fail("Error evaluating 'unless' clause, returned #{status}: '#{output}'") unless status == 0
40 result_count = output.strip.to_i
41 self.debug("Found #{result_count} row(s) executing 'unless' clause")
47 desc "An optional SQL command to execute prior to the main :command; " +
48 "this is generally intended to be used for idempotency, to check " +
49 "for the existence of an object in the database to determine whether " +
50 "or not the main SQL command needs to be executed at all."
52 # Return true if a matching row is found
54 output, status = provider.run_unless_sql_command(value)
55 status = output.exitcode if status.nil?
57 self.fail("Error evaluating 'onlyif' clause, returned #{status}: '#{output}'") unless status == 0
59 result_count = output.strip.to_i
60 self.debug("Found #{result_count} row(s) executing 'onlyif' clause")
65 newparam(:connect_settings) do
66 desc "Connection settings that will be used when connecting to postgres"
70 desc "The name of the database to execute the SQL command against, this overrides any PGDATABASE value in connect_settings"
74 desc "The port of the database server to execute the SQL command against, this overrides any PGPORT value in connect_settings."
77 newparam(:search_path) do
78 desc "The schema search path to use when executing the SQL command"
81 newparam(:psql_path) do
82 desc "The path to psql executable."
86 newparam(:psql_user) do
87 desc "The system user account under which the psql command should be executed."
91 newparam(:psql_group) do
92 desc "The system user group account under which the psql command should be executed."
96 newparam(:cwd, :parent => Puppet::Parameter::Path) do
97 desc "The working directory under which the psql command should be executed."
101 newparam(:environment) do
102 desc "Any additional environment variables you want to set for a
103 SQL command. Multiple environment variables should be
104 specified as an array."
107 Array(values).each do |value|
108 unless value =~ /\w+=/
109 raise ArgumentError, "Invalid environment setting '#{value}'"
115 newparam(:refreshonly, :boolean => true) do
116 desc "If 'true', then the SQL will only be executed via a notify/subscribe event."
119 newvalues(:true, :false)
122 def should_run_sql(refreshing = false)
123 onlyif_param = @parameters[:onlyif]
124 unless_param = @parameters[:unless]
125 return false if !onlyif_param.nil? && !onlyif_param.value.nil? && !onlyif_param.matches(onlyif_param.value)
126 return false if !unless_param.nil? && !unless_param.value.nil? && unless_param.matches(unless_param.value)
127 return false if !refreshing && @parameters[:refreshonly].value == :true
132 self.property(:command).sync if self.should_run_sql(true)