Skip to content

Commit

Permalink
[OU-IMP] payment: review followup
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Dec 9, 2024
1 parent 71763db commit f585b80
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
37 changes: 26 additions & 11 deletions openupgrade_scripts/scripts/payment/17.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions openupgrade_scripts/scripts/payment/17.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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)])
)

0 comments on commit f585b80

Please sign in to comment.