The constraints for a policy are specified in a serialized array name "policy". This results in confusing naming in the code, like $policy['policy'] where $policy is the policy array and $policy['policy'] are the policy's constraints.

$policy['constraints'] seems much clearer. We must rename the database field accordingly.

Comments

AohRveTPV created an issue. See original summary.

aohrvetpv’s picture

Status: Active » Needs review
StatusFileSize
new7.18 KB

Status: Needs review » Needs work
aohrvetpv’s picture

Status: Needs work » Needs review
StatusFileSize
new9.94 KB
aohrvetpv’s picture

Not sure it is correct to use drupal_get_schema() instead of drupal_get_schema_unprocessed() in the new update function.

  • AohRveTPV committed 6f358fd on 7.x-1.x
    Issue #2647176 by AohRveTPV: Rename 'policy' field to 'constraints'
    
aohrvetpv’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Needs review » Patch (to be ported)
eelkeblok’s picture

Status: Patch (to be ported) » Needs work

Although the patch has already been committed, it should be pointed out that referring to the current active schema from an update hook is actually risky business. You should consider the situation when you might make a change to the schema in the future and somebody is updating from before this update hook.

So, although it goes against our nature as programmers, you should actually copy the field spec from the schema into your update hook.

aohrvetpv’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

Thanks. I will fix it, but why is it a problem to use the current active schema? My understanding was other modules might alter the schema using hook_schema_alter(), so using drupal_get_schema() would get the altered schema. So it seems like passing the return value from drupal_get_schema() to db_change_field() is just resaving the current schema.

  • AohRveTPV committed 59b79ab on 7.x-1.x
    Revert "Issue #2647176 by AohRveTPV: Rename 'policy' field to '...
aohrvetpv’s picture

FWIW, drupal_get_schema_unprocessed() is recommended in a comment for db_change_field():

https://api.drupal.org/comment/58298#comment-58298

eelkeblok’s picture

You have a fair point about altering the schema, although IMHO that in itself is something that should be avoided as much as possible (this whole problem in itself is a good reason for that). There are two problems with using drupal_get_schema:

- You might get the cached version (i.e. without your changes);
- More importantly, the *current* schema might not always be what it is right now, and thus not what this update hook was intended to create and not what subsequent update hooks expect (you need to look at this not from the current pov, but from a future pov where several more schema changes have taken place).

The first point can be circumvented by using the unprocessed variant, but then you won't get any alterations. The second point remains.

aohrvetpv’s picture

I am having trouble seeing the second problem you mention. Could you possibly give an example of how it could be a problem?

TRUE could be passed for $rebuild of drupal_get_schema() to avoid getting a cached version.

When password_policy_update_7106() is run, the schema will be whatever it was when the administrator installed the module, plus any changes made by running password_policy_update_710{0,1,2,3,4,5}().

aohrvetpv’s picture

I see the problem now. I was very confused.

aohrvetpv’s picture

Status: Needs work » Needs review
StatusFileSize
new10.03 KB

#4 was broken too, not just bad practice. Apologies to anyone tracking the -dev branch.

If I understand correctly, we could use drupal_get_schema_unprocessed(), but then we might have to change the update function later per schema changes. Easier to just duplicate the field specification in the update function.

Thanks for explaining the proper practice, eelkeblok.

aohrvetpv’s picture

Some indentation was off by a space in last patch.

  • AohRveTPV committed a3884b8 on 7.x-1.x
    Issue #2647176 by AohRveTPV, eelkeblok: Rename 'policy' field to '...
aohrvetpv’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Needs review » Patch (to be ported)
eelkeblok’s picture

Status: Patch (to be ported) » Closed (outdated)

I'm taking the liberty to close this as outdated (at least considering its previous status, Patch to be ported). I take it the D6 version is no longer supported.