From b3824aa6cbecc97fc0cd54773417045e69adc87a Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Tue, 12 Jun 2018 09:44:26 +0200 Subject: [RFR] Hide implementation of constraint definition storage, courtesy of Raphael Collet --- odoo/tools/sql.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'odoo/tools/sql.py') diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py index b1c7ecbbf3a..2a0594b7d34 100644 --- a/odoo/tools/sql.py +++ b/odoo/tools/sql.py @@ -120,17 +120,13 @@ def drop_not_null(cr, tablename, columnname): def constraint_definition(cr, tablename, constraintname): """ Return the given constraint's definition. """ - cr.execute('SELECT pg_get_constraintdef(c.oid) FROM pg_constraint c ' - 'JOIN pg_class t ON t.oid = c.conrelid ' - 'WHERE t.relname = %s AND conname=%s', (tablename, constraintname,)) - return cr.fetchone()[0] if cr.rowcount else None - -def constraint_comment(cr, tablename, constraintname): - """ Return the given constraint's comment. """ - cr.execute('SELECT d.description FROM pg_description d ' - 'JOIN pg_constraint c ON c.oid = d.objoid ' - 'JOIN pg_class t ON t.oid = c.conrelid ' - 'WHERE t.relname = %s AND conname = %s;', (tablename, constraintname,)) + query = """ + SELECT COALESCE(d.description, pg_get_constraintdef(c.oid)) + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + LEFT JOIN pg_description d ON c.oid = d.objoid + WHERE t.relname = %s AND conname = %s;""" + cr.execute(query, (tablename, constraintname)) return cr.fetchone()[0] if cr.rowcount else None def add_constraint(cr, tablename, constraintname, definition): @@ -145,7 +141,7 @@ def add_constraint(cr, tablename, constraintname, definition): except Exception: msg = "Table %r: unable to add constraint %r!\n" \ "If you want to have it, you should update the records and execute manually:\n%s" - _schema.warning(msg, tablename, constraintname, query, exc_info=True) + _schema.warning(msg, tablename, constraintname, query1, exc_info=True) def drop_constraint(cr, tablename, constraintname): """ drop the given constraint. """ -- cgit v1.2.3