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

only set custom field to null if it is really null, not string 'null' #13042

Merged
merged 3 commits into from
Feb 24, 2019
Merged

only set custom field to null if it is really null, not string 'null' #13042

merged 3 commits into from
Feb 24, 2019

Conversation

jackrabbithanna
Copy link
Contributor

Overview

The string 'null' (any case) is converted to actual NULLs. Is this a feature or a bug? It's a bug for one of my clients, as they have a multi-select custom field with one of the option values being 'NULL'

Before

Custom fields cannot be inserted or updated if the value is the string 'NULL'

After

Null strings as values for custom fields are saved.

Technical Details

This may break other stuff?
Comments

Conversation about overall issue here: https://lab.civicrm.org/dev/core/issues/475

@civibot
Copy link

civibot bot commented Oct 31, 2018

(Standard links)

@civibot civibot bot added the master label Oct 31, 2018
@eileenmcnaughton
Copy link
Contributor

@jackrabbithanna I think the 'null' to NULL convention is too baked into Civi to change without terrifying me - we should probably prevent 'null' being an option value name - they still have control over the label

@jackrabbithanna
Copy link
Contributor Author

Can't do that for my client. Only 2 tests failed for the custom table query...
I made this PR to see what would break.

Mostly I want to know, why is it baked into Civi, what are the ramifications...Nobody seems to know exactly why 'null' is converted to NULL....Don't you just want to know?

Just wait, I got a patch for the packages repo, that'll really run the test suite through the gauntlet....:) hehehe....

If you look at:
civicrm/civicrm-packages@520511d

An exception for 'Null' was made awhile back...So I'm not 100% sure it WILL break anything...

@eileenmcnaughton
Copy link
Contributor

@jackrabbithanna it was baked into Civi back in 1.1 or whatever when they decided that would be a good way of handling 'null'.....ie. when you want to do $dao->copyParams or whatever it distinguishes between not set & please ignore & please update this & set it to null

@jackrabbithanna
Copy link
Contributor Author

I wonder if it's only the lower case string 'null' that is necessary....

@eileenmcnaughton
Copy link
Contributor

I think it probably is - certainly the patch implies as much

  • You could try changing the option value.value field in your customer db to 'Null' & in theory that should 'work'

@jackrabbithanna
Copy link
Contributor Author

I have no problem maintaining an little patch for my client, or making an exception for the all uppercase 'NULL' string....I'm just curious about they "why"

@jackrabbithanna
Copy link
Contributor Author

A patch for the CustomTableQuery is still necessary, I could exclude 'Null' and 'NULL' .. andrews patch for 'Null' in the packages repo is for all entity tables, like contacts...

@eileenmcnaughton
Copy link
Contributor

@jackrabbithanna so when you do $dao->save the $dao object is fully populated with properties
so it has the property 'source' regardless of whether it has been changed or not - so the fact it equals NULL doesn't tell you whether or not to set the value to NULL or ignore that field in the given update

@eileenmcnaughton
Copy link
Contributor

so back in the day they decided that specifying 'null' would cover that - my feeling is that it is very specifically 'null' not 'NULL' or 'Null' that is used for that purpose

@jackrabbithanna
Copy link
Contributor Author

Yep, adding a condition for only 'null' to turn into NULL passed

@jackrabbithanna
Copy link
Contributor Author

@eileenmcnaughton let me know if the last commit changes your mind about letting this in, I could write some test coverage...actually it would be really simple to update that entityCustomGroupWithSingleStringMultiSelectFieldCreate used by testCustomDataGet in the SyntaxConformanceTest.php to include an option 'NULL' => 'NULL' ... which should fail when trying to set the custom field value.

Otherwise we'll just keep a client specific patch going and not worry about it.

@eileenmcnaughton
Copy link
Contributor

@jackrabbithanna so does the final commit include anything else - ie. on packages repo

I note the offending line was added by @deepak-srivastava when updating merge logic (also ping @JKingsnorth ) - however I see no evidence that the strtolower was added for some specific reason (as opposed to a coding practice that seemed better at the time).

My feeling is the dedupe code coverage is pretty good and I'm comfortable merging this if you do the test change you suggested so we don't get into whack-a-mole if there is some unexpected fallout

@jackrabbithanna
Copy link
Contributor Author

The patch here in this PR is independent of any patch in the packages repo. This is specific for custom fields query generation, handled in civicrm-core

I think a patch to the packages repo would/could/should be independent, and that is where we'd really find possible regressions...that get's into the core DAO handling of all entities...

I'll have the updated test coverage here today sometime, and we'll make sure everything passes, then I can squash the commits and have it setup for merge into master if you are ok with it, and if we don't here back from anyone about possible issues.

What I'd be worried about is this may represent a departure for people having custom code or extensions, or possible imports. If people are depending on 'NULL' turning into NULL for custom code, or maybe even stock imports via csv, then this could change behavior for them.

I don't want to force this and then have a bunch of people caught by surprise, so if others want to say "no" to this, they can make the case for it.

@eileenmcnaughton
Copy link
Contributor

@jackrabbithanna I feel like 'null' has been the only thing ever supported for emptying an existing field but it's perhaps worth running through an import to check what did happen with 'NULL' in a custom field and in core field prior to this

@petednz
Copy link

petednz commented Nov 23, 2018

maybe i am testing wrong concept but on 5.3.x

| Reason | Contact ID | Gender | XYZ :: XYZ |
| Invalid value for field(s) : XYZ::XYZ; XYZ; Gender | 2 | null | null |

@eileenmcnaughton
Copy link
Contributor

@petednz what about a string field - e.g contact_source & a text custom field?

@petednz
Copy link

petednz commented Nov 23, 2018

where the contact already has data in those fields or doesn't that matter?

@petednz
Copy link

petednz commented Nov 23, 2018

no validation issues with Core or Custom string field - in both cases Null was accepted in the import and replaced existing content in those fields

@eileenmcnaughton eileenmcnaughton changed the title WIP: only set custom field to null if it is really null, not string 'null' only set custom field to null if it is really null, not string 'null' Feb 24, 2019
@eileenmcnaughton
Copy link
Contributor

I tested import & this doesn't affect import as it doesn't get past validation on custom fields. It didn't seem to be possible to pass in 'null' on these fields.

I'm going to merge this - there is no convention anywhere else in our code to support 'NULL' as a substitute for the magic 'null' so I think it makes sense to remove this odd support - the change was added by @deepak-srivastava for 4.7 before we got as tight about adding unit tests & insisting on breaking big PRs down into separate fixes - it appears to me the strotolower was belt & braces rather than needed

https://github.com/civicrm/civicrm-core/pull/6432/files#diff-927e742818deb9c89a00d2528e360bf0R213

I tested a UI edit & the value comes in as 'null' not NULL

I'm going to merge this as it makes sense & adds test coverage for the change - you might like to do some checks @deepak-srivastava

@eileenmcnaughton eileenmcnaughton merged commit d96bf18 into civicrm:master Feb 24, 2019
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