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#4999 - POC CRM_Core_DAO_Base class which reads directly from schema/xml #29424

Closed
wants to merge 2 commits into from

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Feb 18, 2024

Overview

POC for https://lab.civicrm.org/dev/core/-/issues/4999
This allows DAO files to be gutted down to basically nothing, and instead inherit from a new base class which does all the work.

Before

Core + Extensions must generate (and periodically regenerate) their DAO files.

After

This PR plus #29422 would mean that extensions do not have to generate anything.

Comments

As noted in discussion in https://lab.civicrm.org/dev/core/-/issues/4999 parsing XML files is slow, but on the bright side this caches the parsed result so it only needs to happen once per DAO.

Copy link

civibot bot commented Feb 18, 2024

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or 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 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Feb 18, 2024
Copy link

civibot bot commented Feb 18, 2024

The issue associated with the Pull Request can be viewed at https://lab.civicrm.org/dev/core/-/issues/4999

@colemanw
Copy link
Member Author

Retest this please

@artfulrobot
Copy link
Contributor

artfulrobot commented Feb 19, 2024

Sorry to be a pain; I'll say this then butt out because this whole thing seemed to be focussed on making developers happier by solving a minor inconvenience and it's kinda doing the opposite to this developer but I'm only one and you do this more than me, but my personal opinion is:

I'm still un sold on this.

I like the idea of ditching DAO. I don't like the idea of doubling down on XML.

There's some great example of the joys of XML parsing in this PR.

We want XML (string representations) to describe things to PHP (multi types), since it's PHP that will be translating to SQL (other string representations). XML is not easy to parse by PHP. It's mostly humans who have to write this XML code, yet XML is not easy to write either.

Example: the parsing uses simple_xml_load and then the resultant xml php object, whose ultimate values are basically only accessible as strings.

Too much talk about nothing (null/falsy/""): Take <default/> for example. With isset on class properties, you get some weirdness. For example, PHP types "" (and 0 and FALSE) are all considered isset, and therefore "" (or "0" or ""). On the other hand, isset on a property whose value is PHP NULL is FALSE. An unset xml property will behave the same as PHP NULL.

The weird thing is that default's value goes in the SQL, and needs to be SQL-safe. So 0 is valid but "" is not (CREATE TABLE .... field VARCHAR(255) NOT NULL DEFAULT:bomb:, field2...)

We don't care about PHP NULL because we're not using XML properly (attr xsi:nil="true" would be the proper way, right?), so we encode null-default by omitting the default element. Oh but we also support the string value NULL through some hackish stuff which we probably thought was OK because Pear's db layer also does similarly.

Did I say finally? There's another thing which is that we should use LIBXML_NOBLANKS in the parse command because otherwise we need trim() everywhere if using human-written XML, else all these differ:

<default>NULL</default>
<default>
  NULL 
</default>
<default> NULL </default>

@colemanw
Copy link
Member Author

See further conversation at https://lab.civicrm.org/dev/core/-/issues/4999


private static function getEntityDefinition(): SimpleXMLElement {
if (!isset(Civi::$statics[static::class][__FUNCTION__])) {
[, $dir, , $name] = explode('_', static::class);
Copy link
Member

Choose a reason for hiding this comment

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

Related edge cases:

  • CRM_Foo_BAO_Bar extends CRM_Foo_DAO_Bar ==> Would appear as two different classes
  • CRM_Event_Cart_DAO_* and CRM_Mailing_Event_DAO_* ==> Placement of "DAO"/"BAO" can vary
  • In a test-suite, the schema/class-structure remains constant, but Civi::$statics is reset several thousand times
    • (ClassScanner has a kind of similar consideration where it caches class-structure. It uses static::$caches instead of Civi::$statics. Spot-checking api_v3_ContactTest, this saves ~7% on execution-time -- which should translate to 2-3min on each of the bigger suites.)

I suppose it's not complicated to address any of those (though maybe less pretty).

(I think similar notes would apply in this branch or similar variations with different file-formats.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Noted. It looks like we won't be using this code anyway given that the discussion has evolved to favor a different file format & loading mechanism. But still good points.

@colemanw colemanw closed this Feb 22, 2024
@colemanw
Copy link
Member Author

colemanw commented Mar 5, 2024

Closed in favor of #29472

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants