Add 'changed' field to the subscription entity

Created on 17 May 2023, over 1 year ago
Updated 11 June 2024, 6 months ago

Currently, subscription entity type doesn't have a "changed" field which is very useful to track changes made to the subscriptions.
In our use case, we want to view subscriptions that were updated within a specific range.

Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @RedwanJamous
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    70 pass
  • Status changed to Needs work over 1 year ago
  • 🇮🇱Israel jsacksick
    1. +++ b/commerce_recurring.install
      @@ -144,3 +144,21 @@ function commerce_recurring_update_8106() {
      +      ->setLabel(t('Changed'))
      

      The indentation here is wrong.

    2. +++ b/src/Entity/Subscription.php
      @@ -742,6 +744,7 @@ class Subscription extends ContentEntityBase implements SubscriptionInterface {
      +    $this->setChangedTime(\Drupal::time()->getRequestTime());
      

      This shouldn't be needed as the field type plugin is reponsible for this already.

    3. +++ b/templates/commerce-subscription.html.twig
      @@ -30,7 +30,7 @@
      +        {% for key in ['created', 'chnaged', 'trial_starts', 'trial_ends', 'starts', 'ends'] %}
      

      Typo here, should be "changed".

  • Hi @jsacksick, thanks for the reply.

    I fixed the points you mentioned, and added tests.

    I also modified the update hook implementation to update all existing subscriptions to set the initial value of 'changed' field to match the value of 'created' field; this is needed because we need to have a value for 'changed' field in order to update it when doing operations other than saving the form (e.g. cancel operation). Using setInitialValueFromField('created') when defining the field won't work since the field types don't match.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    70 pass
  • 🇮🇱Israel jsacksick

    There's no way we can just load all subscriptions at once from an update hook. What about sites with millions of subscriptions???

  • Loaded them in chunks of size 20, please let me know if this should be changed.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    70 pass
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    70 pass
  • Prevented access check when updating existing subscriptions.

  • Status changed to Needs work over 1 year ago
  • 🇮🇱Israel jsacksick

    Updating subscriptions like this is useless. You're only updating 20 subscriptions here. So maybe we should simply set the changed value to the current time, or not set the field and call it a day.

  • I'm not updating 20 subscriptions only, I'm updating all subscriptions but loading them in chunks of size 20 (loading each group of 20 subscriptions at once) to avoid memory issues.

    Setting the value to the current time is an option, but setting it to match "created" field is more sensible since it will probably be much closer to the real last update.

    Not setting a value at all is problematic as I explained before; if you performed any operation on the subscription other than saving the form and "changed" field didn't have a value at that time, it will not get updated and will remain empty.

  • Status changed to Needs review over 1 year ago
  • 🇳🇿New Zealand john pitcairn

    This will be a valuable feature.

    I think the update hook should set the changed timestamp to the most recent of the "renewed" or "created" fields. That will at least produce approximately sensible results.

    Sites wishing to improve on that can implement their own update to set the changed date based on most recent paid order or whatever.

  • Status changed to Needs work over 1 year ago
  • Thank you for the feedback, @John.

    Attached is an updated patch that utilizes batch processing.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    70 pass
  • Status changed to Needs review 10 months ago
  • Status changed to Needs work 6 months ago
  • 🇮🇱Israel jsacksick

    I think we should skip loading entities all together... Doing direct update queries to the DB makes the most sense here... We don't really want to involve loading / saving entities... It'd take hours for a site with hundreds of thousands of entities this way... So let's just write an update hook that performs direct update queries to the subscription table, similar to what we did for setting the balance field for orders in the latest Commece release.
    Removing the "needs tests" tag as tests were added.

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    70 pass
  • Pipeline finished with Failed
    6 months ago
    Total: 191s
    #181547
  • Status changed to Needs review 6 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    Patch Failed to Apply
  • 🇺🇦Ukraine tbkot

    MR is ready to be checked
    The failed test is not related to the made changes it can be fixed by replacing

      $values = [
        'payment_method[target_id]' => 2,
      ];
    

    with

      $values = [
        'payment_method[target_id]' => '2',
      ];
    

    the number should be a string.

    Attaching a patch, just in case

  • 🇮🇱Israel jsacksick

    @tBKoT: Can you look into using the highest value between the created and the renewed timestamp as suggested in comment #13?

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    70 pass
  • 🇺🇦Ukraine tbkot

    @jsacksick
    MR updated. The changed date will be the greatest value between the "created" and "renewed" dates.

  • Pipeline finished with Failed
    6 months ago
    Total: 247s
    #183338
  • Status changed to Fixed 6 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024