19

I have some modules which expand add-ons of Odoo. For example, models in my_module which expand crm:

class Lead(models.Model):
    _inherit = 'crm.lead'
    # exmaple fields
    field_1 = fields.Char(...)
    field_2 = fields.Many2one(...)
    # ... field 99


class Stage(models.Model):
    _inherit = 'crm.stage'
    # exmaple fields
    field_1 = fields.Char(...)
    field_2 = fields.Many2one(...)
    # ... field 99

The same situation is for modules which expand hr, product, etc. I need to make some changes to the models. For example, in my_module_1, I need to change a couple of fields(type, relation), in my_module_2, just to remove a few fields etc. Of course I also need to change views of each module. And of course I have my custom models which have dependencies with models from different apps/modules. But I have data on production which must be stored. I did not find any information about migrations(or synchronization of modules) in Odoo.

My question is: What is the best way to update modules/apps on production(if we have many changes in fields of models and views)? Thanks in advance.

Danila Ganchar
  • 7,271
  • 11
  • 35
  • 59

5 Answers5

2

My question is: What is the best way to update modules/apps on production(if we have many changes in fields of models and views)?

While this question has been around for a while I cannot see any canonical answer therefore here are my two cents.

The problem of migration from one version to another in Odoo is rather common and definitely complicated. With that in mind the OpenUpgrade project has been created. OpenUpgrade is basically an upgrade path that will help you transform your data and models from version A to version B. For example if a field named fieldA has had its type changed on version 9 and you are on version 8, OpenUpgrade will take care of this by doing the necessary transformations.

OpenUpgrade also gives you the possibility to create your own migration scripts that will do whatever needs to be done in order for your module to be forward ported (or back ported) in various versions. For the standard modules these scripts have already been written to an extend, but for your own you might need to do some writing.

I suggest that you take a look at the documentation above, this is basically your first stop when we talk about migrations in Odoo.


George Daramouskas
  • 3,470
  • 3
  • 18
  • 48
  • I didn't work with `Odoo` last years and I didn't use `Automated migration script`. But I think your answer is very useful. It looks like what I was looking for. So thank you so much! – Danila Ganchar Nov 08 '18 at 15:12
0

First you have to dump the production database and then restore it in your local system.

Once restored in local system, develop your custom modules to expand the existing model features.

Install the developed modules in local system(restored database) and see the changes you made. If everything is perfect with existing data, then Install that module in production database.

To dump the production database execute the below command in postgres. Command : pg_dump dbname > outfile Example : pg_dump prod_db > prod_db.sql

Before restoring the database, you have to create fresh database in your local system. To create fresh database execute the below command, command : createdb --owner owner_name --encoding utf-8 dbname Example : createdb --owner odoo --encoding utf-8 prod_db

To restore the production database execute the below command in postgres. Command : psql dbname < infile path Example : pg_dump prod_db > prod_db.sql

Nilesh Sheliya
  • 591
  • 2
  • 10
  • I have no problem with Postgres or Dumps. I will try to explain... On production was installed some modules. I changed some fields in models. To see changes in models need to restart `openerp-server`. But the server will not start, because structure of tables and models is different. If we click `uninstall` button -> deploy models to server -> click `install` all will be fine, but in this case we lost all data. So, I want to know it is possible to create some **migration script** and use them when we click **update module** or before `start server`. – Danila Ganchar Jan 08 '16 at 08:52
  • Have you changed fields datatype in model of existing fields or added new fields in model? – Nilesh Sheliya Jan 08 '16 at 09:01
  • I'm just looking for a quick and easy way to update modules. I can not believe that there is no tool to generate migrations based on the difference Odoo-models / tables. – Danila Ganchar Jan 08 '16 at 09:01
  • Data is only lost if you change the datatype of existing fields, otherwise all data are resides as it is in database. You just need to changes in xml views to show new fields value. – Nilesh Sheliya Jan 08 '16 at 09:04
  • Ok. Try to add 5-10 different fields, show them in your view and install module. After this try to remove 2 fields, change type from **selection** to **many2one** in one field, add 2 new fields. To see changes you must restart server. Try to restart. You will have errors in your module. – Danila Ganchar Jan 08 '16 at 09:11
  • Yes, In the above case you lost the data. Because of you have changed the datatype of field. – Nilesh Sheliya Jan 08 '16 at 09:20
  • So, I'm looking for a modern and quick solution how best to update module that works. + I can have dependencies between my custom models in different modules. – Danila Ganchar Jan 08 '16 at 09:26
0

Maybe there are some tools for migrations in Odoo enterprise version, but I haven't found any information about this. So the solution that I use is to do everything manually. Step by step, attentively. If you know a better way let me know.

For example. If we need to remove some fields:

  1. Remove fields from models and views in your module/app.
  2. Stop openerp-server. Run sql which drop our columns.
  3. Deploy new version models and views to the server. Start openerp-server.
  4. Activate developer mode -> Go to settings -> find your module/app -> click Update(Upgrade)

If you need to change the type/relation of fields:

  1. Add a field(with a new type/relation) to your model.
  2. Prepare the sql script which will transfer data from the old column to the new column/tables.
  3. Remove the old field from your model and views.
  4. Stop openerp-server. Run the sql script, drop old columns.
  5. Deploy new version models and views to the server. Start openerp-server.
  6. Activate developer mode -> Go to settings -> find your module/app -> click Update(Upgrade)

