diff --git a/database_cleanup/models/purge_tables.py b/database_cleanup/models/purge_tables.py index cde69fd87..69121e67e 100644 --- a/database_cleanup/models/purge_tables.py +++ b/database_cleanup/models/purge_tables.py @@ -2,11 +2,18 @@ # Copyright 2021 Camptocamp # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # pylint: disable=consider-merging-classes-inherited +from psycopg2.extensions import AsIs + from odoo import _, api, fields, models from odoo.exceptions import UserError from ..identifier_adapter import IdentifierAdapter +_TABLE_TYPE_SELECTION = [ + ("base", "SQL Table"), + ("view", "SQL View"), +] + class CleanupPurgeLineTable(models.TransientModel): _inherit = "cleanup.purge.line" @@ -16,6 +23,7 @@ class CleanupPurgeLineTable(models.TransientModel): wizard_id = fields.Many2one( "cleanup.purge.wizard.table", "Purge Wizard", readonly=True ) + table_type = fields.Selection(selection=_TABLE_TYPE_SELECTION) def purge(self): """ @@ -71,8 +79,14 @@ class CleanupPurgeLineTable(models.TransientModel): ), ) - self.logger.info("Dropping table %s", line.name) - self.env.cr.execute("DROP TABLE %s", (IdentifierAdapter(line.name),)) + if line.table_type == "base": + _sql_type = "TABLE" + elif line.table_type == "view": + _sql_type = "VIEW" + self.logger.info("Dropping %s %s", (_sql_type, line.name)) + self.env.cr.execute( + "DROP %s %s", (AsIs(_sql_type), IdentifierAdapter(line.name)) + ) line.write({"purged": True}) return True @@ -85,8 +99,7 @@ class CleanupPurgeWizardTable(models.TransientModel): @api.model def find(self): """ - Search for tables that cannot be instantiated. - Ignore views for now. + Search for tables and views that cannot be instantiated. """ known_tables = [] for model in self.env["ir.model"].search([]): @@ -104,13 +117,21 @@ class CleanupPurgeWizardTable(models.TransientModel): self.env.cr.execute( """ - SELECT table_name FROM information_schema.tables - WHERE table_schema = 'public' AND table_type = 'BASE TABLE' + SELECT table_name, table_type FROM information_schema.tables + WHERE table_schema = 'public' + AND table_type in ('BASE TABLE', 'VIEW') AND table_name NOT IN %s""", (tuple(known_tables),), ) - res = [(0, 0, {"name": row[0]}) for row in self.env.cr.fetchall()] + res = [ + ( + 0, + 0, + {"name": row[0], "table_type": "view" if row[1] == "VIEW" else "base"}, + ) + for row in self.env.cr.fetchall() + ] if not res: raise UserError(_("No orphaned tables found")) return res diff --git a/database_cleanup/views/purge_tables.xml b/database_cleanup/views/purge_tables.xml index f8f3d0d14..6d3c034bc 100644 --- a/database_cleanup/views/purge_tables.xml +++ b/database_cleanup/views/purge_tables.xml @@ -27,7 +27,9 @@ primary - + + +