Remove mysql_table_exists() function

This function is intended to check for the existence of a table before
declaring some resource, but this is neither portable (because functions
orun on the master, not the agent) nor one-run idempotent (because the
function would run before mysql is even installed, and would take two
runs to do anything).

The correct way of doing this would be to update the providers and
dependency ordering to handle the conditional states.

Luckily this was never released so it is backwards compatible.
This commit is contained in:
Hunter Haugen 2016-03-25 11:29:14 -07:00
parent 2ff7b3dbe2
commit f4fe313e65
3 changed files with 0 additions and 86 deletions

View file

@ -1,30 +0,0 @@
module Puppet::Parser::Functions
newfunction(:mysql_table_exists, :type => :rvalue, :doc => <<-EOS
Check if table exists in database.
For example:
mysql_table_exists('*.*') or mysql_table_exists('example_database.*') always return true
mysql_table_exists('example_db.example_table') check existence table `example_table` in `example_database`
EOS
) do |args|
return raise(Puppet::ParseError,
"mysql_table_exists() accept 1 argument - table string like 'database_name.table_name'") unless (args.size == 1 and match = args[0].match(/(.*)\.(.*)/))
db_name, table_name = match.captures
return true if (db_name.eql?('*') or table_name.eql?('*')) ## *.* is valid table string, but we shouldn't check it for existence
query = "SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = '#{table_name}' AND TABLE_SCHEMA = '#{db_name}';"
%x{mysql #{defaults_file} -NBe #{query}}.strip.eql?(table_name)
end
end
def defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end

View file

@ -434,19 +434,6 @@ describe 'mysql_grant' do
expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/Table 'grant_spec_db\.grant_spec_table' doesn't exist/)
end
it 'checks if table exists before grant' do
pp = <<-EOS
if mysql_table_exists('grant_spec_db.grant_spec_table') {
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
user => 'test@localhost',
privileges => 'ALL',
table => 'grant_spec_db.grant_spec_table',
}
}
EOS
apply_manifest(pp, :catch_changes => true)
end
it 'creates table' do
pp = <<-EOS
file { '/tmp/grant_spec_table.sql':
@ -467,22 +454,5 @@ describe 'mysql_grant' do
it 'should have the table' do
expect(shell("mysql -e 'show tables;' grant_spec_db|grep grant_spec_table").exit_code).to be_zero
end
it 'checks if table exists before grant' do
pp = <<-EOS
if mysql_table_exists('grant_spec_db.grant_spec_table') {
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
user => 'test@localhost',
privileges => ['SELECT'],
table => 'grant_spec_db.grant_spec_table',
}
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
end
end

View file

@ -1,26 +0,0 @@
require 'spec_helper'
describe 'the mysql_table_exists function' do
before :all do
Puppet::Parser::Functions.autoloader.loadall
end
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
it 'should exist' do
expect(Puppet::Parser::Functions.function('mysql_table_exists')).to eq('function_mysql_table_exists')
end
it 'should raise a ParseError if there is less than 1 arguments' do
expect { scope.function_mysql_table_exists([]) }.to( raise_error(Puppet::ParseError))
end
it 'should raise a ParserError if argument doesn\'t look like database_name.table_name' do
expect { scope.function_mysql_table_exists(['foo_bar']) }.to( raise_error(Puppet::ParseError))
end
it 'should raise a ParseError if there is more than 1 arguments' do
expect { scope.function_mysql_table_exists(%w(foo.bar foo.bar)) }.to( raise_error(Puppet::ParseError))
end
end