← Back to team overview

launchpad-dev team mailing list archive

New tricks for StormStatementRecorder

 

lp.testing.StormStatementRecorder now has a few more features, making it interesting for interactive use.  The below is taken from the wiki (https://dev.launchpad.net/Debugging).

Sometimes you want to look at the SQL of just a certain slice of code, such as within make harness. The StormStatementRecorder can be a useful tool for this.

Basic usage will get you the SQL run while the recorder is used:


from lp.testing import StormStatementRecorder

with StormStatementRecorder() as recorder:
    ...code that touches the DB goes here...

print recorder

Printing the recorder gives you a full output of what happened. You can also look at .statements, .count, and so on (use dir!).

You can get all tracebacks by passing True when you instantiate the recorder (StormStatementRecorder(True)). Again, print the recorder to see the results.

You can conditionally get tracebacks by passing a callable that receives a SQL query string and returns a boolean True if a traceback should be collected, and False if it should not. The SQL will be normalized to capitalization and space normalized. For example, StormStatementRecorder(lambda sql: 'STRUCTURALSUBSCRIPTION' in sql) would get you tracebacks when the SQL has something to do with structural subscriptons.

Gary

Follow ups