Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG] 10.0 db cleanup #894

Merged
merged 15 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ env:
matrix:
- LINT_CHECK="1"
- TRANSIFEX="1"
- TESTS="1" ODOO_REPO="odoo/odoo"
- TESTS="1" ODOO_REPO="OCA/OCB"
- TESTS="1" ODOO_REPO="odoo/odoo" EXCLUDE="database_cleanup"
- TESTS="1" ODOO_REPO="OCA/OCB" EXCLUDE="database_cleanup"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="database_cleanup"
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="database_cleanup"

virtualenv:
system_site_packages: true
Expand Down
8 changes: 4 additions & 4 deletions database_cleanup/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
Database cleanup
================

Clean your OpenERP database from remnants of modules, models, columns and
Clean your Odoo database from remnants of modules, models, columns and
tables left by uninstalled modules (prior to 7.0) or a homebrew database
upgrade to a new major version of OpenERP.
upgrade to a new major version of Odoo.

Caution! This module is potentially harmful and can *easily* destroy the
integrity of your data. Do not use if you are not entirely comfortable
with the technical details of the OpenERP data model of *all* the modules
with the technical details of the Odoo data model of *all* the modules
that have ever been installed on your database, and do not purge any module,
model, column or table if you do not know exactly what you are doing.

Expand All @@ -28,7 +28,7 @@ in one big step (if you are *really* confident).

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/9.0
:target: https://runbot.odoo-community.org/runbot/149/10.0

Bug Tracker
===========
Expand Down
6 changes: 4 additions & 2 deletions database_cleanup/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Database cleanup',
'version': '9.0.1.0.0',
'version': '10.0.1.0.0',
'author': "Therp BV,Odoo Community Association (OCA)",
'depends': ['base'],
'license': 'AGPL-3',
Expand All @@ -16,7 +16,9 @@
'views/purge_columns.xml',
'views/purge_tables.xml',
'views/purge_data.xml',
"views/create_indexes.xml",
'views/purge_properties.xml',
'views/menu.xml',
],
'installable': False,
'installable': True,
}
134 changes: 129 additions & 5 deletions database_cleanup/i18n/am.po

Large diffs are not rendered by default.

539 changes: 539 additions & 0 deletions database_cleanup/i18n/ar.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/bg.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/bs.po

Large diffs are not rendered by default.

238 changes: 147 additions & 91 deletions database_cleanup/i18n/ca.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/cs.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/da.po

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion database_cleanup/i18n/database_cleanup.pot
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Project-Id-Version: Odoo Server 10.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-08-11 06:45+0000\n"
"PO-Revision-Date: 2015-08-11 06:45+0000\n"
Expand Down
188 changes: 156 additions & 32 deletions database_cleanup/i18n/de.po

Large diffs are not rendered by default.

134 changes: 129 additions & 5 deletions database_cleanup/i18n/el_GR.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/en_GB.po

Large diffs are not rendered by default.

214 changes: 169 additions & 45 deletions database_cleanup/i18n/es.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_AR.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_CL.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_CO.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_CR.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_DO.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_EC.po

Large diffs are not rendered by default.

138 changes: 131 additions & 7 deletions database_cleanup/i18n/es_ES.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_MX.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_PE.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_PY.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/es_VE.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/et.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/eu.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/fa.po

Large diffs are not rendered by default.

143 changes: 134 additions & 9 deletions database_cleanup/i18n/fi.po

Large diffs are not rendered by default.

138 changes: 132 additions & 6 deletions database_cleanup/i18n/fr.po

Large diffs are not rendered by default.

146 changes: 135 additions & 11 deletions database_cleanup/i18n/fr_CA.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/fr_CH.po

Large diffs are not rendered by default.

139 changes: 132 additions & 7 deletions database_cleanup/i18n/gl.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/gl_ES.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/he.po

Large diffs are not rendered by default.

149 changes: 137 additions & 12 deletions database_cleanup/i18n/hr.po

Large diffs are not rendered by default.

142 changes: 133 additions & 9 deletions database_cleanup/i18n/hr_HR.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/hu.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/id.po

Large diffs are not rendered by default.

153 changes: 139 additions & 14 deletions database_cleanup/i18n/it.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/ja.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/ko.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/lt.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/lt_LT.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/lv.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/mk.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/mn.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/nb.po

Large diffs are not rendered by default.

539 changes: 539 additions & 0 deletions database_cleanup/i18n/nb_NO.po

Large diffs are not rendered by default.

150 changes: 138 additions & 12 deletions database_cleanup/i18n/nl.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/nl_BE.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/nl_NL.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/pl.po

Large diffs are not rendered by default.

140 changes: 132 additions & 8 deletions database_cleanup/i18n/pt.po

Large diffs are not rendered by default.

139 changes: 132 additions & 7 deletions database_cleanup/i18n/pt_BR.po

Large diffs are not rendered by default.

143 changes: 134 additions & 9 deletions database_cleanup/i18n/pt_PT.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/ro.po

Large diffs are not rendered by default.

146 changes: 135 additions & 11 deletions database_cleanup/i18n/ru.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/sk.po

Large diffs are not rendered by default.

137 changes: 131 additions & 6 deletions database_cleanup/i18n/sl.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/sr.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/sr@latin.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/sv.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/th.po

Large diffs are not rendered by default.

161 changes: 143 additions & 18 deletions database_cleanup/i18n/tr.po

Large diffs are not rendered by default.

539 changes: 539 additions & 0 deletions database_cleanup/i18n/tr_TR.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/uk.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/vi.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/vi_VN.po

Large diffs are not rendered by default.

144 changes: 134 additions & 10 deletions database_cleanup/i18n/zh_CN.po

Large diffs are not rendered by default.

538 changes: 538 additions & 0 deletions database_cleanup/i18n/zh_TW.po

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions database_cleanup/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
from . import purge_tables
from . import purge_data
from . import purge_menus
from . import ir_model_fields
from . import create_indexes
from . import purge_properties
82 changes: 82 additions & 0 deletions database_cleanup/models/create_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from ..identifier_adapter import IdentifierAdapter
from odoo import api, fields, models


class CreateIndexesLine(models.TransientModel):
_inherit = 'cleanup.purge.line'
_name = 'cleanup.create_indexes.line'

purged = fields.Boolean('Created')
wizard_id = fields.Many2one('cleanup.create_indexes.wizard')
field_id = fields.Many2one('ir.model.fields', required=True)

@api.multi
def purge(self):
tables = set()
for field in self.mapped('field_id'):
model = self.env[field.model]
name = '%s_%s_index' % (model._table, field.name)
self.env.cr.execute(
'create index %s ON %s (%s)',
(
IdentifierAdapter(name, quote=False),
IdentifierAdapter(model._table),
IdentifierAdapter(field.name),
),
)
tables.add(model._table)
for table in tables:
self.env.cr.execute(
'analyze %s', (IdentifierAdapter(model._table),)
)
self.write({
'purged': True,
})


class CreateIndexesWizard(models.TransientModel):
_inherit = 'cleanup.purge.wizard'
_name = 'cleanup.create_indexes.wizard'
_description = 'Create indexes'

purge_line_ids = fields.One2many(
'cleanup.create_indexes.line', 'wizard_id',
)

@api.multi
def find(self):
res = list()
for field in self.env['ir.model.fields'].search([
('index', '=', True),
]):
if field.model not in self.env.registry:
continue
model = self.env[field.model]
name = '%s_%s_index' % (model._table, field.name)
self.env.cr.execute(
'select indexname from pg_indexes '
'where indexname=%s and tablename=%s',
(name, model._table)
)
if self.env.cr.rowcount:
continue

self.env.cr.execute(
'select a.attname '
'from pg_attribute a '
'join pg_class c on a.attrelid=c.oid '
'join pg_tables t on t.tablename=c.relname '
'where attname=%s and c.relname=%s',
(field.name, model._table,)
)
if not self.env.cr.rowcount:
continue

res.append((0, 0, {
'name': '%s.%s' % (field.model, field.name),
'field_id': field.id,
}))
return res
17 changes: 17 additions & 0 deletions database_cleanup/models/ir_model_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import api, models


class IrModelFields(models.Model):
_inherit = 'ir.model.fields'

# In case of purging it means the model does not exist anymore in
# installed module. In this specific case, we need to avoid to check
# if fields can be removed as it would fail.
@api.multi
def _prepare_update(self):
if self.env.context.get('purge'):
self -= self.filtered(lambda x: x.model not in self.env.registry)
return super(IrModelFields, self)._prepare_update()
19 changes: 11 additions & 8 deletions database_cleanup/models/purge_columns.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import _, api, fields, models
from openerp.exceptions import UserError
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from ..identifier_adapter import IdentifierAdapter


Expand All @@ -20,7 +20,12 @@ def purge(self):
"""
Unlink columns upon manual confirmation.
"""
for line in self:
if self:
objs = self
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this happens, I think something went wrong in the UI. How can I reproduce this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: If you really need this for whatever reason, just assign to self and leave the rest of the code as it is. It's just a variable like any other

objs = self.env['cleanup.purge.line.column']\
.browse(self._context.get('active_ids'))
for line in objs:
if line.purged:
continue
model_pool = self.env[line.model_id.model]
Expand Down Expand Up @@ -71,12 +76,10 @@ def get_orphaned_columns(self, model_pools):
of fields which have been removed
"""
columns = list(set([
column
column.name
for model_pool in model_pools
for column in model_pool._columns
if not (isinstance(model_pool._columns[column],
fields.fields.function) and
not model_pool._columns[column].store)
for column in model_pool._fields.values()
if not (column.compute is not None and not column.store)
]))
columns += models.MAGIC_COLUMNS
columns += self.blacklist.get(model_pools[0]._table, [])
Expand Down
13 changes: 9 additions & 4 deletions database_cleanup/models/purge_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import _, api, fields, models
from openerp.exceptions import UserError
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from ..identifier_adapter import IdentifierAdapter


Expand All @@ -17,10 +17,15 @@ class CleanupPurgeLineData(models.TransientModel):
@api.multi
def purge(self):
"""Unlink data entries upon manual confirmation."""
to_unlink = self.filtered(lambda x: not x.purged and x.data_id)
if self:
objs = self
else:
objs = self.env['cleanup.purge.line.data']\
.browse(self._context.get('active_ids'))
to_unlink = objs.filtered(lambda x: not x.purged and x.data_id)
self.logger.info('Purging data entries: %s', to_unlink.mapped('name'))
to_unlink.mapped('data_id').unlink()
return self.write({'purged': True})
return to_unlink.write({'purged': True})


class CleanupPurgeWizardData(models.TransientModel):
Expand Down
16 changes: 12 additions & 4 deletions database_cleanup/models/purge_menus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import _, api, fields, models
from openerp.exceptions import UserError
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class CleanupPurgeLineMenu(models.TransientModel):
Expand All @@ -15,8 +15,16 @@ class CleanupPurgeLineMenu(models.TransientModel):

@api.multi
def purge(self):
self.mapped('menu_id').unlink()
return self.write({'purged': True})
"""Unlink menu entries upon manual confirmation."""
if self:
objs = self
else:
objs = self.env['cleanup.purge.line.menu']\
.browse(self._context.get('active_ids'))
to_unlink = objs.filtered(lambda x: not x.purged and x.menu_id)
self.logger.info('Purging menu entries: %s', to_unlink.mapped('name'))
to_unlink.mapped('menu_id').unlink()
return to_unlink.write({'purged': True})


class CleanupPurgeWizardMenu(models.TransientModel):
Expand Down
50 changes: 27 additions & 23 deletions database_cleanup/models/purge_models.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import _, api, models, fields
from openerp.exceptions import UserError
from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
from odoo import _, api, models, fields
from odoo.exceptions import UserError
from odoo.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG


class IrModel(models.Model):
_inherit = 'ir.model'

@api.multi
def _drop_table(self):
# Allow to skip this step during model unlink
# The super method crashes if the model cannot be instantiated
if self.env.context.get('no_drop_table'):
return True
return super(IrModel, self)._drop_table()

@api.multi
def _inherited_models(self, field_name, arg):
@api.depends()
def _inherited_models(self):
"""this function crashes for undefined models"""
result = dict((i, []) for i in self.ids)
existing_model_ids = [
this.id for this in self if this.model in self.env
]
super_result = super(IrModel, self.browse(existing_model_ids))\
._inherited_models(field_name, arg)
result.update(super_result)
return result

def _register_hook(self, cr):
# patch the function field instead of overwriting it
if self._columns['inherited_model_ids']._fnct !=\
self._inherited_models.__func__:
self._columns['inherited_model_ids']._fnct =\
self._inherited_models.__func__
return super(IrModel, self)._register_hook(cr)
existing_model_ids = self.filtered(lambda x: x.model in self.env)
super(IrModel, existing_model_ids)._inherited_models()


class IrModelFields(models.Model):
_inherit = 'ir.model.fields'

@api.multi
def _prepare_update(self):
# Allow to skip this step during model unlink
# The super method crashes if the model cannot be instantiated
if self.env.context.get('no_prepare_update'):
return True
return super(IrModelFields, self)._prepare_update()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you filter self for existing models as done above? This also would make the extra context key unnecessary



class CleanupPurgeLineModel(models.TransientModel):
Expand All @@ -54,9 +51,16 @@ def purge(self):
context_flags = {
MODULE_UNINSTALL_FLAG: True,
'no_drop_table': True,
'purge': True,
'no_prepare_update': True,
}

for line in self:
if self:
objs = self
else:
objs = self.env['cleanup.purge.line.model']\
.browse(self._context.get('active_ids'))
for line in objs:
self.env.cr.execute(
"SELECT id, model from ir_model WHERE model = %s",
(line.name,))
Expand Down