From e37c71bae5e84bd26c77e9df16bbda6cfa8f8647 Mon Sep 17 00:00:00 2001 From: Joel Grand-Guillaume Date: Thu, 24 Jul 2008 12:56:55 +0200 Subject: [PATCH] Fix truncated string in DB when update server and field are varchar bzr revid: joel.grandguillaume@camptocamp.com-20080724105655-g2zu5oelpk7trm65 --- bin/osv/orm.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index eeb089acfd7..39690f449b5 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1386,14 +1386,16 @@ class orm(orm_template): # columns with the name 'type' cannot be changed for an unknown reason?! if k != 'type': if f_pg_size > f.size: - logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed size (DB = %d, def = %d), strings will be truncated !" % (k, self._table, f_pg_size, f.size)) -#TODO: check si y a des donnees qui vont poser probleme (select char_length(...)) -#TODO: issue a log message even if f_pg_size < f.size - cr.execute("ALTER TABLE \"%s\" RENAME COLUMN \"%s\" TO temp_change_size" % (self._table,k)) - cr.execute("ALTER TABLE \"%s\" ADD COLUMN \"%s\" VARCHAR(%d)" % (self._table,k,f.size)) - cr.execute("UPDATE \"%s\" SET \"%s\"=temp_change_size::VARCHAR(%d)" % (self._table,k,f.size)) - cr.execute("ALTER TABLE \"%s\" DROP COLUMN temp_change_size" % (self._table,)) - cr.commit() + logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed size (DB = %d, def = %d), DB size will be kept !" % (k, self._table, f_pg_size, f.size)) + # If actual DB size is < than new + # We update varchar size, otherwise, we keep DB size + # to avoid truncated string... + if f_pg_size < f.size: + cr.execute("ALTER TABLE \"%s\" RENAME COLUMN \"%s\" TO temp_change_size" % (self._table,k)) + cr.execute("ALTER TABLE \"%s\" ADD COLUMN \"%s\" VARCHAR(%d)" % (self._table,k,f.size)) + cr.execute("UPDATE \"%s\" SET \"%s\"=temp_change_size::VARCHAR(%d)" % (self._table,k,f.size)) + cr.execute("ALTER TABLE \"%s\" DROP COLUMN temp_change_size" % (self._table,)) + cr.commit() # if the field is required and hasn't got a NOT NULL constraint if f.required and f_pg_notnull == 0: # set the field to the default value if any