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.

110 lines
4.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
  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 odoo import api, fields, models, _
  5. from odoo.exceptions import UserError
  6. from ..identifier_adapter import IdentifierAdapter
  7. class CleanupPurgeLineTable(models.TransientModel):
  8. _inherit = 'cleanup.purge.line'
  9. _name = 'cleanup.purge.line.table'
  10. wizard_id = fields.Many2one(
  11. 'cleanup.purge.wizard.table', 'Purge Wizard', readonly=True)
  12. @api.multi
  13. def purge(self):
  14. """
  15. Unlink tables upon manual confirmation.
  16. """
  17. if self:
  18. objs = self
  19. else:
  20. objs = self.env['cleanup.purge.line.table']\
  21. .browse(self._context.get('active_ids'))
  22. tables = objs.mapped('name')
  23. for line in objs:
  24. if line.purged:
  25. continue
  26. # Retrieve constraints on the tables to be dropped
  27. # This query is referenced in numerous places
  28. # on the Internet but credits probably go to Tom Lane
  29. # in this post http://www.postgresql.org/\
  30. # message-id/22895.1226088573@sss.pgh.pa.us
  31. # Only using the constraint name and the source table,
  32. # but I'm leaving the rest in for easier debugging
  33. self.env.cr.execute(
  34. """
  35. SELECT conname, confrelid::regclass, af.attname AS fcol,
  36. conrelid::regclass, a.attname AS col
  37. FROM pg_attribute af, pg_attribute a,
  38. (SELECT conname, conrelid, confrelid,conkey[i] AS conkey,
  39. confkey[i] AS confkey
  40. FROM (select conname, conrelid, confrelid, conkey,
  41. confkey, generate_series(1,array_upper(conkey,1)) AS i
  42. FROM pg_constraint WHERE contype = 'f') ss) ss2
  43. WHERE af.attnum = confkey AND af.attrelid = confrelid AND
  44. a.attnum = conkey AND a.attrelid = conrelid
  45. AND confrelid::regclass = '%s'::regclass;
  46. """, (IdentifierAdapter(line.name, quote=False),))
  47. for constraint in self.env.cr.fetchall():
  48. if constraint[3] in tables:
  49. self.logger.info(
  50. 'Dropping constraint %s on table %s (to be dropped)',
  51. constraint[0], constraint[3])
  52. self.env.cr.execute(
  53. "ALTER TABLE %s DROP CONSTRAINT %s",
  54. (
  55. IdentifierAdapter(constraint[3]),
  56. IdentifierAdapter(constraint[0])
  57. ))
  58. self.logger.info(
  59. 'Dropping table %s', line.name)
  60. self.env.cr.execute(
  61. "DROP TABLE %s", (IdentifierAdapter(line.name),))
  62. line.write({'purged': True})
  63. return True
  64. class CleanupPurgeWizardTable(models.TransientModel):
  65. _inherit = 'cleanup.purge.wizard'
  66. _name = 'cleanup.purge.wizard.table'
  67. _description = 'Purge tables'
  68. @api.model
  69. def find(self):
  70. """
  71. Search for tables that cannot be instantiated.
  72. Ignore views for now.
  73. """
  74. # Start out with known tables with no model
  75. known_tables = ['wkf_witm_trans']
  76. for model in self.env['ir.model'].search([]):
  77. if model.model not in self.env:
  78. continue
  79. model_pool = self.env[model.model]
  80. known_tables.append(model_pool._table)
  81. known_tables += [
  82. column.relation
  83. for column in model_pool._fields.values()
  84. if column.type == 'many2many' and
  85. (column.compute is None or column.store)
  86. ]
  87. self.env.cr.execute(
  88. """
  89. SELECT table_name FROM information_schema.tables
  90. WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
  91. AND table_name NOT IN %s""", (tuple(known_tables),))
  92. res = [(0, 0, {'name': row[0]}) for row in self.env.cr.fetchall()]
  93. if not res:
  94. raise UserError(_('No orphaned tables found'))
  95. return res
  96. purge_line_ids = fields.One2many(
  97. 'cleanup.purge.line.table', 'wizard_id', 'Tables to purge')