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.

155 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],
  86. fields.function) and
  87. not model_pool._columns[column].store)
  88. ]))
  89. columns += orm.MAGIC_COLUMNS
  90. columns += self.blacklist.get(model_pools[0]._table, [])
  91. cr.execute("SELECT a.attname"
  92. " FROM pg_class c, pg_attribute a"
  93. " WHERE c.relname=%s"
  94. " AND c.oid=a.attrelid"
  95. " AND a.attisdropped=%s"
  96. " AND pg_catalog.format_type(a.atttypid, a.atttypmod)"
  97. " NOT IN ('cid', 'tid', 'oid', 'xid')"
  98. " AND a.attname NOT IN %s",
  99. (model_pools[0]._table, False, tuple(columns))),
  100. return [column[0] for column in cr.fetchall()]
  101. def find(self, cr, uid, context=None):
  102. """
  103. Search for columns that are not in the corresponding model.
  104. Group models by table to prevent false positives for columns
  105. that are only in some of the models sharing the same table.
  106. Example of this is 'sale_id' not being a field of stock.picking.in
  107. """
  108. res = []
  109. model_pool = self.pool['ir.model']
  110. model_ids = model_pool.search(cr, uid, [], context=context)
  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(
  118. model_pool._table, (model.id, []))[1].append(model_pool)
  119. for table, model_spec in table2model.iteritems():
  120. for column in self.get_orphaned_columns(
  121. cr, uid, model_spec[1], context=context):
  122. res.append((0, 0, {
  123. 'name': column,
  124. 'model_id': model_spec[0]}))
  125. if not res:
  126. raise orm.except_orm(
  127. _('Nothing to do'),
  128. _('No orphaned columns found'))
  129. return res
  130. _columns = {
  131. 'purge_line_ids': fields.one2many(
  132. 'cleanup.purge.line.column',
  133. 'wizard_id', 'Columns to purge'),
  134. }