Developing a mysql resource


#1

Greetings, chefs!

I’ve decided to refactor my cookbooks and to separate the logic of
creating and initial filling mysql databases into the lightweight
resource. I took MysqlDatabase as a basis.

The idea was to create a database if one doesn’t exist,
then to create the database user if it’s name & password are specified
and to fill the database with supplied data, if it’s empty.

Unfortunately, I’ve run into the troubles while writing the provider
action. Conditional statements of the action body are evaluated before
resources defined above are run. For example, I can’t make a test
whether the database is empty because It would be created later than my
test and corresponding ‘if’ statement is evaluated.

I think it’s a frequent problem. I’d like the hear some thoughts and
advices on how to avoid it.

Regards,
Dmitry


#2

On 21 July 2010 13:56, Dmitry V’yal akamaus@gmail.com wrote:

Unfortunately, I’ve run into the troubles while writing the provider action.
Conditional statements of the action body are evaluated before resources
defined above are run. For example, I can’t make a test whether the database
is empty because It would be created later than my test and corresponding
’if’ statement is evaluated.

I think it’s a frequent problem. I’d like the hear some thoughts and advices
on how to avoid it.

Are you using if statements within the recipe, or only_if attributes
on the resources themselves?

Any statements which aren’t within a resource block will be evaluated
during the compilation phase (shown at
http://wiki.opscode.com/display/chef/Anatomy+of+a+Chef+Run).

If you want to run the code while the resources are being run then you
need to wrap that code in a Ruby resource.