Odoo Help

Welcome!

This community 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.

4

How to convert from filestore to databasestore?

By
ton123
on 2/10/13, 10:57 AM 3,521 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?

6

Andreas Brueckl

--Andreas Brueckl--
5056
| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 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 ..."

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
on 2/20/13, 12:39 PM

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

ton123
on 2/28/13, 5:13 AM
5
Giulio Marcon
On 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).

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

ton123
on 3/28/13, 8:15 AM

How can I restore the files from Filesystem to database?

Lithin T
on 8/27/13, 6:06 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

phoebe
on 2/28/14, 1:03 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/10/13, 10:57 AM
Seen: 3521 times
Last updated: 3/16/15, 8:10 AM