jeudi 23 juillet 2015

continuous logging to database - fix errors

So I have a python module that receives data and uploads it to a database. I have a table health check that checks if the table is present, if number of columns is what it should be, also the types and names of columns.

So I have a bit of code like that:

def log_msg(message):
        try:
            insert(message)
        except sqlite3.OperationalError:
            table_health()
            log_msg(message)

table_health() drops the table if there's anything wrong with it and creates a new one.

I want the module to be able to fix the database, since data will be reported continuously. That's why I don't want to raise an exception, I've decided to handle it like that.

I also don't want to check health before executing the query, because this health check is performed at module import, so table should be ok prior to execution. The only problem arises if the table is dropped/changed during runtime. But most of the time table should be fine, so checking before every query seems like a waste - hence check on error.

Only solution to a possible infinite recursion I can see is to make sure log_msg is executed after an exception only once. By replacing it with insert(message) Since if the table is going to be fixed, it's after one execution. Otherwise there's something wrong

Any suggestions on how to handle this?

Aucun commentaire:

Enregistrer un commentaire