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

CustomGroup - Ensure 'name' is always unique #22675

Merged
merged 1 commit into from
Feb 2, 2022

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Feb 1, 2022

Overview

Stricter enforcement of CustomGroup name uniqueness.

Before

Previously, unique 'name' was indexed in tandem with 'extends'.

After

Now it is ensured to be unique unconditionally.

Technical Details

Note that at the form layer, unconditionally unique names have always been enforced, so in practice, every custom group name should already be unique.

$name = CRM_Utils_String::munge($title, '_', 64);
$query = 'select count(*) from civicrm_custom_group where ( name like %1) and id != %2';
$grpCnt = CRM_Core_DAO::singleValueQuery($query, [
1 => [$name, 'String'],
2 => [(int) $self->_id, 'Integer'],
]);
if ($grpCnt) {
$errors['title'] = ts('Custom group \'%1\' already exists in Database.', [1 => $title]);

However this was never enforced at the BAO or API layer, so this PR includes an upgrade script to uniquify duplicate names.

Comments

The upgrade sql is simplistic, it will update any pair of duplicates found, but doesn't cover the possibility of triplets or quadruplets. But I think that's extremely unlikely - due to the enforcement that was happening at the form layer there really shouldn't be any duplicates at all; so the upgrade sql is there as a safeguard rather than a necessity.

@civibot
Copy link

civibot bot commented Feb 1, 2022

(Standard links)

@civibot civibot bot added the master label Feb 1, 2022
@eileenmcnaughton
Copy link
Contributor

I wonder if we need some messaging - on the one hand we are changing name which could be used to access fields. On the other it would have been flakey.... I could imaging names being used in civicrm_managed - which would be ... flakey.

We could possibly add a pre-message if any will be altered? Which would probably be rare but might alert them to any odd potential

@colemanw colemanw force-pushed the customGroupNameIndex branch from 3ce7b66 to e18b780 Compare February 1, 2022 23:09
@colemanw
Copy link
Member Author

colemanw commented Feb 1, 2022

@eileenmcnaughton I've added the pre-upgrade check.

@colemanw
Copy link
Member Author

colemanw commented Feb 2, 2022

Tests are green and I really think this needs to be merged asap. As @eileenmcnaughton points out, the existence of non-unique names would be flaky in managed entities... but it just occurred to me that they would be even flakier in APIv4, which relies on name uniqueness for getting and setting all custom data!
@demeritcowboy @seamuslee001

@demeritcowboy
Copy link
Contributor

demeritcowboy commented Feb 2, 2022

It runs ok just two suggestions:

  1. The message gets hidden because of the styling of the bullets above and looks like boilerplate footer text which the brain just skips. If it had its own bullet it would be more visible.

Untitled2

2. If they do read it, the average person will think it means the label will be changed. Maybe: "You have custom field groups with duplicate internal names in your system. They will automatically be renamed. If you have custom code that refers to these names then you will need to update that code to match the new names."

Previously, unique 'name' was only enforced in tandem with 'extends'
Now it is required to be unique unconditionally.
@colemanw colemanw force-pushed the customGroupNameIndex branch from e18b780 to 79b6ac2 Compare February 2, 2022 19:07
@colemanw
Copy link
Member Author

colemanw commented Feb 2, 2022

Thanks for testing this @demeritcowboy. I've updated the message text, but kept the markup as-is because it's consistent with just about all other markup for pre-upgrade messages. I agree that making these messages easier to read is something to work on, but I see it as a separate issue.

@colemanw colemanw merged commit 97bd997 into civicrm:master Feb 2, 2022
@colemanw colemanw deleted the customGroupNameIndex branch February 2, 2022 20:38
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