@@ -0,0 +1,1960 @@
+# -*- coding: utf-8 -*-%0A#    Author: Leonardo Pistone%0A#    Copyright 2015 Camptocamp SA%0A#%0A#    This program is free software: you can redistribute it and/or modify%0A#    it under the terms of the GNU Affero General Public License as%0A#    published by the Free Software Foundation, either version 3 of the%0A#    License, or (at your option) any later version.%0A#%0A#    This program is distributed in the hope that it will be useful,%0A#    but WITHOUT ANY WARRANTY; without even the implied warranty of%0A#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A#    GNU Affero General Public License for more details.%0A#%0A#    You should have received a copy of the GNU Affero General Public License%0A#    along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A%0A%0Adef migrate(cr, installed_version):%0A    %22%22%22Add missing portfolios to agreements, create them if necessary.%22%22%22%0A    cr.execute('SELECT a.id, a.supplier_id, p.name '%0A               'FROM framework_agreement a '%0A               'JOIN res_partner p '%0A               'ON a.supplier_id = p.id '%0A               'WHERE portfolio_id is null;')%0A%0A    for agreement_id, supplier_id, supplier_name in cr.fetchall():%0A        cr.execute(%0A            'SELECT id '%0A            'FROM framework_agreement_portfolio '%0A            'WHERE supplier_id = %25s;',%0A            (supplier_id,),%0A        )%0A        portfolios = cr.fetchone()%0A        if portfolios:%0A            new_portfolio = portfolios%5B0%5D%0A        else:%0A            cr.execute('INSERT INTO framework_agreement_portfolio '%0A                       '(name, supplier_id) '%0A                       'VALUES (%25s, %25s) '%0A                       'RETURNING id; ',%0A                       (supplier_name, supplier_id))%0A            new_portfolio = cr.fetchone()%5B0%5D%0A%0A        cr.execute('UPDATE framework_agreement '%0A                   'SET portfolio_id = %25s '%0A                   'WHERE id = %25s',%0A                   (new_portfolio, agreement_id),%0A                   )%0A

add migration script to create missing portfolios