Skip to content

[REF] Crude script to create the full analysis between two versions of Odoo

Sylvain LE GAL edited this page Dec 7, 2020 · 2 revisions

Update of the script, taking into account the refactor of openupgrade_records into upgrade_analysis.

https://github.com/OCA/OpenUpgrade/wiki/Crude-script-to-create-the-full-analysis-between-two-versions-of-Odoo

import logging
import odoorpc

# User settings
host_old = 'localhost'
port_old = 8069
db_admin_pw_old = '******'
host_new = 'localhost'
port_new = 8079
db_admin_pw_new = '******'
user_pw_old = user_pw_new = '******'
dbname_old = 'odoo_database_13'
dbname_new = 'odoo_database_14'

# The list of the modules you want to analyze. Let to None,
# if you want analyze all Odoo modules, in a OpenUpgrade context
modules = None

# Set to True, if you want to drop and recreate databases.
# If set to False, you should have created database before, with base installed.
drop_create_old = False
drop_create_new = False

# Set to True if you want to reinstall all modules, and regenerates records
prepare_old = True
prepare_new = True

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)


def drop_db_if_exists(db_admin_pw, host, port, dbname):
    client = odoorpc.ODOO(host, port=port)
    logging.info("Dropping database %s ..." % dbname)
    client.db.drop(db_admin_pw, dbname)
    logging.info("Done dropping.")


def install_single_module(client, module):
    model = client.env['ir.module.module']
    module_ids = model.search([('name', '=', module)])
    if not module_ids:
        raise RuntimeError('No module called %s' % module)
    state = model.read(module_ids, ['state'])[0]['state']
    if state != 'installed':
        logging.info("Installing module %s ..." % module)
        model.button_immediate_install(module_ids)


def prepare_server(db_admin_pw, host, port, dbname, user_pw, drop_create):
    client = odoorpc.ODOO(host, port=port, timeout=999999)
    if drop_create:
        logging.info("Creating database %s ..." % dbname)
        drop_db_if_exists(db_admin_pw, host, port, dbname)
        client.db.create(
            db_admin_pw, dbname, demo=False, lang='en_US', admin_password=user_pw)

    client.login(dbname, 'admin', user_pw)

    logging.info("Install analysis module ... %s" % dbname)
    if float(client.version) < 14:
        analysis_module = 'openupgrade_records'
    else:
        analysis_module = 'upgrade_analysis'
    install_single_module(client, analysis_module)
    if not modules:
        logging.info("Installing all modules ...")
        if float(client.version) < 14:
            client.env['openupgrade.install.all.wizard'].browse([]).install_all(
                [('name', 'not like', 'l10n_%')])
        else:
            wizard_id = client.env['upgrade.install.wizard'].create({})
            wizard = client.env['upgrade.install.wizard'].browse(wizard_id)
            wizard.select_odoo_modules(extra_domain=[('name', 'not like', 'l10n_%')])
            wizard.install_modules()

    else:
        for module in modules:
            install_single_module(client, module)

    logging.info("Generating records ...")
    if float(client.version) < 14:
        client.env['openupgrade.generate.records.wizard'].generate([])
    else:
        client.env['upgrade.generate.record.wizard'].generate([])

    return client


def get_or_create_config_v13(client):
    config_ids = client.env['openupgrade.comparison.config'].search(
        [('database', '=', dbname_old)])
    if config_ids:
        return config_ids[0]
    else:
        return client.env['openupgrade.comparison.config'].create({
            'name': dbname_old,
            'server': host_old,
            'port': port_old,
            'database': dbname_old,
            'username': 'admin',
            'password': user_pw_old,
        })


def get_or_create_config_v14(client):
    config_ids = client.env['upgrade.comparison.config'].search(
        [('database', '=', dbname_old)])
    if config_ids:
        return config_ids[0]
    else:
        return client.env['upgrade.comparison.config'].create({
            'name': dbname_old,
            'server': host_old,
            'port': port_old,
            'database': dbname_old,
            'username': 'admin',
            'password': user_pw_old,
        })


if prepare_old:
    prepare_server(
        db_admin_pw_old, host_old, port_old, dbname_old, user_pw_old, drop_create_old)
else:
    logging.info("Reusing existing database for source release.")

if prepare_new:
    prepare_server(
        db_admin_pw_new, host_new, port_new, dbname_new, user_pw_new, drop_create_new)
else:
    logging.info("Reusing existing database for target release.")

client_new = odoorpc.ODOO(host_new, port=port_new)
client_new.login(dbname_new, 'admin', user_pw_new)

logging.info("Generating analysis ...")
if float(client_new.version) < 14:
    wizard_id = client_new.env['openupgrade.analysis.wizard'].create({
        'server_config': get_or_create_config_v13(client_new),
    })
    client_new.env['openupgrade.analysis.wizard'].get_communication([wizard_id])
else:
    comparison_config_id = get_or_create_config_v14(client_new)
    analysis_id = client_new.env['upgrade.analysis'].create({
        'config_id': comparison_config_id,
    })
    client_new.env['upgrade.analysis'].analyze([analysis_id])
Clone this wiki locally