(#11508) Only load sql_scripts on DB creation

Previous to this commit, if the sql parameter was provided to a
declaration of the mysql::db defined type, the defined type would always
load the sql script on every catalog run. This changes the exec that
loads that sql script to be refreshonly unless the enforce_sql parameter
is set to true.
This commit is contained in:
Carl Caum 2011-12-19 07:25:35 -08:00
parent 79579125c5
commit e3b9fd90a6
6 changed files with 109 additions and 13 deletions

47
Rakefile Normal file
View file

@ -0,0 +1,47 @@
require 'rake'
require 'fileutils'
begin
require 'rspec/core/rake_task'
HAVE_RSPEC = true
rescue LoadError
HAVE_RSPEC = false
end
task :default => [:build]
def update_module_version
gitdesc = %x{git describe}.chomp
semver = gitdesc.gsub(/v?(\d+\.\d+\.\d+)-?(.*)/) do
newver = "#{$1}"
newver << "git-#{$2}" unless $2.empty?
newver
end
modulefile = File.read("Modulefile")
modulefile.gsub!(/^\s*version\s+'.*?'/, "version '#{semver}'")
File.open("Modulefile", 'w') do |f|
f.write(modulefile)
end
semver
end
desc "Build Puppet Module Package"
task :build do
system("gimli README*.markdown")
FileUtils.cp "Modulefile", "Modulefile.bak"
update_module_version
system("puppet-module build")
FileUtils.mv "Modulefile.bak", "Modulefile"
end
desc "Clean the package directory"
task :clean do
FileUtils.rm_rf("pkg/")
end
if HAVE_RSPEC then
desc 'Run all module spec tests (Requires rspec-puppet gem)'
task :spec do
system 'rspec --format d spec/'
end
end

View file

@ -31,32 +31,46 @@ define mysql::db (
$charset = 'utf8',
$host = 'localhost',
$grant='all',
$enforce_sql = false,
$sql=''
) {
if $grant == 'all' {
$safe_grant = [ 'alter_priv','alter_routine_priv','create_priv','create_routine_priv','create_tmp_table_priv','create_view_priv','delete_priv','drop_priv','event_priv','execute_priv','grant_priv','index_priv','insert_priv','lock_tables_priv','references_priv','select_priv','show_view_priv','trigger_priv','update_priv']
} else {
$safe_grant = $grant
}
database { $name:
ensure => present,
charset => $charset,
ensure => present,
charset => $charset,
provider => 'mysql',
require => Class['mysql::server']
require => Class['mysql::server'],
notify => Exec["${name}-import-import"],
}
database_user{"${user}@${host}":
ensure => present,
ensure => present,
password_hash => mysql_password($password),
provider => 'mysql',
require => Database[$name],
provider => 'mysql',
require => Database[$name],
}
database_grant{"${user}@${host}/${name}":
# privileges => [ 'alter_priv', 'insert_priv', 'select_priv', 'update_priv' ],
privileges => $grant,
provider => 'mysql',
require => Database_user["${user}@${host}"],
privileges => $safe_grant,
provider => 'mysql',
require => Database_user["${user}@${host}"],
}
if($sql) {
exec{"${name}-import-import":
command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}",
logoutput => true,
require => Database_grant["${user}@${host}/${name}"],
command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}",
logoutput => true,
refreshonly => $enforce_sql ? {
true => false,
false => true,
},
}
}
}
}

View file

@ -0,0 +1,6 @@
require 'spec_helper'
describe 'mysql' do
it { should contain_class 'mysql' }
end

View file

@ -0,0 +1,16 @@
require 'spec_helper'
describe 'mysql::db', :type => :define do
let(:title) { 'test_db' }
let(:params) {
{'user' => 'testuser',
'password' => 'testpass',
'enforce_sql' => false,
'sql' => 'test_sql',
}
}
it 'should set load of sql script to refreshonly' do
should create_resource('exec', 'test_db-import-import').with_param('refreshonly', true)
end
end

6
spec/spec.opts Normal file
View file

@ -0,0 +1,6 @@
--format
s
--colour
--loadby
mtime
--backtrace

7
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,7 @@
require 'puppet'
require 'rubygems'
require 'rspec-puppet'
RSpec.configure do |c|
c.module_path = File.join(File.dirname(__FILE__), '../../')
end