From 0233bfe8d6cd5c57aa54038aca541bb3a1c4d5cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 8 Apr 2023 11:24:45 +0200 Subject: [PATCH] Format using SQL Identifier type sequence name The sequence functions are using string parameters so for case sensitive sequence name we must pass a string containing double-quoting if necessary. Closes #12194 --- trytond/trytond/backend/postgresql/database.py | 11 +++++++---- trytond/trytond/backend/postgresql/table.py | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/trytond/trytond/backend/postgresql/database.py b/trytond/trytond/backend/postgresql/database.py index e3ddced1b7..3e4680199b 100644 --- a/trytond/trytond/backend/postgresql/database.py +++ b/trytond/trytond/backend/postgresql/database.py @@ -417,18 +417,21 @@ class Database(DatabaseInterface): def nextid(self, connection, table): cursor = connection.cursor() cursor.execute( - "SELECT NEXTVAL(pg_get_serial_sequence(%s, %s))", (table, 'id')) + "SELECT nextval(pg_get_serial_sequence(format(%s, %s), %s))", + ('%I', table, 'id')) return cursor.fetchone()[0] def setnextid(self, connection, table, value): cursor = connection.cursor() cursor.execute( - "SELECT SETVAL(pg_get_serial_sequence(%s, %s), %s)", - (table, 'id', value)) + "SELECT setval(pg_get_serial_sequence(format(%s, %s), %s), %s)", + ('%I', table, 'id', value)) def currid(self, connection, table): cursor = connection.cursor() - cursor.execute("SELECT pg_get_serial_sequence(%s, %s)", (table, 'id')) + cursor.execute( + "SELECT pg_get_serial_sequence(format(%s, %s), %s)", + ('%I', table, 'id')) sequence_name, = cursor.fetchone() cursor.execute(f"SELECT last_value FROM {sequence_name}") return cursor.fetchone()[0] diff --git a/trytond/trytond/backend/postgresql/table.py b/trytond/trytond/backend/postgresql/table.py index fd6ee28421..a4aefb581c 100644 --- a/trytond/trytond/backend/postgresql/table.py +++ b/trytond/trytond/backend/postgresql/table.py @@ -49,7 +49,7 @@ class TableHandler(TableHandlerInterface): def migrate_to_identity(table, column): previous_seq_name = f"{table}_{column}_seq" cursor.execute( - "SELECT NEXTVAL(%s)", (previous_seq_name,)) + "SELECT nextval(format(%s, %s))", ('%I', previous_seq_name,)) next_val, = cursor.fetchone() cursor.execute( "SELECT seqincrement, seqmax, seqmin, seqcache " @@ -75,7 +75,8 @@ class TableHandler(TableHandlerInterface): "ADD GENERATED BY DEFAULT AS IDENTITY").format( Identifier(table), Identifier(column))) cursor.execute( - "SELECT pg_get_serial_sequence(%s, %s)", (table, column)) + "SELECT pg_get_serial_sequence(format(%s, %s), %s)", + ('%I', table, column)) serial_seq_name, = cursor.fetchone() cursor.execute( (f"ALTER SEQUENCE {serial_seq_name} INCREMENT BY %s "