From 56ec1212c60d405e60c71f621cb7aa6ca4d3aab5 Mon Sep 17 00:00:00 2001 From: "P. Christeas" 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. bzr revid: p_christ@hol.gr-20090912003049-v5xe6eypp5u462vg --- bin/osv/orm.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index f27c1295321..029042adca2 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)))