You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.7 KiB

  1. # Copyright 2016 Therp BV <http://therp.nl>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from contextlib import contextmanager
  4. from psycopg2 import ProgrammingError
  5. from odoo.modules.registry import Registry
  6. from odoo.tools import config, mute_logger
  7. from odoo.tests.common import TransactionCase, tagged
  8. # Use post_install to get all models loaded, more info: odoo/odoo#13458
  9. @tagged('post_install', '-at_install')
  10. class TestDatabaseCleanup(TransactionCase):
  11. def setUp(self):
  12. super(TestDatabaseCleanup, self).setUp()
  13. self.modules = self.env['ir.module.module']
  14. self.models = self.env['ir.model']
  15. # Create one property for tests
  16. self.env['ir.property'].create({
  17. 'fields_id': self.env.ref('base.field_res_partner__name').id,
  18. 'type': 'char',
  19. 'value_text': 'My default partner name',
  20. })
  21. def test_database_cleanup(self):
  22. # delete some index and check if our module recreated it
  23. self.env.cr.execute('drop index res_partner_name_index')
  24. create_indexes = self.env['cleanup.create_indexes.wizard'].create({})
  25. create_indexes.purge_all()
  26. self.env.cr.execute(
  27. 'select indexname from pg_indexes '
  28. "where indexname='res_partner_name_index' and "
  29. "tablename='res_partner'"
  30. )
  31. self.assertEqual(self.env.cr.rowcount, 1)
  32. # duplicate a property
  33. duplicate_property = self.env['ir.property'].search([], limit=1).copy()
  34. purge_property = self.env['cleanup.purge.wizard.property'].create({})
  35. purge_property.purge_all()
  36. self.assertFalse(duplicate_property.exists())
  37. # create an orphaned column
  38. self.env.cr.execute(
  39. 'alter table res_partner add column database_cleanup_test int')
  40. # We need use a model that is not blocked (Avoid use res.users)
  41. partner_model = self.env['ir.model'].search([
  42. ('model', '=', 'res.partner')], limit=1)
  43. purge_columns = self.env['cleanup.purge.wizard.column'].create({
  44. 'purge_line_ids': [(0, 0, {
  45. 'model_id': partner_model.id, 'name': 'database_cleanup_test'}
  46. )]})
  47. purge_columns.purge_all()
  48. # must be removed by the wizard
  49. with self.assertRaises(ProgrammingError):
  50. with self.env.registry.cursor() as cr:
  51. with mute_logger('odoo.sql_db'):
  52. cr.execute('select database_cleanup_test from res_partner')
  53. # create a data entry pointing nowhere
  54. self.env.cr.execute('select max(id) + 1 from res_users')
  55. self.env['ir.model.data'].create({
  56. 'module': 'database_cleanup',
  57. 'name': 'test_no_data_entry',
  58. 'model': 'res.users',
  59. 'res_id': self.env.cr.fetchone()[0],
  60. })
  61. purge_data = self.env['cleanup.purge.wizard.data'].create({})
  62. purge_data.purge_all()
  63. # must be removed by the wizard
  64. with self.assertRaises(ValueError):
  65. self.env.ref('database_cleanup.test_no_data_entry')
  66. # create a nonexistent model
  67. self.models = self.env['ir.model'].create({
  68. 'name': 'Database cleanup test model',
  69. 'model': 'x_database.cleanup.test.model',
  70. })
  71. # and a cronjob for it
  72. cronjob = self.env['ir.cron'].create({
  73. 'name': 'testcronjob',
  74. 'model_id': self.models.id,
  75. })
  76. self.env.cr.execute(
  77. 'insert into ir_attachment (name, res_model, res_id, type) values '
  78. "('test attachment', 'database.cleanup.test.model', 42, 'binary')")
  79. self.env.registry.models.pop('x_database.cleanup.test.model')
  80. purge_models = self.env['cleanup.purge.wizard.model'].create({})
  81. purge_models.purge_all()
  82. # must be removed by the wizard
  83. self.assertFalse(self.env['ir.model'].search([
  84. ('model', '=', 'x_database.cleanup.test.model'),
  85. ]))
  86. self.assertFalse(cronjob.exists())
  87. # create an orphaned table
  88. self.env.cr.execute('create table database_cleanup_test (test int)')
  89. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  90. purge_tables.purge_all()
  91. with self.assertRaises(ProgrammingError):
  92. with self.env.registry.cursor() as cr:
  93. with mute_logger('odoo.sql_db'):
  94. cr.execute('select * from database_cleanup_test')
  95. def test_database_cleanup_modules(self):
  96. @contextmanager
  97. def keep_registry():
  98. """purging a module resets the registry, so here we keep a
  99. reference to our original registry, as we don't want to run tests
  100. twice and need the original for further tests"""
  101. original_registry = Registry.registries[self.env.cr.dbname]
  102. config.options['test_enable'] = False
  103. yield
  104. config.options['test_enable'] = True
  105. # reset afterwards
  106. Registry.registries[self.env.cr.dbname] = original_registry
  107. # create nonexistent modules in different states
  108. self.modules += self.env['ir.module.module'].create({
  109. 'name': 'database_cleanup_test_to_upgrade',
  110. 'state': 'to upgrade',
  111. })
  112. self.modules += self.env['ir.module.module'].create({
  113. 'name': 'database_cleanup_test_uninstalled',
  114. 'state': 'uninstalled',
  115. })
  116. with keep_registry(), mute_logger(
  117. 'odoo.modules.graph', 'odoo.modules.loading'
  118. ):
  119. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  120. # this module should be purged already during default_get
  121. self.assertFalse(self.env['ir.module.module'].search([
  122. ('name', '=', 'database_cleanup_test_uninstalled'),
  123. ]))
  124. with keep_registry(), mute_logger(
  125. 'odoo.modules.graph', 'odoo.modules.loading'
  126. ):
  127. purge_modules.purge_all()
  128. # must be removed by the wizard
  129. self.assertFalse(self.env['ir.module.module'].search([
  130. ('name', '=', 'database_cleanup_test'),
  131. ]))
  132. def tearDown(self):
  133. super(TestDatabaseCleanup, self).tearDown()
  134. with self.registry.cursor() as cr2:
  135. # Release blocked tables with pending deletes
  136. self.env.cr.rollback()
  137. if self.modules:
  138. cr2.execute(
  139. "DELETE FROM ir_module_module WHERE id in %s",
  140. (tuple(self.modules.ids),))
  141. if self.models:
  142. cr2.execute(
  143. "DELETE FROM ir_model WHERE id in %s",
  144. (tuple(self.models.ids),))
  145. cr2.commit()