diff options
| author | Stefan Rijnhart <stefan@opener.am> | 2018-06-12 09:44:26 +0200 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2018-08-16 10:33:06 +0200 |
| commit | b3824aa6cbecc97fc0cd54773417045e69adc87a (patch) | |
| tree | 30d0939e6bf380f0b7e149d499c63ea444dcf0b2 | |
| parent | 6327b8f97fef6640c29339d3b0187c40036cb986 (diff) | |
[RFR] Hide implementation of constraint definition storage, courtesy of Raphael Collet
| -rw-r--r-- | odoo/models.py | 6 | ||||
| -rw-r--r-- | odoo/tools/sql.py | 20 |
2 files changed, 11 insertions, 15 deletions
diff --git a/odoo/models.py b/odoo/models.py index 94cbf31a6bc..c4e748ca4d9 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -2445,11 +2445,11 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})): def process(key, definition): conname = '%s_%s' % (self._table, key) - has_definition = tools.constraint_definition(cr, self._table, conname) - if not has_definition: + current_definition = tools.constraint_definition(cr, self._table, conname) + if not current_definition: # constraint does not exists tools.add_constraint(cr, self._table, conname, definition) - elif tools.constraint_comment(cr, self._table, conname) != definition: + elif current_definition != definition: # constraint exists but its definition may have changed tools.drop_constraint(cr, self._table, conname) tools.add_constraint(cr, self._table, conname, definition) 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. """ |
