Skip to content

Commit d9e9162

Browse files
Merge pull request #349 from RandomNoun7/tickets/FM-8879-handle-t-sql-errors
(FM-8879) Handle T-SQL Errors Properly
2 parents 8d44d62 + d741bd7 commit d9e9162

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/puppet/property/sqlserver_tsql.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Puppet::Property::SqlserverTsql < Puppet::Property # rubocop:disable Style
66
quoted_value = value.gsub('\'', '\'\'')
77
erb_template = <<-TEMPLATE
88
BEGIN TRY
9+
SET NOCOUNT ON
910
DECLARE @sql_text as NVARCHAR(max);
1011
SET @sql_text = N'#{quoted_value}'
1112
EXECUTE sp_executesql @sql_text;

spec/acceptance/sqlserver_tsql_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,37 @@ def ensure_sqlserver_database(db_name, _ensure_val = 'present')
162162
run_sql_query(run_sql_query_opts)
163163
end
164164

165+
it 'Run sqlserver_tsql WITH onlyif that does a table insert:' do
166+
# Initilize a new table name:
167+
@table_name = 'Table_' + SecureRandom.hex(3)
168+
@query = "USE #{db_name}; SELECT * FROM #{@table_name} WHERE id = 2;"
169+
pp = <<-MANIFEST
170+
sqlserver::config{'MSSQLSERVER':
171+
instance_name => 'MSSQLSERVER',
172+
admin_user => 'sa',
173+
admin_pass => 'Pupp3t1@',
174+
}
175+
sqlserver_tsql{'testsqlserver_tsql':
176+
instance => 'MSSQLSERVER',
177+
database => '#{db_name}',
178+
command => "INSERT #{@table_name} VALUES(2, 'name2', '[email protected]');",
179+
onlyif => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));
180+
INSERT #{@table_name} VALUES(1, 'name', '[email protected]');
181+
THROW 5300, 'Throw to trigger second INSERT statement in command property', 10"
182+
}
183+
MANIFEST
184+
apply_manifest(pp, catch_failures: true)
185+
186+
puts "Validate a row is inserted into #{@table_name} by the command:"
187+
run_sql_query_opts = {
188+
query: @query,
189+
sql_admin_user: @admin_user,
190+
sql_admin_pass: @admin_pass,
191+
expected_row_count: 1,
192+
}
193+
run_sql_query(run_sql_query_opts)
194+
end
195+
165196
it 'Negative test: Run tsql with invalid command:' do
166197
pp = <<-MANIFEST
167198
sqlserver::config{'MSSQLSERVER':

spec/unit/puppet/property/tsql_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
it 'munges value to have begin and end try' do
1414
@node[:command] = 'function foo'
1515
@node[:onlyif] = 'exec bar'
16-
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})
17-
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})
16+
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})
17+
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})
1818
end
1919

2020
it 'properlies escape single quotes in queries' do

0 commit comments

Comments
 (0)