Skip to content

(FM-8879) Handle T-SQL Errors Properly #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/puppet/property/sqlserver_tsql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions spec/acceptance/sqlserver_tsql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]');",
onlyif => "CREATE TABLE #{@table_name} (id INT, name VARCHAR(20), email VARCHAR(20));
INSERT #{@table_name} VALUES(1, 'name', '[email protected]');
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':
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/puppet/property/tsql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down