This question has been flagged
2 Replies
10600 Views

Does anybody know an easy method how to convert all the knowledgemanagement files in the filestore internal or external to database and visa versa?

Avatar
Discard
Best Answer

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 ..."
Avatar
Discard
Author

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.

Author

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

Best Answer

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).

Avatar
Discard
Author

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

How can I restore the files from Filesystem to database?

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