summaryrefslogtreecommitdiff
path: root/odoo/tools/sql.py
diff options
context:
space:
mode:
authorStefan Rijnhart <stefan@opener.am>2018-05-08 10:42:50 +0200
committerRaphael Collet <rco@odoo.com>2018-08-16 10:33:06 +0200
commit6327b8f97fef6640c29339d3b0187c40036cb986 (patch)
tree0f07ce642e8ba5ae050751bb8817a1b3572859ac /odoo/tools/sql.py
parent3e043ca49c99400ab51148db7319bf4a670531eb (diff)
[IMP] Store the original constraint definition as a comment on the constraint
and use it to determine if the constraint definition was changed. This prevents endless readding of constraints that are reformatted by postgresql in an unrecognizable way (in which e.g. "CHECK (credit*debit=0)" becomes "CHECK ((credit * debit) = 0::numeric)")
Diffstat (limited to 'odoo/tools/sql.py')
-rw-r--r--odoo/tools/sql.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py
index 7e8f69a4168..b1c7ecbbf3a 100644
--- a/odoo/tools/sql.py
+++ b/odoo/tools/sql.py
@@ -118,17 +118,29 @@ def drop_not_null(cr, tablename, columnname):
cr.execute('ALTER TABLE "{}" ALTER COLUMN "{}" DROP NOT NULL'.format(tablename, columnname))
_schema.debug("Table %r: column %r: dropped constraint NOT NULL", tablename, columnname)
-def constraint_definition(cr, constraintname):
+def constraint_definition(cr, tablename, constraintname):
""" Return the given constraint's definition. """
- cr.execute("SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname=%s", (constraintname,))
+ 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,))
return cr.fetchone()[0] if cr.rowcount else None
def add_constraint(cr, tablename, constraintname, definition):
""" Add a constraint on the given table. """
- query = 'ALTER TABLE "{}" ADD CONSTRAINT "{}" {}'.format(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():
- cr.execute(query)
+ cr.execute(query1)
+ cr.execute(query2, (definition,))
_schema.debug("Table %r: added constraint %r as %s", tablename, constraintname, definition)
except Exception:
msg = "Table %r: unable to add constraint %r!\n" \