@when('mysql.connected')
def install_mysql_supporting_tools():
''' We have a mysql connection, so we should install the tooling now before
it's ready to consume. This step provides the mysql cli'''
apt.queue_install(['mysql-client'])
@when('mysql.available', 'apt.mysql-client.installed')
def run_mysql_commands(mysql):
''' Run database migrations, not idempotent, and this method will
require additional work to pass review '''
# Assume we need to render some template here for your sql, skip if not
from charms.templating.jinja2 import render
render('sql-template.sql', './migration-00.sql', {'config': 'dict'})
# Assemble the command
cmd = "mysql -h mysql://{0}:{1} -u {2} -p{3} {4} << {5}"
cmd.format(mysql.host(),mysql.port(), mysql.username(), mysql.password())
# Import the required modules to format the string as a command and execute
# returning 0, or raising an exception if the command fails for any reason
import shlex
import subprocess
subprocess.check_call(shlex.split(cmd))