← Back to team overview

openerp-expert-framework team mailing list archive

Re: [Blueprint refactor-fields] Allow the fields to be renamed for refactoring

 

On Thursday 11 February 2010, you wrote:
> Blueprint changed by Numérigraphe:
> 
> Whiteboard changed:
> + Please discuss this blueprint on the Framework expert mailing list, and
> + report conclusions here.
> +
>   === Refactoring use case ===
>   We could add a new parameter "replace" in osv.fields, so that an columns
...

Actually, already done that!
I don't know if it's made it to the bzr branches.



From 650b3a310eddd4ef1f19c14d0a35c141e18aba8c Mon Sep 17 00:00:00 2001
From: P. Christeas <p_christ@xxxxxx>
Date: Sat, 12 Sep 2009 03:30:49 +0300
Subject: [PATCH] ORM feature: allow renaming of columns.

Sometimes we may want to rename object attributes, aka. SQL columns.
Provide a mechanism for that.
---
 bin/osv/orm.py |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/bin/osv/orm.py b/bin/osv/orm.py
index f27c129..029042a 100644
--- a/bin/osv/orm.py
+++ b/bin/osv/orm.py
@@ -1740,6 +1740,20 @@ class orm(orm_template):
                                "AND c.oid=a.attrelid " \
                                "AND a.atttypid=t.oid", (self._table, k))
                     res = cr.dictfetchall()
+		    if not res and hasattr(f,'oldname'):
+				cr.execute("SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size " \
+					"FROM pg_class c,pg_attribute a,pg_type t " \
+					"WHERE c.relname=%s " \
+					"AND a.attname=%s " \
+					"AND c.oid=a.attrelid " \
+					"AND a.atttypid=t.oid", (self._table, f.oldname))
+				res_old = cr.dictfetchall()
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'trying to rename %s(%s) to %s'% (self._table, f.oldname, k))
+				if res_old and len(res_old)==1:
+					cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k))
+					res = res_old
+					res[0]['attname'] = k
+				
                     if not res:
                         if not isinstance(f, fields.function) or f.store:
 
@@ -1887,7 +1901,7 @@ class orm(orm_template):
                                             cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
                                             cr.commit()
                     else:
-                        logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error !")
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !"%(self._table,k))
             for order,f,k in todo_update_store:
                 todo_end.append((order, self._update_store, (f, k)))
 
-- 
1.6.4.4