Skip to content

Commit f7b31b7

Browse files
committed
Merge pull request #112 from cyberious/DatabaseTsql
(FM-2791) Add database param to sqlserver_tsql
2 parents cb53ed5 + 2805808 commit f7b31b7

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

lib/puppet/provider/sqlserver_tsql/mssql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def run(query)
88
debug("Running resource #{query} against #{resource[:instance]}")
9-
config = get_config
9+
config = get_config.merge({ :database => resource[:database] })
1010
sqlconn = PuppetX::Sqlserver::SqlConnection.new
1111

1212
sqlconn.open_and_run_command(query, config)

lib/puppet/type/sqlserver_tsql.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def self.checks
2828
end
2929
end
3030

31+
newparam(:database) do
32+
defaultto 'master'
33+
validate do |value|
34+
fail("Invalid database name #{value}") unless /^[[:word:]|#|@]+/.match(value)
35+
end
36+
end
37+
3138
desc 'SQL Query to run and only run if exits with non-zero'
3239
newcheck(:onlyif, :parent => Puppet::Property::SqlserverTsql) do
3340
#Runs in the event that our TSQL exits with anything other than 0
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'spec_helper'
2+
require 'mocha'
3+
4+
5+
RSpec.describe Puppet::Type.type(:sqlserver_tsql).provider(:mssql) do
6+
subject { described_class }
7+
let(:config) { {:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'} }
8+
9+
def stub_open_and_run(query, config)
10+
sqlconn = mock()
11+
sqlconn.expects(:open_and_run_command).with(gen_query(query), config)
12+
PuppetX::Sqlserver::SqlConnection.expects(:new).returns(sqlconn)
13+
end
14+
15+
def create_sqlserver_tsql(args)
16+
@resource = Puppet::Type::Sqlserver_tsql.new(args)
17+
@provider = subject.new(@resource)
18+
end
19+
20+
def stub_get_instance_config(config)
21+
@provider.expects(:get_config).returns(config)
22+
end
23+
24+
def gen_query(query)
25+
<<-PP
26+
BEGIN TRY
27+
#{query}
28+
END TRY
29+
BEGIN CATCH
30+
DECLARE @msg as VARCHAR(max);
31+
SELECT @msg = 'THROW CAUGHT: ' + ERROR_MESSAGE();
32+
THROW 51000, @msg, 10
33+
END CATCH
34+
PP
35+
end
36+
37+
context 'run_update' do
38+
describe 'against non master database' do
39+
it {
40+
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER', :database => 'myDb'})
41+
stub_get_instance_config(config)
42+
stub_open_and_run('whacka whacka', config.merge({:database => 'myDb'}))
43+
44+
@provider.run_update
45+
}
46+
end
47+
describe 'against default database' do
48+
it {
49+
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :instance => 'MSSQLSERVER'})
50+
stub_get_instance_config(config)
51+
stub_open_and_run('whacka whacka', config.merge({:database => 'master'}))
52+
53+
@provider.run_update
54+
}
55+
end
56+
end
57+
context 'run_check' do
58+
describe 'against default database' do
59+
it {
60+
create_sqlserver_tsql({:title => 'runme', :command => 'whacka whacka', :onlyif => 'fozy wozy', :instance => 'MSSQLSERVER'})
61+
stub_get_instance_config(config)
62+
stub_open_and_run('fozy wozy', config.merge({:database => 'master'}))
63+
64+
@provider.run_check
65+
}
66+
end
67+
describe 'against non master database' do
68+
it {
69+
create_sqlserver_tsql(
70+
{:title => 'runme',
71+
:command => 'whacka whacka',
72+
:onlyif => 'fozy wozy',
73+
:instance => 'MSSQLSERVER',
74+
:database => 'myDb'})
75+
stub_get_instance_config(config)
76+
stub_open_and_run('fozy wozy', config.merge({:database => 'myDb'}))
77+
78+
@provider.run_check
79+
}
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)