[FIX] ir_sequence: add support for PG 10

PostgreSQL 10 move sequences' metadata fields into a new `pg_sequence`
system catalog. As a result, selecting from a sequence relation now
only return three fields, and does not include the `increment_by`
metadata field anymore.

This patch obtains the `increment_by` value from the new system catalog
for PostgreSQL server versions >= 10.

Fixes #20269
This commit is contained in:
Olivier Dony
2017-11-11 01:01:48 +01:00
parent ce818f1f8f
commit fa5ee6ed26
+22 -18
View File
@@ -63,6 +63,24 @@ def _update_nogap(self, number_increment):
self.invalidate_cache(['number_next'], [self.id])
return number_next
def _predict_nextval(self, seq_id):
"""Predict next value for PostgreSQL sequence without consuming it"""
# Cannot use currval() as it requires prior call to nextval()
query = """SELECT last_value,
(SELECT increment_by
FROM pg_sequences
WHERE sequencename = 'ir_sequence_%(seq_id)s'),
is_called
FROM ir_sequence_%(seq_id)s"""
if self.env.cr._cnx.server_version < 100000:
query = "SELECT last_value, increment_by, is_called FROM ir_sequence_%(seq_id)s"
self.env.cr.execute(query % {'seq_id': seq_id})
(last_value, increment_by, is_called) = self.env.cr.fetchone()
if is_called:
return last_value + increment_by
# sequence has just been RESTARTed to return last_value next time
return last_value
class ir_sequence(models.Model):
""" Sequence model.
@@ -82,15 +100,8 @@ class ir_sequence(models.Model):
if element.implementation != 'standard':
element.number_next_actual = element.number_next
else:
# get number from postgres sequence. Cannot use currval, because that might give an error when
# not having used nextval before.
query = "SELECT last_value, increment_by, is_called FROM ir_sequence_%03d" % element.id
self.env.cr.execute(query)
(last_value, increment_by, is_called) = self.env.cr.fetchone()
if is_called:
element.number_next_actual = last_value + increment_by
else:
element.number_next_actual = last_value
seq_id = "%03d" % element.id
element.number_next_actual = _predict_nextval(self, seq_id)
def _set_number_next_actual(self):
for record in self:
@@ -319,15 +330,8 @@ class ir_sequence_date_range(models.Model):
if element.sequence_id.implementation != 'standard':
element.number_next_actual = element.number_next
else:
# get number from postgres sequence. Cannot use currval, because that might give an error when
# not having used nextval before.
query = "SELECT last_value, increment_by, is_called FROM ir_sequence_%03d_%03d" % (element.sequence_id.id, element.id)
self.env.cr.execute(query)
(last_value, increment_by, is_called) = self.env.cr.fetchone()
if is_called:
element.number_next_actual = last_value + increment_by
else:
element.number_next_actual = last_value
seq_id = "%03d_%03d" % (element.sequence_id.id, element.id)
element.number_next_actual = _predict_nextval(self, seq_id)
def _set_number_next_actual(self):
for record in self: