From 43387c7f430a5109a5f2fcef9dd9c94594c788ff Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Thu, 23 Dec 2010 16:25:29 +0200 Subject: [PATCH] document: improve the attach_parent_id() function. No need to call an SQL command in iteration! No need to transfer the db_datas back and forth from Postgres! Also, prepended an underscore, so that the function is never exported through RPC. bzr revid: p_christ@hol.gr-20101223142529-hgudbfj54p4d2e34 --- addons/document/document.py | 31 +++++++++++++++++++++++++------ addons/document/document_data.xml | 9 +++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/addons/document/document.py b/addons/document/document.py index 874fc8b8888..dd34dd4295c 100644 --- a/addons/document/document.py +++ b/addons/document/document.py @@ -36,14 +36,33 @@ class document_file(osv.osv): _inherit = 'ir.attachment' _rec_name = 'datas_fname' - def attach_parent_id(self, cr, uid, ids=[], context=None): - """Attach Parent id For document""" + def _attach_parent_id(self, cr, uid, ids=None, context=None): + """Migrate ir.attachments to the document module. + + When the 'document' module is loaded on a db that has had plain attachments, + they will need to be attached to some parent folder, and be converted from + base64-in-bytea to raw-in-bytea format. + This function performs the internal migration, once and forever, for these + attachments. It cannot be done through the nominal ORM maintenance code, + because the root folder is only created after the document_data.xml file + is loaded. + It also establishes the parent_id NOT NULL constraint that ir.attachment + should have had (but would have failed if plain attachments contained null + values). + """ parent_id = self.pool.get('document.directory')._get_root_directory(cr,uid) - ids = self.search(cr, uid, [('parent_id', '=', False)]) - attach_doc = self.browse(cr, uid, ids, context=context) - for attach in attach_doc: - cr.execute("UPDATE ir_attachment SET parent_id = %s,db_datas = decode(encode(%s,'escape'), 'base64') WHERE id = %s", (parent_id, attach.db_datas, attach.id)) + if not parent_id: + logging.getLogger('document').warning("at _attach_parent_id(), still not able to set the parent!") + return False + + if ids is not None: + raise NotImplementedError("Ids is just there by convention! Don't use it yet, please.") + + cr.execute("UPDATE ir_attachment " \ + "SET parent_id = %s, db_datas = decode(encode(db_datas,'escape'), 'base64') " \ + "WHERE parent_id IS NULL", (parent_id,)) + cr.execute("ALTER TABLE ir_attachment ALTER parent_id SET NOT NULL") return True def _get_filestore(self, cr): diff --git a/addons/document/document_data.xml b/addons/document/document_data.xml index 911e25b195c..6d7fe7531eb 100644 --- a/addons/document/document_data.xml +++ b/addons/document/document_data.xml @@ -98,9 +98,10 @@ 0 - - - - + + + +