Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#4418 Add in Upgrade script to fix double json encoding #26760

Merged
merged 3 commits into from
Jul 7, 2023

Conversation

seamuslee001
Copy link
Contributor

Overview

This adds in an upgrade script to fix potential double json encoding of the accepted credit cards

Before

Bad data possibly in the database

After

All good data

ping @larssandergreen @eileenmcnaughton @colemanw

@civibot
Copy link

civibot bot commented Jul 6, 2023

Thank you for contributing to CiviCRM! ❤️ Here's what's next:

  • If this is your first PR, an admin will greenlight automated testing with the command "add to whitelist".
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command "test this please" to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • Here are some handy links for reviewers:

@@ -33,6 +35,7 @@ public function upgrade_5_64_alpha1($rev): void {
$this->addTask('Drop unused civicrm_action_mapping table', 'dropTable', 'civicrm_action_mapping');
$this->addTask('Update post_URL/cancel_URL in logging tables', 'updateLogging');
$this->addTask('Add in Everybody ACL Role option value', 'addEveryBodyAclOptionValue');
$this->addTask(ts('Fix double json encoding of accepted_credit_cards field in payment processor table'), 'fixDoubleEscapingPaymentProcessorCreditCards');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts is unusual here, since it's just a one-off message for admins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are common strings repeated across upgrades, so are better candidates for ts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'd argue they are still just for admins so shouldn't have ts either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the ts now

@seamuslee001 seamuslee001 force-pushed the payment_processor_upgrade branch from 36ca618 to 2b23af7 Compare July 6, 2023 23:46
@seamuslee001 seamuslee001 changed the title [REF] dev/core#4418 Add in Upgrade script to fix double json encoding dev/core#4418 Add in Upgrade script to fix double json encoding Jul 6, 2023
@demeritcowboy
Copy link
Contributor

demeritcowboy commented Jul 6, 2023

I'm getting TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in array_keys() (line 70 of ...\CRM\Upgrade\Incremental\php\FiveSixtyFour.php)

If a pp doesn't have any selected the value is null.

@demeritcowboy
Copy link
Contributor

Also calling json_decode on $paymentProcessor['accepted_credit_cards'] errors even when it's good because the api call returns an array (or something like that - error is TypeError: json_decode(): Argument #1 ($json) must be of type string, array given in json_decode() (line 71 of C...\CRM\Upgrade\Incremental\php\FiveSixtyFour.php)

…d in the upgrade script and fix upgrade scripts as per dave's comments
@seamuslee001
Copy link
Contributor Author

thanks @demeritcowboy I found some issues with my unit test which weren't pointing out those issues, I believe I have now fixed both issues you have reported and made sure the unit test covers all scenarios

@demeritcowboy
Copy link
Contributor

Hehe made you work for this one. Take the rest of the day off ... as per dave's comments...

Works for me.

@larssandergreen
Copy link
Contributor

larssandergreen commented Jul 7, 2023

Unfortunately, it's messier than this and this doesn't actually work to fix the values that are stored in the db for me.
I think because the test uses addValue it won't capture what happens when you pass an array that contains JSON as we were doing with save records => ['accepted_credit_cards' => json_encode($creditCards)]. Then you end up with this ["{\"Visa\":\"Visa\",\"MasterCard\":\"MasterCard\"}"], which isn't going to be fixed by json_decode.

@seamuslee001
Copy link
Contributor Author

@larssandergreen they should be identical addValue v saveRecords should produce identical situations

@demeritcowboy
Copy link
Contributor

I also had ["{\"Visa\":\"Visa\",\"MasterCard\":\"MasterCard\"}"] in the db, and the api.get returns it as an array, so then the [0] then gives the json string, which decoded for me to the right value.

But maybe we shouldn't be using api in the upgrade script, as has come up elsewhere.

@seamuslee001
Copy link
Contributor Author

@demeritcowboy I think the API is safer now than earlier with regards to upgrades

@demeritcowboy
Copy link
Contributor

Is Civi\Api4\PaymentProcessor a real class or is it some kind of virtual entity? It had no problem finding it during the real upgrade, but I don't see it in the tree?

@demeritcowboy
Copy link
Contributor

Oh it's in the ext\civi_contribute extension, which I guess isn't enabled in the upgrade test suite.

@demeritcowboy
Copy link
Contributor

So that might fail in real life if somebody didn't have that component enabled.

@seamuslee001
Copy link
Contributor Author

@demeritcowboy oh probably because extension install tasks go last right so enabling the component extensions stuffs things up hmm

@seamuslee001
Copy link
Contributor Author

ok @demeritcowboy have shifted to use executeQuery instead of the API in the upgrade should be right now right?

@larssandergreen
Copy link
Contributor

OK, I just figured out how to make this work with the API version, but now I see there is a different SQL version. I'll give that a try.

@larssandergreen
Copy link
Contributor

To make this work for me, I needed:
$paymentProcessors = PaymentProcessor::get(FALSE)->addWhere('is_test', 'IS NOT NULL')->execute();
and
PaymentProcessor::update(FALSE)->addValue('accepted_credit_cards', json_decode(($paymentProcessor['accepted_credit_cards'])[0], TRUE))->addWhere('id', '=', $paymentProcessor['id'])->execute(); (adding [0])

@demeritcowboy
Copy link
Contributor

@larssandergreen I think you were testing the initial version. There was a second version that had those.

But @seamuslee001 did you want to keep the is_test IS NOT NULL AND domain_id IS NOT NULL part? I assume those are weeding out anomalies from pp's created by api or something.

@larssandergreen
Copy link
Contributor

@demeritcowboy Oh, that's frustrating, must have missed that. Working for me now anyways.

@seamuslee001
Copy link
Contributor Author

@demeritcowboy no i don't need that those were weeding out implicit filters that the API adds in (i.e. by default if you don't put a where in and an entity has domain_id column it will filter to the current domain and same with the is_test filtering by default to be true)

@demeritcowboy
Copy link
Contributor

Ah ok got it.

So yes this works for me too.

@seamuslee001 seamuslee001 merged commit 457eaf8 into civicrm:master Jul 7, 2023
@seamuslee001 seamuslee001 deleted the payment_processor_upgrade branch July 7, 2023 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants