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.

131 lines
5.0 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. def default_get(self, cr, uid, fields, context=None):
  66. res = super(CleanupPurgeWizardColumn, self).default_get(
  67. cr, uid, fields, context=context)
  68. if 'name' in fields:
  69. res['name'] = _('Purge columns')
  70. return res
  71. def get_orphaned_columns(self, cr, uid, model_pool, context=None):
  72. """
  73. From openobject-server/openerp/osv/orm.py
  74. Iterate on the database columns to identify columns
  75. of fields which have been removed
  76. """
  77. columns = [
  78. c for c in model_pool._columns
  79. if not (isinstance(model_pool._columns[c], fields.function)
  80. and not model_pool._columns[c].store)]
  81. columns += orm.MAGIC_COLUMNS
  82. cr.execute("SELECT a.attname"
  83. " FROM pg_class c, pg_attribute a"
  84. " WHERE c.relname=%s"
  85. " AND c.oid=a.attrelid"
  86. " AND a.attisdropped=%s"
  87. " AND pg_catalog.format_type(a.atttypid, a.atttypmod)"
  88. " NOT IN ('cid', 'tid', 'oid', 'xid')"
  89. " AND a.attname NOT IN %s",
  90. (model_pool._table, False, tuple(columns))),
  91. return [column[0] for column in cr.fetchall()]
  92. def find(self, cr, uid, context=None):
  93. """
  94. Search for columns that are not in the corresponding model.
  95. """
  96. res = []
  97. model_pool = self.pool['ir.model']
  98. model_ids = model_pool.search(cr, uid, [], context=context)
  99. line_pool = self.pool['cleanup.purge.line.column']
  100. for model in model_pool.browse(cr, uid, model_ids, context=context):
  101. model_pool = self.pool.get(model.model)
  102. if not model_pool or not model_pool._auto:
  103. continue
  104. for column in self.get_orphaned_columns(
  105. cr, uid, model_pool, context=context):
  106. res.append((0, 0, {
  107. 'name': column,
  108. 'model_id': model.id}))
  109. if not res:
  110. raise orm.except_orm(
  111. _('Nothing to do'),
  112. _('No orphaned columns found'))
  113. return res
  114. _columns = {
  115. 'purge_line_ids': fields.one2many(
  116. 'cleanup.purge.line.column',
  117. 'wizard_id', 'Columns to purge'),
  118. }