Skip to content

Instantly share code, notes, and snippets.

@agronholm
Created October 10, 2013 19:53
Show Gist options
  • Save agronholm/6924480 to your computer and use it in GitHub Desktop.
Save agronholm/6924480 to your computer and use it in GitHub Desktop.
Temporary table hack for SQLAlchemy unit tests
def create_temporary_tables(conn, cursor, statement, parameters, context, executemany):
if statement.startswith('CREATE TABLE '):
statement = 'CREATE TEMPORARY TABLE ' + statement[13:]
return statement, parameters
def setup_module():
global connection, engine
# Connect to the database and create the schema within a transaction
engine = create_engine('mysql:///user:pass@yourdb')
connection = engine.connect()
listen(connection, "before_cursor_execute", create_temporary_tables, retval=True)
Base.metadata.create_all(connection)
def teardown_module():
# Disconnect from the database
connection.close()
engine.dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment