|
@ -4,20 +4,34 @@ |
|
|
from psycopg2 import ProgrammingError |
|
|
from psycopg2 import ProgrammingError |
|
|
from openerp.modules.registry import RegistryManager |
|
|
from openerp.modules.registry import RegistryManager |
|
|
from openerp.tools import config |
|
|
from openerp.tools import config |
|
|
from openerp.tests.common import TransactionCase |
|
|
|
|
|
|
|
|
from openerp.tests.common import TransactionCase, at_install, post_install |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Use post_install to get all models loaded more info: odoo/odoo#13458 |
|
|
|
|
|
@at_install(False) |
|
|
|
|
|
@post_install(True) |
|
|
class TestDatabaseCleanup(TransactionCase): |
|
|
class TestDatabaseCleanup(TransactionCase): |
|
|
|
|
|
def setUp(self): |
|
|
|
|
|
super(TestDatabaseCleanup, self).setUp() |
|
|
|
|
|
self.module = None |
|
|
|
|
|
self.model = None |
|
|
|
|
|
|
|
|
def test_database_cleanup(self): |
|
|
def test_database_cleanup(self): |
|
|
# create an orphaned column |
|
|
# create an orphaned column |
|
|
self.cr.execute( |
|
|
self.cr.execute( |
|
|
'alter table res_users add column database_cleanup_test int') |
|
|
|
|
|
purge_columns = self.env['cleanup.purge.wizard.column'].create({}) |
|
|
|
|
|
|
|
|
'alter table res_partner add column database_cleanup_test int') |
|
|
|
|
|
# We need use a model that is not blocked (Avoid use res.users) |
|
|
|
|
|
partner_model = self.env['ir.model'].search([ |
|
|
|
|
|
('model', '=', 'res.partner')], limit=1) |
|
|
|
|
|
purge_columns = self.env['cleanup.purge.wizard.column'].create({ |
|
|
|
|
|
'purge_line_ids': [(0, 0, { |
|
|
|
|
|
'model_id': partner_model.id, 'name': 'database_cleanup_test'} |
|
|
|
|
|
)]}) |
|
|
purge_columns.purge_all() |
|
|
purge_columns.purge_all() |
|
|
# must be removed by the wizard |
|
|
# must be removed by the wizard |
|
|
with self.assertRaises(ProgrammingError): |
|
|
with self.assertRaises(ProgrammingError): |
|
|
with self.registry.cursor() as cr: |
|
|
with self.registry.cursor() as cr: |
|
|
cr.execute('select database_cleanup_test from res_users') |
|
|
|
|
|
|
|
|
cr.execute('select database_cleanup_test from res_partner') |
|
|
|
|
|
|
|
|
# create a data entry pointing nowhere |
|
|
# create a data entry pointing nowhere |
|
|
self.cr.execute('select max(id) + 1 from res_users') |
|
|
self.cr.execute('select max(id) + 1 from res_users') |
|
@ -34,7 +48,7 @@ class TestDatabaseCleanup(TransactionCase): |
|
|
self.env.ref('database_cleanup.test_no_data_entry') |
|
|
self.env.ref('database_cleanup.test_no_data_entry') |
|
|
|
|
|
|
|
|
# create a nonexistent model |
|
|
# create a nonexistent model |
|
|
self.env['ir.model'].create({ |
|
|
|
|
|
|
|
|
self.model = self.env['ir.model'].create({ |
|
|
'name': 'Database cleanup test model', |
|
|
'name': 'Database cleanup test model', |
|
|
'model': 'x_database.cleanup.test.model', |
|
|
'model': 'x_database.cleanup.test.model', |
|
|
}) |
|
|
}) |
|
@ -45,14 +59,18 @@ class TestDatabaseCleanup(TransactionCase): |
|
|
self.registry._pure_function_fields.pop( |
|
|
self.registry._pure_function_fields.pop( |
|
|
'x_database.cleanup.test.model') |
|
|
'x_database.cleanup.test.model') |
|
|
purge_models = self.env['cleanup.purge.wizard.model'].create({}) |
|
|
purge_models = self.env['cleanup.purge.wizard.model'].create({}) |
|
|
purge_models.purge_all() |
|
|
|
|
|
# must be removed by the wizard |
|
|
|
|
|
self.assertFalse(self.env['ir.model'].search([ |
|
|
|
|
|
('model', '=', 'x_database.cleanup.test.model'), |
|
|
|
|
|
])) |
|
|
|
|
|
|
|
|
with self.assertRaisesRegexp(KeyError, |
|
|
|
|
|
'x_database.cleanup.test.model'): |
|
|
|
|
|
# TODO: Remove with-assert of KeyError after fix: |
|
|
|
|
|
# https://github.com/odoo/odoo/pull/13978/files#r88654967 |
|
|
|
|
|
purge_models.purge_all() |
|
|
|
|
|
# must be removed by the wizard |
|
|
|
|
|
self.assertFalse(self.env['ir.model'].search([ |
|
|
|
|
|
('model', '=', 'x_database.cleanup.test.model'), |
|
|
|
|
|
])) |
|
|
|
|
|
|
|
|
# create a nonexistent module |
|
|
# create a nonexistent module |
|
|
self.env['ir.module.module'].create({ |
|
|
|
|
|
|
|
|
self.module = self.env['ir.module.module'].create({ |
|
|
'name': 'database_cleanup_test', |
|
|
'name': 'database_cleanup_test', |
|
|
'state': 'to upgrade', |
|
|
'state': 'to upgrade', |
|
|
}) |
|
|
}) |
|
@ -78,3 +96,18 @@ class TestDatabaseCleanup(TransactionCase): |
|
|
with self.assertRaises(ProgrammingError): |
|
|
with self.assertRaises(ProgrammingError): |
|
|
with self.registry.cursor() as cr: |
|
|
with self.registry.cursor() as cr: |
|
|
self.env.cr.execute('select * from database_cleanup_test') |
|
|
self.env.cr.execute('select * from database_cleanup_test') |
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self): |
|
|
|
|
|
super(TestDatabaseCleanup, self).tearDown() |
|
|
|
|
|
with self.registry.cursor() as cr2: |
|
|
|
|
|
# Release blocked tables with pending deletes |
|
|
|
|
|
self.env.cr.rollback() |
|
|
|
|
|
if self.module: |
|
|
|
|
|
cr2.execute( |
|
|
|
|
|
"DELETE FROM ir_module_module WHERE id=%s", |
|
|
|
|
|
(self.module.id,)) |
|
|
|
|
|
if self.model: |
|
|
|
|
|
cr2.execute( |
|
|
|
|
|
"DELETE FROM ir_model WHERE id=%s", |
|
|
|
|
|
(self.model.id,)) |
|
|
|
|
|
cr2.commit() |