checking if DB populated

151 views
Skip to first unread message

Luca Gioppo

unread,
Dec 3, 2012, 9:57:28 AM12/3/12
to puppet...@googlegroups.com
Which is the bast strategy to check if a DB has been populated?

The target is to execute the "mysql < file.sql" just once since it could not be idempotent.

The idea is to use the unless clause to stop it to run if there is the known data in the table, but it seems that it is just happy with the mysql managing to connect and the fact that the DB return of an empy set is treated as a success return from the mysql command (obviously it managed to execute and the return value of the command does not depend on the result of the query).

Any hint?
Luca

Nikola Petrov

unread,
Dec 3, 2012, 10:12:25 AM12/3/12
to puppet...@googlegroups.com
I am using the following and it serves us well. Basically it makes sure
that the importing is done and executes '&& touch semaphore' file. There
are plans to put those into /var/lib/puppet/sql-snippets instead of in
/opt but oh well it seems I am too lazy to go and move them on each
server now...


<snip>
class sql {
$sqls = '/opt/sql-snippets'

file { $sql::sqls:
ensure => directory,
mode => "700",
}

define do($source='', $db, $content='') {
$script = "$sql::sqls/$name.sql"

file { $script:
mode => "600",
}

case $content {
'': {
File[$script] {
source => $source
}
}
default: {
File[$script] {
content => $content
}
}
}

exec {"mysql-import-$name":
path => ['/bin', '/sbin', '/usr/bin'],
command => "mysql --defaults-file=/root/.my.cnf $db < $script && touch $script.semaphore",
creates => "$script.semaphore",
require => File[$script],
timeout => '0',
}
}

define source($local_script_path, $db) {
$semaphore = "$sql::sqls/$name.semaphore"
exec { "mysql-execute-script-$name":
path => ['/bin', '/sbin', '/usr/bin'],
command => "mysql --defaults-file=/root/.my.cnf $db < $local_script_path && touch $semaphore",
creates => "$semaphore",
timeout => '0',
}
}

Class['mysql'] -> Class['sql']
}

<snip>

Note that it requires a mysql class to be defined before that.

You will get bonus points if you make it work for other databases like
postgre and what not

Some problems:
* the name of the sql::do or sql::sourc should be lowercase as i don't
want to support that(if you want put quotes around semaphore in the
touch


--
Nikola
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/Csk_kOJXTtcJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
>
Reply all
Reply to author
Forward
0 new messages