fundamentals.mysql.directory_script_runner¶
Given a directory of MySQL scripts, execute the scripts and process the script files according to their success or failure
Run the following code once to set a login-path for your mysql server:
mysql_config_editor set --login-path=<uniqueLoginName> --host=localhost --user=<myUsername> --password
This store’s your credentials in an encrypted file located at ‘~/.mylogin.cnf’.
Use mysql_config_editor print --all to see all of the login-paths set.
Usage:
mysqlSucker
pathToDirectory path to the directory containing the sql scripts to run (scripts must have `.sql` extension)
loginPath the local-path as set with `mysql_config_editor` (`mysqlSucker -h` for more details)
databaseName the name of the database to execute the scripts within
Options: -h, –help show this help message -s successRule, –success successRule what to do if script succeeds. Default None [None|delete|subFolderName] -f failureRule, –failure failureRule what to do if script fails. Default None [None|delete|subFolderName]
- Examples:
To simply execute the scripts in a directory you can run:
mysqlSucker /path/to/scriptdir myLoginPath myDatabaseName
To delete script after thay have executed successfully:
mysqlSucker /path/to/scriptdir myLoginPath myDatabaseName -s delete
To move successful script to a
passedsub-directory of/path/to/scriptdirand failed scripts to afailedsub-directorymysqlSucker /path/to/scriptdir myLoginPath myDatabaseName -s pass -f failed
- Author
David Young
Module Contents¶
Functions¶
A function to run all mysql scripts in a given directory (in a modified date order, oldest first) and then act on the script files in accordance with the succcess or failure of their execution |
|
execute a mysql script given its file path and return the success or failure status of the execution |
|
The main function used when |
API¶
- fundamentals.mysql.directory_script_runner.directory_script_runner(log, pathToScriptDirectory, dbConn=False, waitForResult=True, successRule=None, failureRule=None, loginPath=False, databaseName=False, force=True)[source][source]¶
A function to run all mysql scripts in a given directory (in a modified date order, oldest first) and then act on the script files in accordance with the succcess or failure of their execution
The function can be run with either with an established database connection (
dbConn) or with a mysql generatedlogin-pathname (loginPath).with
dbConnSimply pass the connection
dbConnestablished elsewhere in your code.with
loginPathAs it’s insecure to pass in mysql database credentials via the command-line, run the following command from the terminal
mysql_config_editor set --login-path=<uniqueLoginName> --host=localhost --user=<myUsername> --password > Enter password:
This will store your database credentials in an encrypted file located at ‘~/.mylogin.cnf’. Use
mysql_config_editor print --allto see all of the login-paths set.The
directory_script_runnerfunction can work by taking advantage of mysql’s--login-pathargument so not to require knowledge of the database credentials.Pass the login-path name via the
loginPathparameter to usedirectory_script_runnerin this manner.If both
dbConnandloginPathparameters are given,dbConnwill be given precedent.Key Arguments
log– loggerpathToScriptDirectory– the path to the directory containing the sql script to be rundatabaseName– the name of the databaseforce– force the script to run, skipping over lines with errors, Default TrueloginPath– the local-path as set withmysql_config_editordbConn– the database connectionwaitForResult– wait for the mysql script to finish execution? If ‘False’ the MySQL script will run in background (do not wait for completion), or if ‘delete’ the script will run then delete regardless of success status. Default True. [True|False|delete]successRule– what to do if script succeeds. Default None [None|delete|subFolderName]failureRule– what to do if script fails. Default None [None|delete|subFolderName]
Return
None
Usage
To run the scripts in the directroy and not act on the script file use something similar to:
from fundamentals.mysql import directory_script_runner directory_script_runner( log=log, pathToScriptDirectory="/path/to/mysql_scripts", databaseName="imports", loginPath="myLoginDetails" )
To delete successful scripts and archive failed scripts for later inspection:
from fundamentals.mysql import directory_script_runner directory_script_runner( log=log, pathToScriptDirectory="/path/to/mysql_scripts", databaseName="imports", loginPath="myLoginDetails", successRule="delete", failureRule="failed" )
This creates a folder at
/path/to/mysql_scripts/failedand moves the failed scripts into that folder.Finally to execute the scripts within a directory but not wait for the results to return (much fast but you lose error checking in the MySQL scripts):
from fundamentals.mysql import directory_script_runner directory_script_runner( log=log, pathToScriptDirectory="/path/to/mysql_scripts", databaseName="imports", loginPath="myLoginDetails", waitForResults=False )
Setting
waitForResults= ‘delete’ will trash the script once it has run (or failed … be very careful!)
- fundamentals.mysql.directory_script_runner.execute_mysql_script(pathToScript, dbConn, log)[source][source]¶
execute a mysql script given its file path and return the success or failure status of the execution
Key Arguments:
pathToScript– path to the sql script to be executeddbConn– mysql database connectionlog– logger
Return:
exception– None or the exception thrown during execution of the sql script.
Usage:
To execute a mysql script file located on your local file system:
from fundamentals.mysql import execute_mysql_script exception = execute_mysql_script( pathToScript="/path/to/my/script.sql", log=log, dbConn=dbConn ) if exception: ...