Odoo Help

4

7
2 Answers
7
Avatar

Andreas Brueckl Austria

--Andreas Brueckl--

5536
| 8 8 9
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl Austria
2/20/13, 11:43 AM

I have migrated from databasestore to a filesystem storage. Therefore I have used the following python-Script. Maybe this can help you. I have applied this script on OpenERP version 6.0, but the functionality should be the same.

#!/usr/bin/python

# Check total size afterwards: "du -b <filestore>" minux 4096 (size of dir '.')
# Check total size in DB: "select sum(file_size) from ir_attachment"
# UPDATE SQL: 
# update ir_attachment set db_datas = null
# --select count(*) from ir_attachment
# where store_fname is not null 
#

import xmlrpclib

username = 'admin' #the user
pwd = 'xxx'      #the password of the user
dbname = 'prod_2812'    #the database

# Get the uid
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')

FILESTORE = 2

def migrate_attachment(att_id):
    # 1. get data
    att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', att_id, ['datas','parent_id'])

    dir_id = att['parent_id'][0]
    data = att['datas']

    # 2. Save old storage_id
    dir = sock.execute(dbname, uid, pwd, 'document.directory', 'read', dir_id, ['storage_id'])
    old_storage_id = dir['storage_id'][0]

    if old_storage_id == FILESTORE:
        print "skipping"
        return

    # Set storage to "FileStore"
    sock.execute(dbname, uid, pwd, 'document.directory', 'write', [dir_id], {'storage_id': FILESTORE})

    # Re-Write attachment
    a = sock.execute(dbname, uid, pwd, 'ir.attachment', 'write', [att_id], {'datas': data})

    # Reset storage to "Database"
    sock.execute(dbname, uid, pwd, 'document.directory', 'write', [dir_id], {'storage_id': old_storage_id})


# SELECT attachemnts:
att_ids = sock.execute(dbname, uid, pwd, 'ir.attachment', 'search', [('parent_id','=',1),('store_fname','=',False)])

cnt = len(att_ids)
i = 0
for id in att_ids:
    att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', id, ['datas','parent_id'])

    migrate_attachment(id)
    print 'Migrated ID %d (attachment %d of %d)' % (id,i,cnt)
    i = i + 1
#    if i > 10:
#        break

print "done ..."
2 Comments
ton123 Netherlands
2/20/13, 12:39 PM

Thanks Andreas! I am not familiar with python so I need time to study and test. As a matter of fact I have an old v6.0 version with database store with 300 documents in it. I am happy I can convert if needed. And I also think a script doing the opposite can be made based on this script.

ton123 Netherlands
2/28/13, 5:13 AM

Although the answer is not 100% touch, for me it is good enough to vote for correct answer.

5
Giulio Marcon Austria
3/28/13, 6:57 AM

I modified the script for OpenERP 7.0:

#!/usr/bin/python

import xmlrpclib

username = 'admin' #the user
pwd = 'password'      #the password of the user
dbname = 'database'    #the database

# Get the uid
sock_common = xmlrpclib.ServerProxy ('<URL>/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy('<URL>/xmlrpc/object')

def migrate_attachment(att_id):
    # 1. get data
    att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', att_id, ['datas'])            

    data = att['datas']

    # Re-Write attachment
    a = sock.execute(dbname, uid, pwd, 'ir.attachment', 'write', [att_id], {'datas': data})

# SELECT attachments:
att_ids = sock.execute(dbname, uid, pwd, 'ir.attachment', 'search', [('store_fname','=',False)])

cnt = len(att_ids)        
i = 0
for id in att_ids:
    att = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', id, ['datas','parent_id'])

    migrate_attachment(id)
    print 'Migrated ID %d (attachment %d of %d)' % (id,i,cnt)
    i = i + 1

print "done ..."

After running the script, clean up your ir_attachments table:

update ir_attachment set db_datas = null where store_fname is not null
vacuum (full, analyze) ir_attachment

Replace <URL> with your OpenERP installation URL (sorry for that, I do not have enough Karma to post the standard localhost link).

3 Comments
ton123 Netherlands
3/28/13, 8:15 AM

Thank you Giulio. This is becoming a nice collection of scripts! Hope we can collect the backwards conversion also.

Lithin T United Arab Emirates
8/27/13, 6:06 AM

How can I restore the files from Filesystem to database?

phoebe Hong Kong
2/28/14, 1:03 AM

Hi Giulio, I'm not sure where to settle this script. I just put it to under openerp installation directory and, change the <URL> part and login information part, and type 'python migration.py' (I called it like that). Then I would get 'done...' in the terminal screen. but in fact, no change in the filestore folder. Could you kindly teach me how to handle the script? Thanks in advance and sorry I'm a newbie in OpenERP

Ask a Question
Writer
Keep Informed
1 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now