summaryrefslogtreecommitdiff
path: root/addons/document/document.py
diff options
context:
space:
mode:
authorP. Christeas <p_christ@hol.gr>2010-12-23 16:25:29 +0200
committerP. Christeas <p_christ@hol.gr>2010-12-23 16:25:29 +0200
commit43387c7f430a5109a5f2fcef9dd9c94594c788ff (patch)
treeae29eccad6c979671240cf9fee97cf9ff2d440c5 /addons/document/document.py
parentf551479a9bd379204197947dbcbaac04972eb7ba (diff)
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
Diffstat (limited to 'addons/document/document.py')
-rw-r--r--addons/document/document.py31
1 files changed, 25 insertions, 6 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):