Skip to content

Instantly share code, notes, and snippets.

@florentx
Created February 19, 2014 17:15
Show Gist options
  • Save florentx/9096677 to your computer and use it in GitHub Desktop.
Save florentx/9096677 to your computer and use it in GitHub Desktop.
import re
# Helper to check if the name is a valid identifier
_isidentifier = re.compile(r'[a-zA-Z_]\w*').match
@contextlib.contextmanager
def savepoint(cr, name, quiet=False):
assert _isidentifier(name)
cr.execute('SAVEPOINT "%s";' % name)
try:
yield
except Exception:
cr.execute('ROLLBACK TO "%s";' % name)
if not quiet:
raise
finally:
cr.execute('RELEASE "%s";' % name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment