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.

82 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2017 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from ..identifier_adapter import IdentifierAdapter
  5. from odoo import api, fields, models
  6. class CreateIndexesLine(models.TransientModel):
  7. _inherit = 'cleanup.purge.line'
  8. _name = 'cleanup.create_indexes.line'
  9. purged = fields.Boolean('Created')
  10. wizard_id = fields.Many2one('cleanup.create_indexes.wizard')
  11. field_id = fields.Many2one('ir.model.fields', required=True)
  12. @api.multi
  13. def purge(self):
  14. tables = set()
  15. for field in self.mapped('field_id'):
  16. model = self.env[field.model]
  17. name = '%s_%s_index' % (model._table, field.name)
  18. self.env.cr.execute(
  19. 'create index %s ON %s (%s)',
  20. (
  21. IdentifierAdapter(name, quote=False),
  22. IdentifierAdapter(model._table),
  23. IdentifierAdapter(field.name),
  24. ),
  25. )
  26. tables.add(model._table)
  27. for table in tables:
  28. self.env.cr.execute(
  29. 'analyze %s', (IdentifierAdapter(model._table),)
  30. )
  31. self.write({
  32. 'purged': True,
  33. })
  34. class CreateIndexesWizard(models.TransientModel):
  35. _inherit = 'cleanup.purge.wizard'
  36. _name = 'cleanup.create_indexes.wizard'
  37. _description = 'Create indexes'
  38. purge_line_ids = fields.One2many(
  39. 'cleanup.create_indexes.line', 'wizard_id',
  40. )
  41. @api.multi
  42. def find(self):
  43. res = list()
  44. for field in self.env['ir.model.fields'].search([
  45. ('index', '=', True),
  46. ]):
  47. if field.model not in self.env.registry:
  48. continue
  49. model = self.env[field.model]
  50. name = '%s_%s_index' % (model._table, field.name)
  51. self.env.cr.execute(
  52. 'select indexname from pg_indexes '
  53. 'where indexname=%s and tablename=%s',
  54. (name, model._table)
  55. )
  56. if self.env.cr.rowcount:
  57. continue
  58. self.env.cr.execute(
  59. 'select a.attname '
  60. 'from pg_attribute a '
  61. 'join pg_class c on a.attrelid=c.oid '
  62. 'join pg_tables t on t.tablename=c.relname '
  63. 'where attname=%s and c.relname=%s',
  64. (field.name, model._table,)
  65. )
  66. if not self.env.cr.rowcount:
  67. continue
  68. res.append((0, 0, {
  69. 'name': '%s.%s' % (field.model, field.name),
  70. 'field_id': field.id,
  71. }))
  72. return res