From f4fe313e6554ddfb9a1b944a69bc329228be67b5 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Fri, 25 Mar 2016 11:29:14 -0700 Subject: [PATCH] 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. --- .../parser/functions/mysql_table_exists.rb | 30 ------------------- spec/acceptance/types/mysql_grant_spec.rb | 30 ------------------- .../functions/mysql_table_exists_spec.rb | 26 ---------------- 3 files changed, 86 deletions(-) delete mode 100644 lib/puppet/parser/functions/mysql_table_exists.rb delete mode 100644 spec/unit/puppet/functions/mysql_table_exists_spec.rb diff --git a/lib/puppet/parser/functions/mysql_table_exists.rb b/lib/puppet/parser/functions/mysql_table_exists.rb deleted file mode 100644 index ff089d8..0000000 --- a/lib/puppet/parser/functions/mysql_table_exists.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/spec/acceptance/types/mysql_grant_spec.rb b/spec/acceptance/types/mysql_grant_spec.rb index d1bfa25..b6cc74e 100644 --- a/spec/acceptance/types/mysql_grant_spec.rb +++ b/spec/acceptance/types/mysql_grant_spec.rb @@ -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 diff --git a/spec/unit/puppet/functions/mysql_table_exists_spec.rb b/spec/unit/puppet/functions/mysql_table_exists_spec.rb deleted file mode 100644 index 03fb1cb..0000000 --- a/spec/unit/puppet/functions/mysql_table_exists_spec.rb +++ /dev/null @@ -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 \ No newline at end of file