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.

102 lines
4.0 KiB

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