From f585b80828c4f12300ea7ef0196b08eef83f3a52 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 9 Dec 2024 18:00:39 +0100 Subject: [PATCH] [OU-IMP] payment: review followup --- .../payment/17.0.2.0/post-migration.py | 37 +++++++++++++------ .../scripts/payment/17.0.2.0/pre-migration.py | 8 ++++ .../payment/tests/data_payment_migration.py | 24 ++++++++++++ .../payment/tests/test_payment_migration.py | 13 +++++++ 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 openupgrade_scripts/scripts/payment/tests/data_payment_migration.py create mode 100644 openupgrade_scripts/scripts/payment/tests/test_payment_migration.py diff --git a/openupgrade_scripts/scripts/payment/17.0.2.0/post-migration.py b/openupgrade_scripts/scripts/payment/17.0.2.0/post-migration.py index 385af4a4ffa..0dc3f175cd2 100644 --- a/openupgrade_scripts/scripts/payment/17.0.2.0/post-migration.py +++ b/openupgrade_scripts/scripts/payment/17.0.2.0/post-migration.py @@ -6,23 +6,38 @@ def _fill_payment_method(env): + PaymentProvider = env["payment.provider"] PaymentToken = env["payment.token"].with_context(active_test=False) PaymentTransaction = env["payment.transaction"].with_context(active_test=False) PaymentMethod = env["payment.method"].with_context(active_test=False) unknown_payment_method = env.ref("payment.payment_method_unknown") - for payment_token in PaymentToken.search([("payment_method_id", "=", False)]): - payment_token.payment_method_id = ( - PaymentMethod._get_from_code(payment_token.provider_id.code) - or unknown_payment_method - ).id - - for transaction in PaymentTransaction.search([("payment_method_id", "=", False)]): - transaction.payment_method_id = ( - PaymentMethod._get_from_code(transaction.provider_id.code) - or unknown_payment_method - ).id + for group in PaymentToken.read_group( + [("payment_method_id", "=", False)], ["provider_id"], ["provider_id"] + ): + provider = PaymentProvider.browse(group["provider_id"][0]) + PaymentToken.search(group["__domain"]).write( + { + "payment_method_id": ( + PaymentMethod._get_from_code(provider.code) + or unknown_payment_method + ).id, + } + ) + + for group in PaymentTransaction.read_group( + [("payment_method_id", "=", False)], ["provider_id"], ["provider_id"] + ): + provider = PaymentProvider.browse(group["provider_id"][0]) + PaymentTransaction.search(group["__domain"]).write( + { + "payment_method_id": ( + PaymentMethod._get_from_code(provider.code) + or unknown_payment_method + ).id + } + ) @openupgrade.migrate() diff --git a/openupgrade_scripts/scripts/payment/17.0.2.0/pre-migration.py b/openupgrade_scripts/scripts/payment/17.0.2.0/pre-migration.py index b51facaacc8..0ac866e0a0e 100644 --- a/openupgrade_scripts/scripts/payment/17.0.2.0/pre-migration.py +++ b/openupgrade_scripts/scripts/payment/17.0.2.0/pre-migration.py @@ -142,8 +142,15 @@ _table_renames = [ ("payment_icon", "payment_method"), + ("payment_icon_payment_provider_rel", "payment_method_payment_provider_rel"), ] +_column_renames = { + "payment_method_payment_provider_rel": [ + ("payment_icon_id", "payment_method_id"), + ], +} + _field_renames = [ ( "payment.provider", @@ -184,6 +191,7 @@ def migrate(env, version): openupgrade.rename_models(env.cr, _model_renames) openupgrade.rename_tables(env.cr, _table_renames) + openupgrade.rename_columns(env.cr, _column_renames) openupgrade.rename_fields(env, _field_renames) openupgrade.rename_xmlids(env.cr, _xmlids_renames) openupgrade.set_xml_ids_noupdate_value(env, "payment", _noupdate_xmlids, True) diff --git a/openupgrade_scripts/scripts/payment/tests/data_payment_migration.py b/openupgrade_scripts/scripts/payment/tests/data_payment_migration.py new file mode 100644 index 00000000000..8573cdf4b07 --- /dev/null +++ b/openupgrade_scripts/scripts/payment/tests/data_payment_migration.py @@ -0,0 +1,24 @@ +env = locals().get("env") +# create a payment token and a transaction to +# see if our migration migrates them correctly +demo_provider = env.ref("payment.payment_provider_demo") +demo_provider.state = "test" +token = env["payment.token"].create( + { + "provider_id": demo_provider.id, + "partner_id": env.user.partner_id.id, + "provider_ref": "hello world", + } +) +env["payment.transaction"].create( + { + "provider_id": demo_provider.id, + "token_id": token.id, + "reference": "hello world", + "amount": 42, + "currency_id": demo_provider.main_currency_id.id, + "state": "pending", + "partner_id": env.user.partner_id.id, + } +) +env.cr.commit() diff --git a/openupgrade_scripts/scripts/payment/tests/test_payment_migration.py b/openupgrade_scripts/scripts/payment/tests/test_payment_migration.py new file mode 100644 index 00000000000..6f37e0599aa --- /dev/null +++ b/openupgrade_scripts/scripts/payment/tests/test_payment_migration.py @@ -0,0 +1,13 @@ +from odoo.tests import TransactionCase + +from odoo.addons.openupgrade_framework import openupgrade_test + + +@openupgrade_test +class TestPaymentMigration(TransactionCase): + def test_no_transactions_without_method(self): + payment_transactions = self.env["payment.transaction"].search([]) + self.assertTrue(payment_transactions) + self.assertFalse( + payment_transactions.filtered_domain([("payment_method_id", "=", False)]) + )