[FIX] sql_db: add flush() in savepoint()

closes odoo/odoo#36060

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2019-08-26 13:39:16 +00:00
parent eafdf1812d
commit c7f5c4afd2
5 changed files with 15 additions and 8 deletions
+1 -1
View File
@@ -951,7 +951,7 @@ class Field(MetaField('DummyField', (object,), {})):
indexname = '%s_%s_index' % (model._table, self.name)
if self.index:
try:
with model._cr.savepoint():
with model._cr.savepoint(flush=False):
sql.create_index(model._cr, indexname, model._table, ['"%s"' % self.name])
except psycopg2.OperationalError:
_schema.error("Unable to add index for %s", self)
+1 -1
View File
@@ -80,7 +80,7 @@ def load_demo(cr, package, idref, mode, report=None):
try:
_logger.info("Module %s: loading demo", package.name)
with cr.savepoint():
with cr.savepoint(flush=False):
load_data(cr, idref, mode, kind='demo', package=package, report=report)
return True
except Exception as e:
+1 -1
View File
@@ -304,7 +304,7 @@ def restore_db(db, dump_file, copy=False):
if odoo.tools.config['unaccent']:
try:
with cr.savepoint():
with cr.savepoint(flush=False):
cr.execute("CREATE EXTENSION unaccent")
except psycopg2.Error:
pass
+8 -1
View File
@@ -425,13 +425,20 @@ class Cursor(object):
@contextmanager
@check
def savepoint(self):
def savepoint(self, flush=True):
"""context manager entering in a new savepoint"""
name = uuid.uuid1().hex
env = get_env(currentframe(), 2) if flush else None
if env is not None:
env['base'].flush()
self.execute('SAVEPOINT "%s"' % name)
try:
yield
if env is not None:
env['base'].flush()
except Exception:
if env is not None:
env.clear()
self.execute('ROLLBACK TO SAVEPOINT "%s"' % name)
raise
else:
+4 -4
View File
@@ -88,7 +88,7 @@ def rename_column(cr, tablename, columnname1, columnname2):
def convert_column(cr, tablename, columnname, columntype):
""" Convert the column to the given type. """
try:
with cr.savepoint():
with cr.savepoint(flush=False):
cr.execute('ALTER TABLE "{}" ALTER COLUMN "{}" TYPE {}'.format(tablename, columnname, columntype),
log_exceptions=False)
except psycopg2.NotSupportedError:
@@ -106,7 +106,7 @@ def set_not_null(cr, tablename, columnname):
""" Add a NOT NULL constraint on the given column. """
query = 'ALTER TABLE "{}" ALTER COLUMN "{}" SET NOT NULL'.format(tablename, columnname)
try:
with cr.savepoint():
with cr.savepoint(flush=False):
cr.execute(query)
_schema.debug("Table %r: column %r: added constraint NOT NULL", tablename, columnname)
except Exception:
@@ -135,7 +135,7 @@ def add_constraint(cr, tablename, constraintname, definition):
query1 = 'ALTER TABLE "{}" ADD CONSTRAINT "{}" {}'.format(tablename, constraintname, definition)
query2 = 'COMMENT ON CONSTRAINT "{}" ON "{}" IS %s'.format(constraintname, tablename)
try:
with cr.savepoint():
with cr.savepoint(flush=False):
cr.execute(query1)
cr.execute(query2, (definition,))
_schema.debug("Table %r: added constraint %r as %s", tablename, constraintname, definition)
@@ -147,7 +147,7 @@ def add_constraint(cr, tablename, constraintname, definition):
def drop_constraint(cr, tablename, constraintname):
""" drop the given constraint. """
try:
with cr.savepoint():
with cr.savepoint(flush=False):
cr.execute('ALTER TABLE "{}" DROP CONSTRAINT "{}"'.format(tablename, constraintname))
_schema.debug("Table %r: dropped constraint %r", tablename, constraintname)
except Exception: