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.

153 lines
5.8 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. from openerp.tools.translate import _
  23. class CleanupPurgeLineColumn(orm.TransientModel):
  24. _inherit = 'cleanup.purge.line'
  25. _name = 'cleanup.purge.line.column'
  26. _columns = {
  27. 'model_id': fields.many2one(
  28. 'ir.model', 'Model',
  29. required=True, ondelete='CASCADE'),
  30. 'wizard_id': fields.many2one(
  31. 'cleanup.purge.wizard.column', 'Purge Wizard', readonly=True),
  32. }
  33. def purge(self, cr, uid, ids, context=None):
  34. """
  35. Unlink columns upon manual confirmation.
  36. """
  37. for line in self.browse(cr, uid, ids, context=context):
  38. if line.purged:
  39. continue
  40. model_pool = self.pool[line.model_id.model]
  41. # Check whether the column actually still exists.
  42. # Inheritance such as stock.picking.in from stock.picking
  43. # can lead to double attempts at removal
  44. cr.execute(
  45. 'SELECT count(attname) FROM pg_attribute '
  46. 'WHERE attrelid = '
  47. '( SELECT oid FROM pg_class WHERE relname = %s ) '
  48. 'AND attname = %s',
  49. (model_pool._table, line.name));
  50. if not cr.fetchone()[0]:
  51. continue
  52. self.logger.info(
  53. 'Dropping column %s from table %s',
  54. line.name, model_pool._table)
  55. cr.execute(
  56. """
  57. ALTER TABLE "%s" DROP COLUMN "%s"
  58. """ % (model_pool._table, line.name))
  59. line.write({'purged': True})
  60. cr.commit()
  61. return True
  62. class CleanupPurgeWizardColumn(orm.TransientModel):
  63. _inherit = 'cleanup.purge.wizard'
  64. _name = 'cleanup.purge.wizard.column'
  65. # List of known columns in use without corresponding fields
  66. # Format: {table: [fields]}
  67. blacklist = {
  68. 'wkf_instance': ['uid'], # lp:1277899
  69. }
  70. def default_get(self, cr, uid, fields, context=None):
  71. res = super(CleanupPurgeWizardColumn, self).default_get(
  72. cr, uid, fields, context=context)
  73. if 'name' in fields:
  74. res['name'] = _('Purge columns')
  75. return res
  76. def get_orphaned_columns(self, cr, uid, model_pools, context=None):
  77. """
  78. From openobject-server/openerp/osv/orm.py
  79. Iterate on the database columns to identify columns
  80. of fields which have been removed
  81. """
  82. columns = list(set([
  83. column for model_pool in model_pools
  84. for column in model_pool._columns
  85. if not (isinstance(model_pool._columns[column], fields.function)
  86. and not model_pool._columns[column].store)
  87. ]))
  88. columns += orm.MAGIC_COLUMNS
  89. columns += self.blacklist.get(model_pools[0]._table, [])
  90. cr.execute("SELECT a.attname"
  91. " FROM pg_class c, pg_attribute a"
  92. " WHERE c.relname=%s"
  93. " AND c.oid=a.attrelid"
  94. " AND a.attisdropped=%s"
  95. " AND pg_catalog.format_type(a.atttypid, a.atttypmod)"
  96. " NOT IN ('cid', 'tid', 'oid', 'xid')"
  97. " AND a.attname NOT IN %s",
  98. (model_pools[0]._table, False, tuple(columns))),
  99. return [column[0] for column in cr.fetchall()]
  100. def find(self, cr, uid, context=None):
  101. """
  102. Search for columns that are not in the corresponding model.
  103. Group models by table to prevent false positives for columns
  104. that are only in some of the models sharing the same table.
  105. Example of this is 'sale_id' not being a field of stock.picking.in
  106. """
  107. res = []
  108. model_pool = self.pool['ir.model']
  109. model_ids = model_pool.search(cr, uid, [], context=context)
  110. line_pool = self.pool['cleanup.purge.line.column']
  111. # mapping of tables to tuples (model id, [pool1, pool2, ...])
  112. table2model = {}
  113. for model in model_pool.browse(cr, uid, model_ids, context=context):
  114. model_pool = self.pool.get(model.model)
  115. if not model_pool or not model_pool._auto:
  116. continue
  117. table2model.setdefault(model_pool._table, (model.id, []))[1].append(model_pool)
  118. for table, model_spec in table2model.iteritems():
  119. for column in self.get_orphaned_columns(
  120. cr, uid, model_spec[1], context=context):
  121. res.append((0, 0, {
  122. 'name': column,
  123. 'model_id': model_spec[0]}))
  124. if not res:
  125. raise orm.except_orm(
  126. _('Nothing to do'),
  127. _('No orphaned columns found'))
  128. return res
  129. _columns = {
  130. 'purge_line_ids': fields.one2many(
  131. 'cleanup.purge.line.column',
  132. 'wizard_id', 'Columns to purge'),
  133. }