From ac623419ebd332d4a6b2b872d7b64082cf1863b7 Mon Sep 17 00:00:00 2001 From: aheficent Date: Thu, 27 Jul 2017 12:51:37 +0200 Subject: [PATCH] [IMP]add uninstall hook. Add checks when deleting records. --- bi_sql_editor/__init__.py | 1 + bi_sql_editor/__openerp__.py | 1 + bi_sql_editor/demo/bi_sql_view_demo.xml | 88 +++++++++++++------------ bi_sql_editor/hooks.py | 52 +++++++++++++++ bi_sql_editor/models/bi_sql_view.py | 37 +++++++---- bi_sql_editor/tests/__init__.py | 5 -- bi_sql_editor/tests/test_bi_sql_view.py | 56 ---------------- 7 files changed, 124 insertions(+), 116 deletions(-) create mode 100644 bi_sql_editor/hooks.py delete mode 100644 bi_sql_editor/tests/__init__.py delete mode 100644 bi_sql_editor/tests/test_bi_sql_view.py diff --git a/bi_sql_editor/__init__.py b/bi_sql_editor/__init__.py index cde864ba..e4f76a9e 100644 --- a/bi_sql_editor/__init__.py +++ b/bi_sql_editor/__init__.py @@ -1,3 +1,4 @@ # -*- coding: utf-8 -*- from . import models +from .hooks import uninstall_hook diff --git a/bi_sql_editor/__openerp__.py b/bi_sql_editor/__openerp__.py index 8670ca6e..edd4bf5f 100644 --- a/bi_sql_editor/__openerp__.py +++ b/bi_sql_editor/__openerp__.py @@ -25,4 +25,5 @@ 'demo/bi_sql_view_demo.xml', ], 'installable': True, + 'uninstall_hook': 'uninstall_hook' } diff --git a/bi_sql_editor/demo/bi_sql_view_demo.xml b/bi_sql_editor/demo/bi_sql_view_demo.xml index 29e1b767..7758110b 100644 --- a/bi_sql_editor/demo/bi_sql_view_demo.xml +++ b/bi_sql_editor/demo/bi_sql_view_demo.xml @@ -5,48 +5,49 @@ Copyright (C) 2014 - Today GRAP (http://www.grap.coop) License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> - - - - Draft Incorrect SQL View - incorrect_view - - - - - - Partners View - partners_view - - - - - - Modules by Authors - modules_view - - - - + + + + + Draft Incorrect SQL View + incorrect_view + + + + + + Partners View + partners_view + + + + + + Modules by Authors + modules_view + + + + @@ -54,4 +55,5 @@ FROM ir_module_module - + + diff --git a/bi_sql_editor/hooks.py b/bi_sql_editor/hooks.py new file mode 100644 index 00000000..ee78f707 --- /dev/null +++ b/bi_sql_editor/hooks.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2015-2017 Onestein () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp import SUPERUSER_ID +from openerp.api import Environment + + +def uninstall_hook(cr, registry): + env = Environment(cr, SUPERUSER_ID, {}) + recs = env['bi.sql.view'].search([]) + for rec in recs: + rec.button_set_draft() + rec.unlink() + + # delete dirty data that could cause problems + # while re-installing the module + # Drop materialized views + cr.execute(""" + select relname + from pg_class + where relname like 'x_bi_sql_view%' and relkind='m' + """) + for r in cr.fetchall(): + cr.execute(""" + DROP MATERIALIZED VIEW %s + """ % r) + + cr.execute(""" + select relname + from pg_class + where relname like 'x_bi_sql_view%' and relkind='r' + """) + for r in cr.fetchall(): + cr.execute(""" + DROP TABLE %s + """ % r) + cr.execute(""" + select table_name from INFORMATION_SCHEMA.views + where table_name like 'x_bi_sql%'""") + + # Drop not materialized views + for v in cr.fetchall(): + cr.execute(""" + DROP VIEW %s + """ % v) + + # Drop table if uninstalling went wrong + cr.execute(""" + delete from ir_model_fields where model like 'bi.sql.view%'; + delete from ir_model_fields where model like 'bi_sql_%'; + delete from ir_model where model like 'x_bi_sql_view.%'; + """) diff --git a/bi_sql_editor/models/bi_sql_view.py b/bi_sql_editor/models/bi_sql_view.py index 048c4d42..8b7ae9ff 100644 --- a/bi_sql_editor/models/bi_sql_view.py +++ b/bi_sql_editor/models/bi_sql_view.py @@ -208,13 +208,20 @@ class BiSQLView(models.Model): # Drop ORM sql_view._drop_model_and_fields() - sql_view.tree_view_id.unlink() - sql_view.graph_view_id.unlink() - sql_view.pivot_view_id.unlink() - sql_view.search_view_id.unlink() - sql_view.action_id.unlink() - sql_view.menu_id.unlink() - sql_view.rule_id.unlink() + if sql_view.tree_view_id: + sql_view.tree_view_id.unlink() + if sql_view.graph_view_id: + sql_view.graph_view_id.unlink() + if sql_view.pivot_view_id: + sql_view.pivot_view_id.unlink() + if sql_view.search_view_id: + sql_view.search_view_id.unlink() + if sql_view.action_id: + sql_view.action_id.unlink() + if sql_view.menu_id: + sql_view.menu_id.unlink() + if sql_view.rule_id: + sql_view.rule_id.unlink() if sql_view.cron_id: sql_view.cron_id.unlink() sql_view.write({'state': 'draft', 'has_group_changed': False}) @@ -409,9 +416,14 @@ class BiSQLView(models.Model): @api.multi def _drop_view(self): for sql_view in self: - self._log_execute( - "DROP %s VIEW IF EXISTS %s" % ( - sql_view.materialized_text, sql_view.view_name)) + try: + self._log_execute( + "DROP %s VIEW IF EXISTS %s" % ( + sql_view.materialized_text, sql_view.view_name)) + except ProgrammingError as e: + # If it is not a materialized view will raise an error, + # and we pass. + continue sql_view.size = False @api.multi @@ -464,7 +476,8 @@ class BiSQLView(models.Model): @api.multi def _drop_model_and_fields(self): for sql_view in self: - sql_view.model_id.unlink() + if sql_view.model_id: + sql_view.model_id.unlink() @api.multi def _hook_executed_request(self): @@ -547,7 +560,7 @@ class BiSQLView(models.Model): @api.multi def _refresh_materialized_view(self): - for sql_view in self: + for sql_view in self.filtered(lambda v: v.is_materialized == True): req = "REFRESH %s VIEW %s" % ( sql_view.materialized_text, sql_view.view_name) self._log_execute(req) diff --git a/bi_sql_editor/tests/__init__.py b/bi_sql_editor/tests/__init__.py deleted file mode 100644 index 7b4cf478..00000000 --- a/bi_sql_editor/tests/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 Onestein () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from . import test_bi_sql_view diff --git a/bi_sql_editor/tests/test_bi_sql_view.py b/bi_sql_editor/tests/test_bi_sql_view.py deleted file mode 100644 index c555e635..00000000 --- a/bi_sql_editor/tests/test_bi_sql_view.py +++ /dev/null @@ -1,56 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 Onestein () -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openerp.tests.common import TransactionCase, at_install, post_install -from openerp.exceptions import Warning as UserError - - -@at_install(False) -@post_install(True) -class TestBiSqlViewEditor(TransactionCase): - - def setUp(self): - super(TestBiSqlViewEditor, self).setUp() - self.res_partner = self.env['res.partner'] - self.res_users = self.env['res.users'] - self.bi_sql_view = self.env['bi.sql.view'] - self.view = self.env.ref( - 'bi_sql_editor.partner_sql_view') - # deleting the existing views otherwise it fails - self.view.state = 'model_valid' - self.view.button_set_draft() - self.group_bi_user = self.env.ref( - 'sql_request_abstract.group_sql_request_user') - self.group_user = self.env.ref( - 'base.group_user') - self.company = self.env.ref('base.main_company') - - def test_process_view(self): - self.assertEqual(self.view.state, 'draft', 'state not draft') - self.view.button_validate_sql_expression() - self.assertEqual(self.view.state, 'sql_valid', 'state not sql_valid') - self.view._check_execution() - for field in self.view.bi_sql_view_field_ids: - field.graph_type = 'row' - self.view.button_create_sql_view_and_model() - self.assertEqual(self.view.state, 'model_valid', - 'state not model_valid') - self.view.button_create_ui() - self.assertEqual(self.view.state, 'ui_valid', 'state not ui_valid') - self.view.button_open_view() - self.view.button_set_draft() - - def test_copy(self): - self.assertEqual(self.view.mod, 'draft', 'state not draft') - copy_view = self.view.copy() - self.assertEqual(copy_view.name, 'Partners View (Copy)', 'Wrong name') - - def test_unlink(self): - self.assertEqual(self.view.state, 'draft', 'state not draft') - self.view.button_validate_sql_expression() - self.view.button_create_sql_view_and_model() - with self.assertRaises(UserError): - self.view.unlink() - self.view.button_set_draft() - self.view.unlink()