@@ -173,10 +173,8 @@ def migration_invoice_moves(env):
173173 ai.create_uid, ai.create_date, ai.write_uid, ai.write_date)
174174 FROM account_invoice ai
175175 WHERE am.id = ai.move_id AND ai.state not in ('draft', 'cancel')
176- RETURNING ai.id, am.id
177176 """ ,
178177 )
179- ids1 = env .cr .fetchall ()
180178 # Insert moves for draft or canceled invoices
181179 openupgrade .logged_query (
182180 env .cr , """
@@ -215,13 +213,10 @@ def migration_invoice_moves(env):
215213 vendor_display_name, cash_rounding_id, id, create_uid, create_date,
216214 write_uid, write_date
217215 FROM account_invoice ai
218- WHERE ai.state in ('draft', 'cancel')
219- RETURNING old_invoice_id, id""" ,
216+ WHERE ai.state in ('draft', 'cancel')""" ,
220217 )
221- ids2 = env .cr .fetchall ()
222- if ids1 or ids2 :
223- _move_model_in_data (
224- env , ids1 + ids2 , 'account.invoice' , 'account.move' )
218+ _move_model_in_data (
219+ env , 'account.invoice' , 'account.move' , 'old_invoice_id' )
225220 # Not Draft or Cancel Invoice Lines
226221 # 1st: update the ungrouped ones
227222 openupgrade .logged_query (env .cr , "ALTER TABLE account_invoice_line ADD aml_matched BOOLEAN" )
@@ -421,10 +416,11 @@ def migration_invoice_moves(env):
421416 )
422417 openupgrade .logged_query (
423418 env .cr , """
424- UPDATE account_invoice_payment_rel aipr
425- SET invoice_id = am.id
426- FROM account_move am
427- WHERE am.old_invoice_id = aipr.invoice_id
419+ INSERT INTO account_invoice_payment_rel
420+ (invoice_id, payment_id)
421+ SELECT am.id, aipr.payment_id
422+ FROM old_account_invoice_payment_rel as aipr
423+ INNER JOIN account_move am ON am.old_invoice_id = aipr.invoice_id
428424 """ ,
429425 )
430426 # Relink analytic tags
@@ -509,10 +505,8 @@ def migration_voucher_moves(env):
509505 FROM account_voucher av
510506 WHERE am.old_voucher_id = av.id AND av.state not in (
511507 'draft', 'cancel', 'proforma')
512- RETURNING am.old_voucher_id, am.id
513508 """ ,
514509 )
515- ids1 = env .cr .fetchall ()
516510 openupgrade .logged_query (
517511 env .cr , """
518512 INSERT INTO account_move (message_main_attachment_id, name, date,
@@ -529,12 +523,10 @@ def migration_voucher_moves(env):
529523 write_date
530524 FROM account_voucher av
531525 WHERE av.state in ('draft', 'cancel', 'proforma')
532- RETURNING old_voucher_id, id """ ,
526+ """ ,
533527 )
534- ids2 = env .cr .fetchall ()
535- if ids1 or ids2 :
536- _move_model_in_data (
537- env , ids1 + ids2 , 'account.voucher' , 'account.move' )
528+ _move_model_in_data (
529+ env , 'account.voucher' , 'account.move' , 'old_voucher_id' )
538530 # Not draft, cancel, proforma voucher lines
539531 openupgrade .logged_query (
540532 env .cr , """
@@ -605,35 +597,45 @@ def migration_voucher_moves(env):
605597 )
606598
607599
608- def _move_model_in_data (env , ids_map , old_model , new_model ):
600+ def _move_model_in_data (env , old_model , new_model , field ):
609601 renames = [
610602 ('mail_message' , 'model' , 'res_id' ),
611- ('mail_followers' , 'res_model' , 'res_id' ),
612603 ('ir_attachment' , 'res_model' , 'res_id' ),
613604 ('mail_activity' , 'res_model' , 'res_id' ),
614605 ('ir_model_data' , 'model' , 'res_id' ),
615606 ]
616- for old_id , new_id in ids_map :
617- for rename in renames :
618- query = """
619- UPDATE {table}
620- SET {field1} = %(new_value1)s, {field2} = %(new_value2)s
621- WHERE {field1} = %(old_value1)s AND {field2} = %(old_value2)s"""
622- if rename [0 ] == 'mail_followers' :
623- query += """ AND partner_id NOT IN (
624- SELECT partner_id FROM mail_followers
625- WHERE res_model = 'account.move' AND res_id = %(new_value2)s
626- )"""
627- env .cr .execute (sql .SQL (query ).format (
628- table = sql .Identifier (rename [0 ]),
629- field1 = sql .Identifier (rename [1 ]),
630- field2 = sql .Identifier (rename [2 ])
631- ), {
632- "old_value1" : old_model ,
633- "new_value1" : new_model ,
634- "old_value2" : old_id ,
635- "new_value2" : new_id ,
636- })
607+ for rename in renames :
608+ query = """
609+ UPDATE {table} t
610+ SET {field1} = %(new_value1)s, {field2} = am.id
611+ FROM account_move am
612+ WHERE t.{field1} = %(old_value1)s AND am.{field} = t.{field2}"""
613+ openupgrade .logged_query (env .cr , sql .SQL (query ).format (
614+ table = sql .Identifier (rename [0 ]),
615+ field1 = sql .Identifier (rename [1 ]),
616+ field2 = sql .Identifier (rename [2 ]),
617+ field = sql .Identifier (field )
618+ ), {
619+ "old_value1" : old_model ,
620+ "new_value1" : new_model ,
621+ })
622+ openupgrade .logged_query (env .cr , sql .SQL ("""
623+ UPDATE mail_followers mf
624+ SET res_model = %(new_value1)s, res_id = am.id
625+ FROM account_move am
626+ JOIN mail_followers mf1
627+ ON (am.{field} = mf1.res_id AND mf1.res_model = %(old_value1)s)
628+ LEFT JOIN mail_followers mf2
629+ ON (am.id = mf2.res_id
630+ AND mf2.res_model = 'account.move'
631+ AND mf2.partner_id = mf1.partner_id)
632+ WHERE mf.id = mf1.id AND mf2.id IS NULL
633+ """ ).format (
634+ field = sql .Identifier (field )
635+ ), {
636+ "old_value1" : old_model ,
637+ "new_value1" : new_model ,
638+ })
637639
638640
639641def fill_account_move_reversed_entry_id (env ):
@@ -1036,6 +1038,21 @@ def _recompute_move_entries_totals(env):
10361038 )
10371039
10381040
1041+ def fill_account_move_line_missing_fields (env ):
1042+ openupgrade .logged_query (env .cr , """
1043+ UPDATE account_move_line aml
1044+ SET account_root_id = aa.root_id
1045+ FROM account_account aa
1046+ WHERE aa.id = aml.account_id
1047+ """ )
1048+ openupgrade .logged_query (env .cr , """
1049+ UPDATE account_move_line aml
1050+ SET tax_group_id = at.tax_group_id
1051+ FROM account_tax at
1052+ WHERE at.id = aml.tax_line_id
1053+ """ )
1054+
1055+
10391056@openupgrade .migrate ()
10401057def migrate (env , version ):
10411058 fill_account_reconcile_model_second_analytic_tag_rel_table (env )
@@ -1047,6 +1064,7 @@ def migrate(env, version):
10471064 fill_account_journal_restrict_mode_hash_table (env )
10481065 archive_account_tax_type_tax_use_adjustment (env )
10491066 fill_account_journal_invoice_reference_type (env )
1067+ fill_account_move_line_missing_fields (env )
10501068 migration_invoice_moves (env )
10511069 if openupgrade .table_exists (env .cr , 'account_voucher' ):
10521070 migration_voucher_moves (env )
0 commit comments