Blog Post

...

Odoo module upgrade on multiple databases

This post provides the script that solves the problem of automated module upgrade on multiple databases in Odoo 10.

If you've worked with Odoo you might know that in order to install some new functionality, you need to install (or update) the appropriate module, i.e. initiate the upgrade process on a database. By default Odoo can deal with this smoothly, just by going to Modules List (having Admin privileges) and pressing "Upgrade" button. The issue here is that this upgrade process takes place on the current database only. And what if there are multiple databases exist, then you have to login to each DB entity via Odoo and navigate to Module list and press that Upgrade button. These steps to be performed manually and if there are, let's say, more than 3 DBs then it may be really annoying to get through each upgrade. There are many inquires on the internet to solve that problem, e.g. here and here, but the solution there is outdated, so in this post you’ll find a workable script for modern Odoo 10 version.

To automate the module upgrade process - a Python script is written that can be invoked on Lynux only (because it involves the usage of pexpect library, which is available on Linux only), no Windows, sorry.

Firstly we get the list of available databases in Postgres, then we iterate through each of them to perform an upgrade.

Second important step is to form odoo-bin execution command. Odoo-bin is an entry point to start/upgrade Odoo instance and in this way it allows to initiate the update of some particular module on a database using predefined parameters (e.g. --database, --update).

Third step is to use pexpect lib to invoke the command on odoo-bin and keep track of Odoo log to catch the message "Modules loaded.", which means that Odoo has finished upgrade and we can switch to the other DB.

The final script looks so:

#!/usr/bin/env python

from datetime import date
import subprocess, xmlrpclib, sys, os, psycopg2, pexpect

# Database info
db_user = 'odoo'
db_passwd = ''
db_host = 'localhost'
db_port = 5432
db_name = 'postgres'
oerp_conf = '/opt/odoo/odoo.conf'
oerp_bin = "/opt/odoo/odoo-bin"
module_to_upgrade = "module_to_upgrade"

today = date.today()
log_date = today.strftime('%Y%m%d')
log_name = "update-{}.log".format(log_date)
base_dir = os.getcwd()

with open(log_name, 'a+') as log_file:

  # Get list of databases
  db = psycopg2.connect(user=       db_user, 
                        password=   db_passwd,
                        host=       db_host,
                        port=       db_port,
                        database=   db_name)
  cr = db.cursor()
  cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename='{}') order by datname".format(db_user))
  dblist = [str(name) for (name,) in cr.fetchall()]

  for database in dblist:
    # Wait for server to upgrade database, then kill it
    print "Upgrading database {}...".format(database)
    
    dbupdate_cmd = "{} shell -c {} --database={} --update={}".format(oerp_bin, oerp_conf, database, module_to_upgrade) 
    output = pexpect.spawn(dbupdate_cmd)
    try:
      # to catch this string loading.py was altered (line 461)
      output.expect('.*Modules loaded.', timeout=600)

    except pexpect.ExceptionPexpect as e:
      print "Timeout reached while upgrading {}. Try manually upgrading the database with the command '{}'.".format(database, dbupdate_cmd)
    output.kill(0)
    status = "Upgraded database {}.".format(database)
    print status
    log_file.write(status + "\n")

This script is also provides logging to console and external file, so you could always check the upgrade progress and results.

In this post the multiple database upgrade script for Odoo 10 is introduced. We use it pretty often and I hope you’ll like it as we do.

Comments (5)

Tags: odoo


Comments:

...

rithiaanandh Jun 20, 2019 at 14:21 #

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact, your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it. Sql server dba online training

...

sasi Jun 24, 2019 at 10:51 #

Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this.  ReactJS Online Training

...

gowsalya Jun 26, 2019 at 08:36 #

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well. msbi online training

...

priya Jun 29, 2019 at 12:11 #

I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done! Tableau online training

...

Kiruthika Jul 02, 2019 at 10:01 #

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.. Oracle DBA Online Training

Leave a Comment