-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
03b191f
ef8780c
8eb30f0
287f779
e77d9da
3531dce
460771c
145329f
bf40673
55fd3d5
54141e5
95e0b9c
b466c0c
88a5bbf
02a9225
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
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 |
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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