[IMP] database_cleanup : delete also obsolete SQL views
Co-authored-by: Pedro M. Baeza <pedro.baeza@tecnativa.com>pull/2951/head
parent
3c0f183c56
commit
4b9c6d246d
|
@ -2,11 +2,18 @@
|
||||||
# Copyright 2021 Camptocamp <https://camptocamp.com>
|
# Copyright 2021 Camptocamp <https://camptocamp.com>
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
# pylint: disable=consider-merging-classes-inherited
|
# pylint: disable=consider-merging-classes-inherited
|
||||||
|
from psycopg2.extensions import AsIs
|
||||||
|
|
||||||
from odoo import _, api, fields, models
|
from odoo import _, api, fields, models
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
|
||||||
from ..identifier_adapter import IdentifierAdapter
|
from ..identifier_adapter import IdentifierAdapter
|
||||||
|
|
||||||
|
_TABLE_TYPE_SELECTION = [
|
||||||
|
("base", "SQL Table"),
|
||||||
|
("view", "SQL View"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class CleanupPurgeLineTable(models.TransientModel):
|
class CleanupPurgeLineTable(models.TransientModel):
|
||||||
_inherit = "cleanup.purge.line"
|
_inherit = "cleanup.purge.line"
|
||||||
|
@ -16,6 +23,7 @@ class CleanupPurgeLineTable(models.TransientModel):
|
||||||
wizard_id = fields.Many2one(
|
wizard_id = fields.Many2one(
|
||||||
"cleanup.purge.wizard.table", "Purge Wizard", readonly=True
|
"cleanup.purge.wizard.table", "Purge Wizard", readonly=True
|
||||||
)
|
)
|
||||||
|
table_type = fields.Selection(selection=_TABLE_TYPE_SELECTION)
|
||||||
|
|
||||||
def purge(self):
|
def purge(self):
|
||||||
"""
|
"""
|
||||||
|
@ -71,8 +79,14 @@ class CleanupPurgeLineTable(models.TransientModel):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
self.logger.info("Dropping table %s", line.name)
|
if line.table_type == "base":
|
||||||
self.env.cr.execute("DROP TABLE %s", (IdentifierAdapter(line.name),))
|
_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})
|
line.write({"purged": True})
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -85,8 +99,7 @@ class CleanupPurgeWizardTable(models.TransientModel):
|
||||||
@api.model
|
@api.model
|
||||||
def find(self):
|
def find(self):
|
||||||
"""
|
"""
|
||||||
Search for tables that cannot be instantiated.
|
Search for tables and views that cannot be instantiated.
|
||||||
Ignore views for now.
|
|
||||||
"""
|
"""
|
||||||
known_tables = []
|
known_tables = []
|
||||||
for model in self.env["ir.model"].search([]):
|
for model in self.env["ir.model"].search([]):
|
||||||
|
@ -104,13 +117,21 @@ class CleanupPurgeWizardTable(models.TransientModel):
|
||||||
|
|
||||||
self.env.cr.execute(
|
self.env.cr.execute(
|
||||||
"""
|
"""
|
||||||
SELECT table_name FROM information_schema.tables
|
SELECT table_name, table_type FROM information_schema.tables
|
||||||
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
|
WHERE table_schema = 'public'
|
||||||
|
AND table_type in ('BASE TABLE', 'VIEW')
|
||||||
AND table_name NOT IN %s""",
|
AND table_name NOT IN %s""",
|
||||||
(tuple(known_tables),),
|
(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:
|
if not res:
|
||||||
raise UserError(_("No orphaned tables found"))
|
raise UserError(_("No orphaned tables found"))
|
||||||
return res
|
return res
|
||||||
|
|
|
@ -27,7 +27,9 @@
|
||||||
<field name="inherit_id" ref="tree_purge_line" />
|
<field name="inherit_id" ref="tree_purge_line" />
|
||||||
<field name="mode">primary</field>
|
<field name="mode">primary</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<data />
|
<field name="name" position="after">
|
||||||
|
<field name="table_type" />
|
||||||
|
</field>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue