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.

1

More generic model -> Class renaming without data losing

By
Cyril Sester
on 8/15/14, 2:56 AM 1,118 views

Hi,

I'm currently refactoring a specific class in something more generic in order to re-use it in other use cases. For example :

My initial class is sepa_mandate. In order to reuse the mandate object, I want to create a new class called banking_mandate and then refactore sepa_mandate to inherit and specialize banking_mandate.

Everything works fine if the module is newly installed. But when the old module is already installed, is there a way to migrate data contained in "sepa_mandate" table to "banking_mandate" table?

Best regards,

Cyril

1
Cyril Sester
On 8/15/14, 5:17 AM

Wow, rubber duck debugging really works :)

More seriously, I will simply answer myself to maybe help someone with the same question. Openerp allows you to write migration scripts. Searching around, I just found the openerp/modules/migration.py file containing a really great description of how to do upgrade pre or post treatment.

        This class manage the migration of modules
        Migrations files must be python files containing a "migrate(cr, installed_version)" function.
        Theses files must respect a directory tree structure: A 'migrations' folder which containt a
        folder by version. Version can be 'module' version or 'server.module' version (in this case,
        the files will only be processed by this version of the server). Python file names must start
        by 'pre' or 'post' and will be executed, respectively, before and after the module initialisation
        Example:

            <moduledir>
            `-- migrations
                |-- 1.0
                |   |-- pre-update_table_x.py
                |   |-- pre-update_table_y.py
                |   |-- post-clean-data.py
                |   `-- README.txt              # not processed
                |-- 5.0.1.1                     # files in this folder will be executed only on a 5.0 server
                |   |-- pre-delete_table_z.py
                |   `-- post-clean-data.py
                `-- foo.py                      # not processed

        This similar structure is generated by the maintenance module with the migrations files get by
        the maintenance contract

In my case, I just wrote some SQL queries in order to copy data from the old table to the new one and also from old FK to new ones. After that, I also cleaned up the old fields and old table to keep the database clean.

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: 8/15/14, 2:56 AM
Seen: 1118 times
Last updated: 3/16/15, 8:10 AM