Skip to content

Commit 4c4cd1a

Browse files
authored
Merge pull request #2919 from tegin/13.0-imp-account-script
[IMP] account: Do the transformation of messages on a single query, not one for each record
2 parents 5468e51 + 3259aed commit 4c4cd1a

File tree

2 files changed

+78
-65
lines changed

2 files changed

+78
-65
lines changed

addons/account/migrations/13.0.1.1/post-migration.py

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

639641
def 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()
10401057
def 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)

addons/account/migrations/13.0.1.1/pre-migration.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
('account_register_payments', 'account_payment_register'),
5252
('account_analytic_tag_account_reconcile_model_rel',
5353
'account_reconcile_model_analytic_tag_rel'),
54+
('account_invoice_payment_rel', 'old_account_invoice_payment_rel'),
55+
]
56+
57+
_field_adds = [
58+
("account_root_id", "account.move.line", "account_move_line", "many2one", False, "account"),
59+
("tax_group_id", "account.move.line", "account_move_line", "many2one", False, "account")
5460
]
5561

5662

@@ -146,36 +152,25 @@ def create_account_move_new_columns(env):
146152
)
147153

148154

149-
def fill_account_move_line_parent_state(env):
150-
"""Faster way"""
151-
openupgrade.logged_query(
152-
env.cr, """
153-
ALTER TABLE account_move_line
154-
ADD COLUMN parent_state varchar""",
155-
)
156-
openupgrade.logged_query(
157-
env.cr, """
158-
UPDATE account_move_line aml
159-
SET parent_state = am.state
160-
FROM account_move am
161-
WHERE am.id = aml.move_id""",
162-
)
163-
164-
165-
def fill_account_move_line_account_internal_type(env):
155+
def fill_account_move_line(env):
166156
"""Faster way"""
167157
openupgrade.logged_query(
168158
env.cr, """
169159
ALTER TABLE account_move_line
160+
ADD COLUMN parent_state varchar,
170161
ADD COLUMN account_internal_type varchar""",
171162
)
172163
openupgrade.logged_query(
173164
env.cr, """
174165
UPDATE account_move_line aml
175-
SET account_internal_type = aat.type
176-
FROM account_account aa
177-
JOIN account_account_type aat ON aa.user_type_id = aat.id
178-
WHERE aml.account_id = aa.id""",
166+
SET parent_state = am.state,
167+
account_internal_type = aat.type
168+
FROM account_move am,
169+
account_account aa,
170+
account_account_type aat
171+
WHERE am.id = aml.move_id
172+
AND aa.user_type_id = aat.id
173+
AND aml.account_id = aa.id""",
179174
)
180175

181176

@@ -315,11 +310,11 @@ def migrate(env, version):
315310
)
316311
openupgrade.rename_models(cr, _model_renames)
317312
openupgrade.rename_tables(cr, _table_renames)
313+
openupgrade.add_fields(env, _field_adds)
318314
type_change_account_fiscal_position_zips(env)
319315
create_account_invoice_amount_tax_company_signed(env)
320316
create_account_move_new_columns(env)
321-
fill_account_move_line_parent_state(env)
322-
fill_account_move_line_account_internal_type(env)
317+
fill_account_move_line(env)
323318
create_res_partner_ranks(env)
324319
delete_fk_constraints(env)
325320
fill_account_move_commercial_partner_id(env)

0 commit comments

Comments
 (0)