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.

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