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.

139 lines
5.2 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_pool, 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 = [
  83. c for c in model_pool._columns
  84. if not (isinstance(model_pool._columns[c], fields.function)
  85. and not model_pool._columns[c].store)]
  86. columns += orm.MAGIC_COLUMNS
  87. columns += self.blacklist.get(model_pool._table, [])
  88. cr.execute("SELECT a.attname"
  89. " FROM pg_class c, pg_attribute a"
  90. " WHERE c.relname=%s"
  91. " AND c.oid=a.attrelid"
  92. " AND a.attisdropped=%s"
  93. " AND pg_catalog.format_type(a.atttypid, a.atttypmod)"
  94. " NOT IN ('cid', 'tid', 'oid', 'xid')"
  95. " AND a.attname NOT IN %s",
  96. (model_pool._table, False, tuple(columns))),
  97. return [column[0] for column in cr.fetchall()]
  98. def find(self, cr, uid, context=None):
  99. """
  100. Search for columns that are not in the corresponding model.
  101. """
  102. res = []
  103. model_pool = self.pool['ir.model']
  104. model_ids = model_pool.search(cr, uid, [], context=context)
  105. line_pool = self.pool['cleanup.purge.line.column']
  106. for model in model_pool.browse(cr, uid, model_ids, context=context):
  107. model_pool = self.pool.get(model.model)
  108. if not model_pool or not model_pool._auto:
  109. continue
  110. for column in self.get_orphaned_columns(
  111. cr, uid, model_pool, context=context):
  112. res.append((0, 0, {
  113. 'name': column,
  114. 'model_id': model.id}))
  115. if not res:
  116. raise orm.except_orm(
  117. _('Nothing to do'),
  118. _('No orphaned columns found'))
  119. return res
  120. _columns = {
  121. 'purge_line_ids': fields.one2many(
  122. 'cleanup.purge.line.column',
  123. 'wizard_id', 'Columns to purge'),
  124. }