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

Restrict doing plugin upgrade routine when not in admin #4538

Merged
merged 1 commit into from
Apr 13, 2020

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Apr 6, 2020

Summary

The changes here in effect restrict the upgrade routine to happen at admin_init. This was discussed in depth at #4177. This implements #3284 (comment):

The DB upgrade routines don't happen on the frontend either, as you recall the upgrade DB message is only presented once trying to access the admin.

Fixes #3284

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label Apr 6, 2020
@westonruter westonruter marked this pull request as ready for review April 6, 2020 17:39
@westonruter westonruter added this to the v1.5.3 milestone Apr 6, 2020
@schlessera
Copy link
Collaborator

Before doing this, we'll need to audit what will happen if a new version of the plugin runs with an old DB version, as the upgrade routine will now not run unconditionally anymore and it might be hours, days or weeks between updating the plugin files and updating the DB.

This can lead to unexpected behavior and breakage if the new code doesn't expect the old DB schema.

@westonruter
Copy link
Member Author

@schlessera There are only two instances of the amp_plugin_update hook being used at present:

/**
* Handle update to plugin.
*
* @param string $old_version Old version.
*/
public static function handle_plugin_update( $old_version ) {
// Update the old post type slug from amp_validated_url to amp_validated_url.
if ( '1.0-' === substr( $old_version, 0, 4 ) || version_compare( $old_version, '1.0', '<' ) ) {
global $wpdb;
$post_ids = get_posts(
[
'post_type' => 'amp_invalid_url',
'fields' => 'ids',
'posts_per_page' => -1,
]
);
foreach ( $post_ids as $post_id ) {
$wpdb->update(
$wpdb->posts,
[ 'post_type' => self::POST_TYPE_SLUG ],
[ 'ID' => $post_id ]
);
clean_post_cache( $post_id );
}
}
}

/**
* Handle update to plugin.
*
* @param string $old_version Old version.
*/
public function handle_plugin_update( $old_version ) {
// Reset the disabling of the CSS caching subsystem when updating from versions 1.5.0 or 1.5.1.
if ( version_compare( $old_version, '1.5.0', '>=' ) && version_compare( $old_version, '1.5.2', '<' ) ) {
AMP_Options_Manager::update_option( Option::DISABLE_CSS_TRANSIENT_CACHING, false );
}
}

I don't see how either of these will cause problems if they are deferred to when the admin user logs in. For the first, it's only for sites running the AMP plugin on versions before 1.0 stable. And it is just renaming a post type for the validated URLs. There's no impact on that being deferred.

For the second, it's just making sure the transient caching gets re-enabled when upgrading from 1.5.0+ to 1.5.2. This also can wait until the admin user logs in.

So in both cases I see no issue to move forward.

@schlessera
Copy link
Collaborator

Looks good then. We just need to keep this in mind when we use this hook. It changes from enforcing consistency after upgrades to hopefully end up with eventual consistency instead. So we cannot rely on anything that happens during an update event, it will either need to be optional or trigger user feedback in some way.

@westonruter westonruter merged commit db74211 into develop Apr 13, 2020
@westonruter westonruter deleted the update/plugin-upgrade-spawning branch April 13, 2020 18:20
westonruter added a commit to hansschuijff/amp-wp that referenced this pull request Apr 14, 2020
* 'develop' of github.com:ampproject/amp-wp:
  Update tests after block-library/style.css changes in Gutenberg 7.9 (ampproject#4579)
  Restrict doing plugin upgrade routine when not in admin (ampproject#4538)
@pierlon
Copy link
Contributor

pierlon commented Apr 14, 2020

Verified in QA. The upgrade routine is only run when an admin logs in that has the permission of managing options.

westonruter added a commit that referenced this pull request Apr 15, 2020
* tag '1.5.3':
  Bump 1.5.3
  Bump version to 1.5.3-RC1
  Fix handling of Mustache templates (#4583)
  Stub request based on test scenario (#4588)
  Update tests after block-library/style.css changes in Gutenberg 7.9 (#4579)
  Restrict doing plugin upgrade routine when not in admin (#4538)
  Add new accessibility sanitizer (#4535)
  Fix unit tests (#4564)
  Add button into Site Health to reenable CSS transient caching (#4522)
  Restore unification of multi-page post content in Reader mode (#4547)
  Prevent styles from being removed when in Customizer preview with Standard mode (#4553)
  Omit Jetpack from being activated during PHPUnit test runs (#4474)
  Mock Facebook embed tests (#4474)
  Mock Imgur embed tests (#4474)
  Use title case for Paired Browsing link in edit post screen (#4540)
  Ensure that validation query vars persist through redirects (#4544)
  Add requirements to plugin file header (#4543)
  Force status code of validation responses to be 200 (#4533)
  Update optimizer test specs (#4527)
  Bump 1.5.3-alpha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce scenarios where DB writes are performed during frontend requests
4 participants