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.

127 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014-2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import _, api, fields, models
  5. from openerp.exceptions import UserError
  6. from ..identifier_adapter import IdentifierAdapter
  7. class CleanupPurgeLineColumn(models.TransientModel):
  8. _inherit = 'cleanup.purge.line'
  9. _name = 'cleanup.purge.line.column'
  10. model_id = fields.Many2one('ir.model', 'Model', required=True,
  11. ondelete='CASCADE')
  12. wizard_id = fields.Many2one(
  13. 'cleanup.purge.wizard.column', 'Purge Wizard', readonly=True)
  14. @api.multi
  15. def purge(self):
  16. """
  17. Unlink columns upon manual confirmation.
  18. """
  19. for line in self:
  20. if line.purged:
  21. continue
  22. model_pool = self.env[line.model_id.model]
  23. # Check whether the column actually still exists.
  24. # Inheritance such as stock.picking.in from stock.picking
  25. # can lead to double attempts at removal
  26. self.env.cr.execute(
  27. 'SELECT count(attname) FROM pg_attribute '
  28. 'WHERE attrelid = '
  29. '( SELECT oid FROM pg_class WHERE relname = %s ) '
  30. 'AND attname = %s',
  31. (model_pool._table, line.name))
  32. if not self.env.cr.fetchone()[0]:
  33. continue
  34. self.logger.info(
  35. 'Dropping column %s from table %s',
  36. line.name, model_pool._table)
  37. self.env.cr.execute(
  38. 'ALTER TABLE %s DROP COLUMN %s',
  39. (
  40. IdentifierAdapter(model_pool._table),
  41. IdentifierAdapter(line.name)
  42. ))
  43. line.write({'purged': True})
  44. # we need this commit because the ORM will deadlock if
  45. # we still have a pending transaction
  46. self.env.cr.commit() # pylint: disable=invalid-commit
  47. return True
  48. class CleanupPurgeWizardColumn(models.TransientModel):
  49. _inherit = 'cleanup.purge.wizard'
  50. _name = 'cleanup.purge.wizard.column'
  51. _description = 'Purge columns'
  52. # List of known columns in use without corresponding fields
  53. # Format: {table: [fields]}
  54. blacklist = {
  55. 'wkf_instance': ['uid'], # lp:1277899
  56. }
  57. @api.model
  58. def get_orphaned_columns(self, model_pools):
  59. """
  60. From openobject-server/openerp/osv/orm.py
  61. Iterate on the database columns to identify columns
  62. of fields which have been removed
  63. """
  64. columns = list(set([
  65. column
  66. for model_pool in model_pools
  67. for column in model_pool._columns
  68. if not (isinstance(model_pool._columns[column],
  69. fields.fields.function) and
  70. not model_pool._columns[column].store)
  71. ]))
  72. columns += models.MAGIC_COLUMNS
  73. columns += self.blacklist.get(model_pools[0]._table, [])
  74. self.env.cr.execute(
  75. "SELECT a.attname FROM pg_class c, pg_attribute a "
  76. "WHERE c.relname=%s AND c.oid=a.attrelid AND a.attisdropped=False "
  77. "AND pg_catalog.format_type(a.atttypid, a.atttypmod) "
  78. "NOT IN ('cid', 'tid', 'oid', 'xid') "
  79. "AND a.attname NOT IN %s",
  80. (model_pools[0]._table, tuple(columns)))
  81. return [column for column, in self.env.cr.fetchall()]
  82. @api.model
  83. def find(self):
  84. """
  85. Search for columns that are not in the corresponding model.
  86. Group models by table to prevent false positives for columns
  87. that are only in some of the models sharing the same table.
  88. Example of this is 'sale_id' not being a field of stock.picking.in
  89. """
  90. res = []
  91. # mapping of tables to tuples (model id, [pool1, pool2, ...])
  92. table2model = {}
  93. for model in self.env['ir.model'].search([]):
  94. if model.model not in self.env:
  95. continue
  96. model_pool = self.env[model.model]
  97. if not model_pool._auto:
  98. continue
  99. table2model.setdefault(
  100. model_pool._table, (model.id, [])
  101. )[1].append(model_pool)
  102. for table, model_spec in table2model.iteritems():
  103. for column in self.get_orphaned_columns(model_spec[1]):
  104. res.append((0, 0, {
  105. 'name': column,
  106. 'model_id': model_spec[0]}))
  107. if not res:
  108. raise UserError(_('No orphaned columns found'))
  109. return res
  110. purge_line_ids = fields.One2many(
  111. 'cleanup.purge.line.column', 'wizard_id', 'Columns to purge')