Be careful when you change relations(one2many, many2many). Make dumps and versions of apps. Check your modifications on local machine with db from production for a couple of times.

One more thing about new fields with relations. For example I have module_1 which depends on crm. module_2 which depends on module_1 etc. I need to add some fields to crm models and show them in module_1. In module_2 I need to show new fields from my custom models in module_1.

We can add all the new fields to models and views in our modules. Stop the server and run the server with the parameter --update like this:

./openerp-server --update=all

In this case all the modules will be updated. If we need to update only the modules which depend on crm we need just to update crm:

./openerp-server --update=crm
Danila Ganchar
  • 7,271
  • 11
  • 35
  • 59
0

Restart the server from the command line once with the -u and -d flags eg.

sudo service odoo stop
/path/to/odoo/odoo.py -d <your_db_name> -u custom_module1,custom_module2

If you're dealing with a production server, I would test this locally, on your development machine with a fresh dump of the production DB make sure it works, tweak it if needed (eg some fields might need defaults, whatever), test it again on yet another fresh dump, until the point that all I need to do is restart the server as above to make the changes happen. Once that happens, back up the database, datastore and even the affected modules on the production server, upload the new module(s) and restart the production server as above (no dumping databases from test to production here) the module update should take care of database changes.

If you're trying to change the structure of a table considerably (eg changing fields datatype) and retain data in tables the only way I can think of doing this is to create add NEW fields first with the new datatype, populate them with data from old fields (either directly with postgres queries or within your "interim version module") and this really depends on the changes, a change from selection to many2one involves inserting selection values into a new table, two very different things from the database point of view the actual field type on the table will be a integer, the ID from the row that holds the selection value in a relational table...

Once your new fields are populated, make the final version of the module removing all the fields you no longer need (keep the other version for the production database).

I would probably test the database population manually on the development server first either in postgres or with some tool like pgadminIII, but plan on creating a script for doing it on the production server (or better yet build it all into the new module version) as it will have to be down while that happens.

I would also look at my postgres tables after it's all done, some fields might still be there even if the new module doesn't use them.

Sorry, I don't know of any simpler, more automatic way of doing it, there just is too many variables...

Sciallo
  • 43
  • 1
  • 7
  • it is way like in my answer just with parameter *-d* for database name. But I was looking for easy way for creating migrations, transfer data etc. 'from box' – Danila Ganchar May 05 '16 at 13:25
0

As per your example, I believe that you already know how to add a new field to an existing model.

I still do not understand why anyone would want to remove a field from an existing model - this is/will be more trouble than it is worth (if there is a valid reason, please let me know). This also applies when attempting to recast field types. With this said, you can quite easily remove/replace/hide an existing field on a view, which should in essence achieve the same outcome.

https://www.odoo.com/forum/help-1/question/add-remove-fields-to-inherited-custom-module-72945

<record model="ir.ui.view" id="enter_an_id_here">
    <field name="name">some.text.here.form</field>
    <field name="model">crm.lead</field>
    <field name="inherit_id" ref="crm.external_id_here" />
    <field name="arch" type="xml">
        <field name="name" position="after">
            <field name="your_field_name"/>
        </field>
    </field>
</record>

You also talked about migrating data from dependents to another model via copy/paste. This should not be required as you can simply access the data in the existing model through a direct object/field reference, or by using a related field.

related field on odoo?

new_field = fields.Char(string='String', related='product_id.name')
or inside python
value = self.product_id.name

I will not address anything to do with installing the module/server side commands as the other answers here have already addressed these aspects.

Community
  • 1
  • 1
Palza
  • 356
  • 2
  • 12
  • Ok. I have 10-50 custom modules. Some columns are not used. Of Course I can just hide it(but I think it is wrong way). What about renaming, changing types/ relations etc? I can create sql scripts manually and execute their. What about history, transactions? I should manually stop server, execute scripts, update sources, update modules and run server? What if there is a problem with the module upgrade? It is not modern way. – Danila Ganchar Sep 23 '16 at 07:56
  • As far as I am aware you cannot rename fields on existing models. When you enter a different name it will actually create a new field. If you did rename a field then it will remove the old field from the model, but not from the database. This means the model will have the new field and the DB will have both. If you remove a field from a model, then you also have to remove the field from all views, and also remove any fields on other models which referred to the original field that you removed. This could also impact any modules you install at a later date that use a field you have removed. – Palza Sep 29 '16 at 01:35
  • In regard to the scripts etc - You can run SQL scripts at any time, your preferred timing is normally dependent on what you are trying to accomplish. Ask yourself why you want odoo up or down during these times when the scripts are run. – Palza Sep 29 '16 at 01:50
  • I know what you wrote above. But I can't believe that there is no module for migrations. Creating sql-scripts manually or using third-party tools it looks at least strange. All modern libs and frameworks provide possibility to generate difference between models and db, run migrations before application and rollback them, view history etc. I think all solutions is manual, but not automatic - too much actions for the simple things. – Danila Ganchar Sep 29 '16 at 07:31