How to migrate binary field to filestorage

Introduction

By default Tryton stores the data in binary fields in the database, but thanks to the Binary.file_id attribute it is possible to store this data in the FileStore instead. The main goal of doing this is to reduce the size of the database.

Here is a technique you can use to move existing binary data out of the database and into the FileStore.

Configuration

You must first setup the Binary.file_id. Some modules provide a configuration option that can be used to switch on this behavior for their binary fields like the Account Invoice Module.

When changing the configuration, the server must be restarted.

Migration

To move any existing data you can use a trytond-console script which will clean out the database column and add the file to the FileStore.

Here is an example for the invoice report cache that can be pasted into the trytond-console:

models = [pool.get('account.invoice')]
try:
    models.append(pool.get('account.invoice.report.revision'))
except KeyError:
    pass

limit = 1000

for Model in models:
    while True:
        records = Model.search([
                ('invoice_report_cache', '!=', None),
                ], order=[('id', 'ASC')], limit=limit)
        if not records:
            break
        for record in records:
            report = record.invoice_report_cache  # backup the data
            record.invoice_report_cache = None   # clean the database column
            record.save()
            record.invoice_report_cache = report  # restore the data
            record.save()
        transaction.commit()
5 Likes

A post was split to a new topic: Migrate only half of binary fields