From 74dc8715ef0b641ab819afe43d41bfb45c0fe45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 22 Sep 2015 19:54:14 +0200 Subject: [PATCH] Fix cost_price migration as it has a default value of 0 issue4977 --- timesheet.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/timesheet.py b/timesheet.py index 9c8af79acb..d54f32605b 100644 --- a/timesheet.py +++ b/timesheet.py @@ -5,6 +5,7 @@ from sql import Null from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta from trytond.model import fields +from trytond import backend from .company import price_digits @@ -22,24 +23,29 @@ class TimesheetLine: def __register__(cls, module_name): pool = Pool() Employee = pool.get('company.employee') + TableHandler = backend.get('TableHandler') cursor = Transaction().cursor table = cls.__table__() + table_h = TableHandler(cursor, cls, module_name) + + migrate_cost_price = not table_h.column_exist('cost_price') super(TimesheetLine, cls).__register__(module_name) # Migration from 3.6: add cost_price - cursor.execute(*table.select(table.id, table.employee, table.date, - where=(table.cost_price == Null) - & (table.employee != Null) - & (table.date != Null))) - for line_id, employee_id, date in cursor.fetchall(): - employee = Employee(employee_id) - cost_price = employee.compute_cost_price(date=date) - cursor.execute(*table.update( - [table.cost_price], - [cost_price], - where=table.id == line_id)) + if migrate_cost_price: + cursor.execute(*table.select(table.id, table.employee, table.date, + where=(table.cost_price == 0) + & (table.employee != Null) + & (table.date != Null))) + for line_id, employee_id, date in cursor.fetchall(): + employee = Employee(employee_id) + cost_price = employee.compute_cost_price(date=date) + cursor.execute(*table.update( + [table.cost_price], + [cost_price], + where=table.id == line_id)) @classmethod def default_cost_price(cls):