Skip to content

Commit

Permalink
(MODULES-1338) Allow mysql::db to import several files
Browse files Browse the repository at this point in the history
A user might need to import several files on database creation.
Currently the module only allows the import of a single file.
This commit allows one to, from now on, import severals.

Before :

  mysql::db { 'test' :
    sql => '/tmp/my_import1.sql',
  }

Now :

  mysql::db { 'test' :
    sql => [
      '/tmp/my_import1.sql',
      '/tmp/my_import2.sql',
    ]
  }
  • Loading branch information
Spredzy committed Nov 13, 2014
1 parent 89762a7 commit 5e6a1c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 11 additions & 9 deletions manifests/db.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
define mysql::db (
$user,
$password,
$dbname = $name,
$charset = 'utf8',
$collate = 'utf8_general_ci',
$host = 'localhost',
$grant = 'ALL',
$sql = undef,
$enforce_sql = false,
$ensure = 'present',
$dbname = $name,
$charset = 'utf8',
$collate = 'utf8_general_ci',
$host = 'localhost',
$grant = 'ALL',
$sql = undef,
$enforce_sql = false,
$ensure = 'present',
$import_timeout = 300,
) {
#input validation
validate_re($ensure, '^(present|absent)$',
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
$table = "${dbname}.*"
$sql_inputs = join(any2array($sql), ' ')

include '::mysql::client'

Expand Down Expand Up @@ -49,10 +50,11 @@

if $sql {
exec{ "${dbname}-import":
command => "/usr/bin/mysql ${dbname} < ${sql}",
command => "cat ${sql_inputs} | mysql ${dbname}",
logoutput => true,
environment => "HOME=${::root_home}",
refreshonly => $refresh,
path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin',
require => Mysql_grant["${user}@${host}/${table}"],
subscribe => Mysql_database[$dbname],
timeout => $import_timeout,
Expand Down
5 changes: 5 additions & 0 deletions spec/defines/mysql_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
is_expected.to contain_exec('test_db-import').with_refreshonly(false)
end

it 'should import sql scripts when more than one is specified' do
params.merge!({'sql' => ['test_sql', 'test_2_sql']})
is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db')
end

it 'should not create database and database user' do
params.merge!({'ensure' => 'absent', 'host' => 'localhost'})
is_expected.to contain_mysql_database('test_db').with_ensure('absent')
Expand Down

0 comments on commit 5e6a1c4

Please sign in to comment.