Last active
October 8, 2019 08:38
-
-
Save amatmv/0724eaeca3381589bcd71261607cc5d5 to your computer and use it in GitHub Desktop.
Print the queries that may be executed to uninstall the module. The module to uninstall will be indicated with a SQL Regex with the parameter --module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from erppeek import Client | |
import click | |
def uninstall_module_get_queries(client, module_id): | |
queries = [] | |
imd_o = client.model('ir.model.data') | |
module_o = client.model('ir.module.module') | |
module_vals = module_o.read(module_id, ['name']) | |
obres_imd_ids = imd_o.search([('module', '=', module_vals['name'])]) | |
obres_imd_values = imd_o.read(obres_imd_ids, ['id', 'model', 'res_id']) | |
uninstall_query = """ | |
UPDATE ir_module_module SET state = 'uninstalled' WHERE id = {}; | |
""".format(module_vals['id']) | |
queries.append(uninstall_query) | |
for imd_value in obres_imd_values: | |
table = client.model(imd_value['model']) | |
# We don't have the _table attr through erppeek, so we get | |
# it by replacing dots by underscores | |
table_name = table._name.replace('.', '_') | |
# The table 'ir_actions_act_window' has another table name, so we | |
# cannot simply get it by replacing dots | |
if table_name == 'ir_actions_act_window': | |
table_name = 'ir_act_window' | |
queries.append(""" | |
DELETE FROM {table} | |
WHERE id = {id}; | |
""".format( | |
table=table_name, id=imd_value['res_id'] | |
)) | |
queries.append(""" | |
DELETE FROM ir_model_data WHERE id = {}; | |
""".format(imd_value['id'])) | |
return queries | |
@click.command() | |
@click.option('-u', '--user', default='admin', help='Usuari servidor ERP') | |
@click.option('-w', '--password', default='admin', help='Contrasenya usuari ERP') | |
@click.option('-s', '--server', default='localhost', help='IP servidor ERP') | |
@click.option('-p', '--port', default=8069, help='Port servidor ERP', type=click.INT) | |
@click.option('-d', '--database', help='Nom de la BD') | |
@click.option('-m', '--module', help='Module (SQL)regex to remove') | |
def uninstall_modules(**kwargs): | |
server = 'http://{0}:{1}'.format(kwargs['server'], | |
kwargs['port']) | |
c = Client(server=server, db=kwargs['database'], user=kwargs['user'], | |
password=kwargs['password']) | |
module_o = c.model('ir.module.module') | |
modules_to_uninstall_ids = module_o.search([ | |
('name', 'like', kwargs['module']), | |
('state', '=', 'installed') | |
]) | |
# Print queries to execute | |
for module_id in modules_to_uninstall_ids: | |
queries_to_exec = uninstall_module_get_queries(c, module_id) | |
for q in queries_to_exec: | |
print ("BEGIN;") | |
print (q) | |
print ("COMMIT;") | |
if __name__ == "__main__": | |
uninstall_modules() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment