Skip to content

Commit

Permalink
[PRT] OpenUpgrade patch to Odoo core files, unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Oct 3, 2015
1 parent 52484b0 commit 3426b63
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 43 deletions.
35 changes: 31 additions & 4 deletions openerp/addons/base/ir/ir_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools.translate import _

from openerp.openupgrade import openupgrade_log, openupgrade

_logger = logging.getLogger(__name__)

MODULE_UNINSTALL_FLAG = '_force_unlink'
Expand Down Expand Up @@ -123,6 +125,11 @@ def _name_search(self, cr, uid, name='', args=None, operator='ilike', context=No

def _drop_table(self, cr, uid, ids, context=None):
for model in self.browse(cr, uid, ids, context):
# OpenUpgrade: do not run the new table cleanup
openupgrade.message(
cr, 'Unknown', False, False,
"Not dropping the table or view of model %s", model.model)
continue
model_pool = self.pool[model.model]
cr.execute('select relkind from pg_class where relname=%s', (model_pool._table,))
result = cr.fetchone()
Expand Down Expand Up @@ -388,6 +395,11 @@ def _drop_column(self, cr, uid, ids, context=None):
for field in self.browse(cr, uid, ids, context):
if field.name in MAGIC_COLUMNS:
continue
# OpenUpgrade: do not drop columns
openupgrade.message(
cr, 'Unknown', False, False,
"Not dropping the column of field %s of model %s", field.name, field.model)
continue
model = self.pool[field.model]
cr.execute('SELECT relkind FROM pg_class WHERE relname=%s', (model._table,))
result = cr.fetchone()
Expand Down Expand Up @@ -1046,6 +1058,10 @@ def unlink(self, cr, uid, ids, context=None):
return super(ir_model_data,self).unlink(cr, uid, ids, context=context)

def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False, context=None):
#OpenUpgrade: log entry (used in csv import)
if xml_id:
openupgrade_log.log_xml_id(cr, module, xml_id)

model_obj = self.pool[model]
if not context:
context = {}
Expand Down Expand Up @@ -1218,6 +1234,10 @@ def unlink_if_refcount(to_unlink):
_logger.info('Deleting orphan external_ids %s', external_ids)
self.unlink(cr, uid, external_ids)
continue
# OpenUpgrade specific start
if not self.pool.get(field.model):
continue
# OpenUpgrade specific end
if field.name in openerp.models.LOG_ACCESS_COLUMNS and self.pool[field.model]._log_access:
continue
if field.name == 'id':
Expand Down Expand Up @@ -1277,10 +1297,17 @@ def _process_end(self, cr, uid, modules):
for (id, name, model, res_id, module) in cr.fetchall():
if (module, name) not in self.loads:
if model in self.pool:
_logger.info('Deleting %s@%s (%s.%s)', res_id, model, module, name)
if self.pool[model].exists(cr, uid, [res_id], context=context):
self.pool[model].unlink(cr, uid, [res_id], context=context)
else:
_logger.info('Deleting %s@%s', res_id, model)
try:
cr.execute('SAVEPOINT ir_model_data_delete');
self.pool[model].unlink(cr, uid, [res_id])
cr.execute('RELEASE SAVEPOINT ir_model_data_delete')
except Exception:
cr.execute('ROLLBACK TO SAVEPOINT ir_model_data_delete');
_logger.warning(
'Could not delete obsolete record with id: %d of model %s\n'
'Please refer to the log message right above',
res_id, model)
bad_imd_ids.append(id)
if bad_imd_ids:
self.unlink(cr, uid, bad_imd_ids, context=context)
Expand Down
29 changes: 26 additions & 3 deletions openerp/addons/base/ir/ir_ui_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,16 @@ def _check_xml(self, cr, uid, ids, context=None):
if view.type != 'qweb':
view_doc = etree.fromstring(view_arch_utf8)
# verify that all fields used are valid, etc.
self.postprocess_and_fields(cr, uid, view.model, view_doc, view.id, context=context)
try:
self.postprocess_and_fields(cr, uid, view.model, view_doc, view.id, context=context)
except Exception:
# OpenUpgrade: We ignore all view errors as they are
# caused by views introduced by models not year loaded
_logger.warn(
"Can't render view %s for model: %s. If you are "
"migrating between major versions of OpenERP, "
"this is to be expected (otherwise, do not run "
"OpenUpgrade server).", view.xml_id, view.model)
# RNG-based validation is not possible anymore with 7.0 forms
view_docs = [view_doc]
if view_docs[0].tag == 'data':
Expand All @@ -307,9 +316,23 @@ def _check_xml(self, cr, uid, ids, context=None):
if parse_version(version) < parse_version('7.0') and validator and not validator.validate(view_arch):
for error in validator.error_log:
_logger.error(tools.ustr(error))
return False
# OpenUpgrade: We ignore all view errors as they are
# caused by views introduced by models not year loaded
_logger.warn(
"Can't render view %s for model: %s. If you are "
"migrating between major versions of OpenERP, "
"this is to be expected (otherwise, do not run "
"OpenUpgrade server).", view.xml_id, view.model)
return True
if not valid_view(view_arch):
return False
# OpenUpgrade: We ignore all view errors as they are
# caused by views introduced by models not year loaded
_logger.warn(
"Can't render view %s for model: %s. If you are "
"migrating between major versions of OpenERP, "
"this is to be expected (otherwise, do not run "
"OpenUpgrade server).", view.xml_id, view.model)
return True
return True

_sql_constraints = [
Expand Down
54 changes: 48 additions & 6 deletions openerp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,40 @@ def _field_create(self, cr, context=None):
(name_id, context['module'], 'ir.model', model_id)
)

cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
# OpenUpgrade edit start: In rare cases, an old module defined a field
# on a model that is not defined in another module earlier in the
# chain of inheritance. Then we need to assign the ir.model.fields'
# xmlid to this other module, otherwise the column would be dropped
# when uninstalling the first module.
# An example is res.partner#display_name defined in 7.0 by
# account_report_company, but now the field belongs to the base
# module
# Given that we arrive here in order of inheritance, we simply check
# if the field's xmlid belongs to a module already loaded, and if not,
# update the record with the correct module name.
cr.execute(
"SELECT f.*, d.module, d.id as xmlid_id "
"FROM ir_model_fields f LEFT JOIN ir_model_data d "
"ON f.id=d.res_id and d.model='ir.model.fields' WHERE f.model=%s",
(self._name,))
# OpenUpgrade edit end
cols = {}
for rec in cr.dictfetchall():
# OpenUpgrade start:
if 'module' in context and\
rec['module'] and\
rec['name'] in self._columns.keys() and\
rec['module'] != context.get('module') and\
rec['module'] not in self.pool._init_modules:
_logger.info(
'Moving XMLID for ir.model.fields record of %s#%s '
'from %s to %s',
self._name, rec['name'], rec['module'], context['module'])
cr.execute(
"UPDATE ir_model_data SET module=%(module)s "
"WHERE id=%(xmlid_id)s",
dict(rec, module=context['module']))
# OpenUpgrade end
cols[rec['name']] = rec

ir_model_fields_obj = self.pool.get('ir.model.fields')
Expand Down Expand Up @@ -994,6 +1025,8 @@ def log(m):

position = 0
try:
# use savepoints for openupgrade instead of transactions
cr.execute('SAVEPOINT convert_records')
for res_id, xml_id, res, info in self._convert_records(cr, uid,
self._extract_records(cr, uid, fields, datas,
context=context, log=log),
Expand All @@ -1011,8 +1044,9 @@ def log(m):
if context.get('defer_parent_store_computation'):
self._parent_store_compute(cr)
cr.commit()
cr.execute('RELEASE SAVEPOINT convert_records')
except Exception, e:
cr.rollback()
cr.execute('ROLLBACK TO SAVEPOINT convert_records')
return -1, {}, 'Line %d : %s' % (position + 1, tools.ustr(e)), ''

if context.get('defer_parent_store_computation'):
Expand Down Expand Up @@ -2576,11 +2610,14 @@ def _auto_init(self, cr, context=None):
self._set_default_value_on_column(cr, k, context=context)
# add the NOT NULL constraint
try:
# use savepoints for openupgrade instead of transactions
cr.execute('SAVEPOINT add_constraint');
cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k), log_exceptions=False)
cr.commit()
cr.execute('RELEASE SAVEPOINT add_constraint');
_schema.debug("Table '%s': column '%s': added NOT NULL constraint",
self._table, k)
except Exception:
cr.execute('ROLLBACK TO SAVEPOINT add_constraint');
msg = "Table '%s': unable to set a NOT NULL constraint on column '%s' !\n"\
"If you want to have it, you should update the records and execute manually:\n"\
"ALTER TABLE %s ALTER COLUMN %s SET NOT NULL"
Expand Down Expand Up @@ -2653,11 +2690,14 @@ def _auto_init(self, cr, context=None):
cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
if f.required:
try:
cr.commit()
#use savepoints for openupgrade instead of transactions
cr.execute('SAVEPOINT add_constraint');
cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
_schema.debug("Table '%s': column '%s': added a NOT NULL constraint",
self._table, k)
cr.execute('RELEASE SAVEPOINT add_constraint');
except Exception:
cr.execute('ROLLBACK TO SAVEPOINT add_constraint');
msg = "WARNING: unable to set column %s of table %s not null !\n"\
"Try to re-run: openerp-server --update=module\n"\
"If it doesn't work, update records and execute manually:\n"\
Expand Down Expand Up @@ -2862,12 +2902,14 @@ def unify_cons_text(txt):
sql_actions.sort(key=lambda x: x['order'])
for sql_action in [action for action in sql_actions if action['execute']]:
try:
# use savepoints for openupgrade instead of transactions
cr.execute('SAVEPOINT add_constraint2');
cr.execute(sql_action['query'])
cr.commit()
cr.execute('RELEASE SAVEPOINT add_constraint2');
_schema.debug(sql_action['msg_ok'])
except:
_schema.warning(sql_action['msg_err'])
cr.rollback()
cr.execute('ROLLBACK TO SAVEPOINT add_constraint2');


def _execute_sql(self, cr):
Expand Down
11 changes: 11 additions & 0 deletions openerp/modules/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,23 @@ def add_modules(self, cr, module_list, force=None):
force = []
packages = []
len_graph = len(self)

# force additional dependencies for the upgrade process if given
# in config file
forced_deps = tools.config.get_misc('openupgrade', 'force_deps', '{}')
forced_deps = tools.config.get_misc('openupgrade',
'force_deps_' + release.version,
forced_deps)
forced_deps = tools.safe_eval.safe_eval(forced_deps)

for module in module_list:
# This will raise an exception if no/unreadable descriptor file.
# NOTE The call to load_information_from_description_file is already
# done by db.initialize, so it is possible to not do it again here.
info = openerp.modules.module.load_information_from_description_file(module)

if info and info['installable']:
info['depends'].extend(forced_deps.get(module, []))
packages.append((module, info)) # TODO directly a dict, like in get_modules_with_version
else:
_logger.warning('module %s: not installable, skipped', module)
Expand Down
Loading

0 comments on commit 3426b63

Please sign in to comment.