'Modify this model\'s database table constraints so they match the one in
_sql_constraints.'

@api.model_cr
def _add_sql_constraints(self):

    cr = self._cr
    def unify_cons_text(txt):
        return txt.lower().replace(',    ', ',').replace('    (', '(')
    def drop(name, definition, old_definition):
        try:
            cr.execute(('ALTER    TABLE    "%s"    DROP    CONSTRAINT    "%s"' % (self._table, name)))
            cr.commit()
            _schema.debug("Table    '%s':    dropped    constraint    '%s'.    Reason:    its    definition    changed    from    '%s'    to    '%s'", self._table, name, old_definition, definition)
        except Exception:
            _schema.warning("Table    '%s':    unable    to    drop    constraint    '%s'!", self._table, definition)
            cr.rollback()
    def add(name, definition):
        query = ('ALTER    TABLE    "%s"    ADD    CONSTRAINT    "%s"    %s' % (self._table, name, definition))
        try:
            cr.execute(query)
            cr.commit()
            _schema.debug("Table    '%s':    added    constraint    '%s'    with    definition=%s", self._table, name, definition)
        except Exception:
            _schema.warning("Table    '%s':    unable    to    add    constraint    '%s'!\nIf    you    want    to    have    it,    you    should    update    the    records    and    execute    manually:\n%s", self._table, definition, query)
            cr.rollback()
    constraint_module = {constraint[0]: cls._module for cls in reversed(type(self).mro()) if (not getattr(cls, 'pool', None)) for constraint in getattr(cls, '_local_sql_constraints', ())}
    for (key, definition, _) in self._sql_constraints:
        conname = ('%s_%s' % (self._table, key))
        cr.execute('SELECT    definition,    1\n                                                                                                        FROM    ir_model_constraint    imc\n                                                                                                        RIGHT    JOIN    pg_constraint    pgc\n                                                                                                        ON    (pgc.conname    =    imc.name)\n                                                                                                        WHERE    pgc.conname=%s\n                                                                                                        ', (conname,))
        existing = cr.dictfetchone()
        if (not existing):
            add(conname, definition)
        elif (unify_cons_text(definition) != existing['definition']):
            drop(conname, definition, (existing['definition'] or ''))
            add(conname, definition)
        module = constraint_module.get(key)
        self._save_constraint(conname, 'u', unify_cons_text(definition), module)
