diff --git a/mysqlred.py b/mysqlred.py index c7e27ab..5c2d48b 100644 --- a/mysqlred.py +++ b/mysqlred.py @@ -31,6 +31,8 @@ try: except ImportError: import MySQLdb as mysqldb +import atexit + from pywikibot import config import jogobot @@ -72,7 +74,10 @@ class MysqlRed: user=type( self ).db_username, passwd=type( self ).db_password, db=type( self ).db_name ) - + + # Register callback for warnig if exit with cached db write querys + atexit.register( type(self).warn_if_not_flushed ) + def __del__( self ): """ Before deleting class, close connection to MySQL-DB @@ -108,6 +113,16 @@ class MysqlRed: if cls._cached_insert_data or cls._cached_update_data: cls.connection.commit() + @classmethod + def warn_if_not_flushed(cls): + """ + Outputs a warning if there are db write querys cached and not flushed + before exiting programm! + """ + if cls._cached_update_data or cls._cached_insert_data: + jogobot.output( "Cached Database write querys not flushed!!! " + + "Data loss is possible!", "WARNING" ) + class MysqlRedPage( MysqlRed ): """