diff --git a/lib/puppet/property/sqlserver_tsql.rb b/lib/puppet/property/sqlserver_tsql.rb index 254ab26d..8063af18 100644 --- a/lib/puppet/property/sqlserver_tsql.rb +++ b/lib/puppet/property/sqlserver_tsql.rb @@ -6,6 +6,7 @@ class Puppet::Property::SqlserverTsql < Puppet::Property # rubocop:disable Style quoted_value = value.gsub('\'', '\'\'') erb_template = <<-TEMPLATE BEGIN TRY + SET NOCOUNT ON DECLARE @sql_text as NVARCHAR(max); SET @sql_text = N'#{quoted_value}' EXECUTE sp_executesql @sql_text; diff --git a/spec/acceptance/sqlserver_tsql_spec.rb b/spec/acceptance/sqlserver_tsql_spec.rb index 4a641f28..9f0ebc5c 100644 --- a/spec/acceptance/sqlserver_tsql_spec.rb +++ b/spec/acceptance/sqlserver_tsql_spec.rb @@ -162,6 +162,37 @@ def ensure_sqlserver_database(db_name, _ensure_val = 'present') run_sql_query(run_sql_query_opts) end + it 'Run sqlserver_tsql WITH onlyif that does a table insert:' do + # Initilize a new table name: + @table_name = 'Table_' + SecureRandom.hex(3) + @query = "USE #{db_name}; SELECT * FROM #{@table_name} WHERE id = 2;" + pp = <<-MANIFEST + sqlserver::config{'MSSQLSERVER': + instance_name => 'MSSQLSERVER', + admin_user => 'sa', + admin_pass => 'Pupp3t1@', + } + sqlserver_tsql{'testsqlserver_tsql': + instance => 'MSSQLSERVER', + database => '#{db_name}', + command => "INSERT #{@table_name} VALUES(2, 'name2', 'email2@domain.tld');", + onlyif => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20)); + INSERT #{@table_name} VALUES(1, 'name', 'email@domain.tld'); + THROW 5300, 'Throw to trigger second INSERT statement in command property', 10" + } + MANIFEST + apply_manifest(pp, catch_failures: true) + + puts "Validate a row is inserted into #{@table_name} by the command:" + run_sql_query_opts = { + query: @query, + sql_admin_user: @admin_user, + sql_admin_pass: @admin_pass, + expected_row_count: 1, + } + run_sql_query(run_sql_query_opts) + end + it 'Negative test: Run tsql with invalid command:' do pp = <<-MANIFEST sqlserver::config{'MSSQLSERVER': diff --git a/spec/unit/puppet/property/tsql_spec.rb b/spec/unit/puppet/property/tsql_spec.rb index 4413db67..fbd80921 100644 --- a/spec/unit/puppet/property/tsql_spec.rb +++ b/spec/unit/puppet/property/tsql_spec.rb @@ -13,8 +13,8 @@ it 'munges value to have begin and end try' do @node[:command] = 'function foo' @node[:onlyif] = 'exec bar' - expect(@node[:onlyif]).to match(%r{BEGIN TRY\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'exec bar'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY}) - expect(@node[:command]).to match(%r{BEGIN TRY\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'function foo'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY}) + expect(@node[:onlyif]).to match(%r{BEGIN TRY\n\s+SET NOCOUNT ON\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'exec bar'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY}) + expect(@node[:command]).to match(%r{BEGIN TRY\n\s+SET NOCOUNT ON\n\s+DECLARE @sql_text as NVARCHAR\(max\);\n\s+SET @sql_text = N'function foo'\n\s+EXECUTE sp_executesql @sql_text;\nEND TRY}) end it 'properlies escape single quotes in queries' do