Skip to content

Commit 63e0768

Browse files
committed
Merge pull request #776 from pondohva/mysql_table_exists
(MODULES-2767) allow to check if table exists before grant
2 parents c40b533 + cafbc80 commit 63e0768

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:mysql_table_exists, :type => :rvalue, :doc => <<-EOS
3+
Check if table exists in database.
4+
5+
For example:
6+
7+
mysql_table_exists('*.*') or mysql_table_exists('example_database.*') always return true
8+
mysql_table_exists('example_db.example_table') check existence table `example_table` in `example_database`
9+
10+
EOS
11+
) do |args|
12+
13+
return raise(Puppet::ParseError, "mysql_table_exists() accept 1 argument - table string like 'database_name.table_name'") unless match = args[0].match(/(.*)\.(.*)/)
14+
15+
db_name, table_name = match.captures
16+
return true if (db_name.eql?('*') or table_name.eql?('*')) ## *.* is valid table string, but we shouldn't check it for existence
17+
query = "SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = '#{table_name}' AND TABLE_SCHEMA = '#{db_name}';"
18+
%x{mysql #{defaults_file} -NBe #{query}}.strip.eql?(table_name)
19+
20+
end
21+
end
22+
23+
def defaults_file
24+
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
25+
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
26+
else
27+
nil
28+
end
29+
end

spec/acceptance/types/mysql_grant_spec.rb

+72
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,76 @@ class { 'mysql::server':
413413
end
414414
end
415415

416+
describe 'adding privileges to specific table' do
417+
# Using puppet_apply as a helper
418+
it 'setup mysql server' do
419+
pp = <<-EOS
420+
class { 'mysql::server': override_options => { 'root_password' => 'password' } }
421+
EOS
422+
423+
apply_manifest(pp, :catch_failures => true)
424+
end
425+
426+
it 'creates grant on missing table will fail' do
427+
pp = <<-EOS
428+
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
429+
user => 'test@localhost',
430+
privileges => ['SELECT'],
431+
table => 'grant_spec_db.grant_spec_table',
432+
}
433+
EOS
434+
expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/Table 'grant_spec_db\.grant_spec_table' doesn't exist/)
435+
end
436+
437+
it 'checks if table exists before grant' do
438+
pp = <<-EOS
439+
if mysql_table_exists('grant_spec_db.grant_spec_table') {
440+
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
441+
user => 'test@localhost',
442+
privileges => 'ALL',
443+
table => 'grant_spec_db.grant_spec_table',
444+
}
445+
}
446+
EOS
447+
apply_manifest(pp, :catch_changes => true)
448+
end
449+
450+
it 'creates table' do
451+
pp = <<-EOS
452+
file { '/tmp/grant_spec_table.sql':
453+
ensure => file,
454+
content => 'CREATE TABLE grant_spec_table (id int);',
455+
before => Mysql::Db['grant_spec_db'],
456+
}
457+
mysql::db { 'grant_spec_db':
458+
user => 'root1',
459+
password => 'password',
460+
sql => '/tmp/grant_spec_table.sql',
461+
}
462+
EOS
463+
464+
apply_manifest(pp, :catch_failures => true)
465+
end
466+
467+
it 'should have the table' do
468+
expect(shell("mysql -e 'show tables;' grant_spec_db|grep grant_spec_table").exit_code).to be_zero
469+
end
470+
471+
it 'checks if table exists before grant' do
472+
pp = <<-EOS
473+
if mysql_table_exists('grant_spec_db.grant_spec_table') {
474+
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
475+
user => 'test@localhost',
476+
privileges => ['SELECT'],
477+
table => 'grant_spec_db.grant_spec_table',
478+
}
479+
}
480+
EOS
481+
apply_manifest(pp, :catch_failures => true)
482+
apply_manifest(pp, :catch_changes => true)
483+
end
484+
485+
486+
end
487+
416488
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'spec_helper'
2+
3+
describe 'the mysql_table_exists function' do
4+
before :all do
5+
Puppet::Parser::Functions.autoloader.loadall
6+
end
7+
8+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
9+
10+
it 'should exist' do
11+
expect(Puppet::Parser::Functions.function('mysql_table_exists')).to eq('function_mysql_table_exists')
12+
end
13+
14+
it 'should raise a ParseError if there is less than 1 arguments' do
15+
expect { scope.function_mysql_table_exists([]) }.to( raise_error(Puppet::ParseError))
16+
end
17+
18+
it 'should raise a ParseError if there is more than 1 arguments' do
19+
expect { scope.function_mysql_table_exists(%w(foo bar)) }.to( raise_error(Puppet::ParseError))
20+
end
21+
22+
end

0 commit comments

Comments
 (0)