-
Notifications
You must be signed in to change notification settings - Fork 39
Migrate public to private metakey #108
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
Open
tijmenbruggeman
wants to merge
21
commits into
tinify:master
Choose a base branch
from
wcreateweb:migrate-public-to-private-metakey
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3d81f3e
rename meta key to private meta key
tijmenbruggeman 5d99e3a
add migration 370
tijmenbruggeman 159d5e7
run migration on plugin load
tijmenbruggeman 7cc6dde
add migration tests
tijmenbruggeman 662a526
Change version to plain counter
tijmenbruggeman ab4f772
Rename migration
tijmenbruggeman ace767f
exit if migration fails
tijmenbruggeman d70b46c
consistent return value
tijmenbruggeman 27bfcc7
add changelog entry
tijmenbruggeman 5a5faac
cancel migration if it errored
tijmenbruggeman bde6368
remove redundent set
tijmenbruggeman 48f2c24
move migration to plugins_loaded hook
tijmenbruggeman 669853e
refer to the const instead of option
tijmenbruggeman da54abb
comment for clarity where nothing to migration is positive
tijmenbruggeman c535958
add test to validate migration failure
tijmenbruggeman 044ea9a
create an order list of migrations
tijmenbruggeman 9a75b4b
log error when migration failed
tijmenbruggeman 454ad77
add backoff mechanism to retry migration each hour
tijmenbruggeman 54027e0
format
tijmenbruggeman 1029b73
add tests for backoff
tijmenbruggeman 4c0f7fa
only add callable methods to stubs, swallow error logs in test
tijmenbruggeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php | ||
| /* | ||
| * Tiny Compress Images - WordPress plugin. | ||
| * Copyright (C) 2015-2026 Tinify B.V. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License as published by the Free | ||
| * Software Foundation; either version 2 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| * more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., 51 | ||
| * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| */ | ||
|
|
||
| /** | ||
| * Handles sequential database migrations for the TinyPNG plugin. | ||
| * | ||
| * Each migration method targets a specific version and is only executed | ||
| * once per site, tracked via the `DB_VERSION_OPTION` constant. | ||
| * | ||
| * @since 3.7.0 | ||
| */ | ||
| class Tiny_Migrate { | ||
|
|
||
| /** | ||
| * The current database schema version. | ||
| * | ||
| * Increment this integer by 1 each time a new migration is added. | ||
| * | ||
| * @since 3.7.0 | ||
| * @var int | ||
| */ | ||
| const DB_VERSION = 1; | ||
|
|
||
| /** | ||
| * WordPress option key used to track the applied database version. | ||
| * | ||
| * @since 3.7.0 | ||
| * @var string | ||
| */ | ||
| const DB_VERSION_OPTION = 'tinypng_db_version'; | ||
|
tijmenbruggeman marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * When migration fails, will pause migration for an hour | ||
| * when the key exists in memory | ||
| * | ||
| * @since 3.7.0 | ||
| * @var string | ||
| */ | ||
| const MIGRATION_BACKOFF_KEY = 'tinypng_migration_backoff'; | ||
|
|
||
| /** | ||
| * Returns an ordered map of migrations keyed by version number. | ||
| * | ||
| * Each entry maps a version integer to a callable that performs the | ||
| * corresponding migration. Add new entries in ascending version order. | ||
| * Increment `DB_VERSION` when adding a new migration. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return array<int, callable> Ordered map of version to migration callable. | ||
| */ | ||
| private static function migrations() { | ||
| return array( | ||
| 1 => array( self::class, 'migrate_meta_key_to_private' ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Runs all pending migrations in version order. | ||
| * | ||
| * Compares the stored database version against each known migration | ||
| * and executes any that have not yet been applied. Updates the stored | ||
| * version upon completion. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function run() { | ||
| $stored_version = (int) get_option( self::DB_VERSION_OPTION, 0 ); | ||
|
|
||
| if ( $stored_version >= self::DB_VERSION ) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ( self::migrations() as $version => $migration ) { | ||
| if ( $stored_version >= $version ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( get_transient( self::MIGRATION_BACKOFF_KEY ) ) { | ||
| // transient key to hold migrations exists so exit early | ||
| return; | ||
| } | ||
|
|
||
| if ( ! call_user_func( $migration ) ) { | ||
| set_transient( self::MIGRATION_BACKOFF_KEY, 1, HOUR_IN_SECONDS ); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| update_option( self::DB_VERSION_OPTION, self::DB_VERSION ); | ||
| } | ||
|
tijmenbruggeman marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Migrates the tiny meta key from public to private. | ||
| * | ||
| * Renames all `tiny_compress_images` post meta entries to | ||
| * `_tiny_compress_images`. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return bool True on success or when there is nothing to migrate, false on DB error. | ||
| */ | ||
| private static function migrate_meta_key_to_private() { | ||
| global $wpdb; | ||
|
|
||
| // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | ||
| $result = $wpdb->update( | ||
| $wpdb->postmeta, | ||
| array( 'meta_key' => '_tiny_compress_images' ), | ||
| array( 'meta_key' => 'tiny_compress_images' ), | ||
| array( '%s' ), | ||
| array( '%s' ) | ||
| ); | ||
|
|
||
| if ( false === $result ) { | ||
| // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | ||
| error_log( 'Tinify: failed to migrate meta key. DB error: ' . $wpdb->last_error ); | ||
| return false; | ||
| } | ||
|
tijmenbruggeman marked this conversation as resolved.
|
||
|
|
||
| // A return value of 0 means there was nothing to migrate, which is valid | ||
| // for fresh installs or databases that were already migrated. | ||
| return false !== $result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <?php | ||
|
|
||
| require_once dirname(__FILE__) . '/TinyTestCase.php'; | ||
| require_once dirname(__FILE__) . '/../../src/class-tiny-migrate.php'; | ||
|
|
||
| class Tiny_Migrate_Test extends Tiny_TestCase | ||
| { | ||
|
|
||
| public function set_up() | ||
| { | ||
| parent::set_up(); | ||
| // migration test logs error in stdout so swallow error logs | ||
| ini_set('error_log', '/dev/null'); | ||
| $this->wp->stub('update', function() { | ||
| return 1; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Helper to check if a specific option update occurred. | ||
| */ | ||
| private function assertOptionWasUpdated($option, $value) | ||
| { | ||
| $calls = $this->wp->getCalls('update_option'); | ||
| foreach ($calls as $call) { | ||
| if (isset($call[0], $call[1]) && $call[0] === $option && $call[1] === $value) { | ||
| return $this->assertTrue(true); | ||
| } | ||
| } | ||
| $this->fail("Failed asserting that option '$option' was updated to '$value'."); | ||
| } | ||
|
|
||
| public function test_run_skips_migration_when_db_version_is_current() | ||
| { | ||
| $this->wp->addOption(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $this->assertCount(0, $this->wp->getCalls('update'), 'Should not touch DB if version matches.'); | ||
| } | ||
|
|
||
| public function test_run_performs_migration_and_updates_version() | ||
| { | ||
| Tiny_Migrate::run(); | ||
|
|
||
| $update_calls = $this->wp->getCalls('update'); | ||
| $this->assertCount(1, $update_calls); | ||
|
|
||
| list($table, $data, $where) = $update_calls[0]; | ||
|
|
||
| $this->assertEquals('wp_postmeta', $table); | ||
| $this->assertEquals(array('meta_key' => '_tiny_compress_images'), $data); | ||
| $this->assertEquals(array('meta_key' => 'tiny_compress_images'), $where); | ||
|
|
||
| $this->assertOptionWasUpdated(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION); | ||
| } | ||
|
|
||
|
tijmenbruggeman marked this conversation as resolved.
|
||
| public function test_run_does_not_update_db_version_when_migration_fails() | ||
| { | ||
| $this->wp->stub('update', function() { return false; }); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $option_calls = $this->wp->getCalls('update_option'); | ||
| $version_updates = array_filter($option_calls, function($call) { return $call[0] === Tiny_Migrate::DB_VERSION_OPTION; }); | ||
|
|
||
| $this->assertEmpty($version_updates, 'Should not update DB version when migration fails.'); | ||
| } | ||
|
|
||
| public function test_run_does_not_update_option_if_unnecessary() | ||
| { | ||
| $this->wp->addOption(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $this->assertEmpty($this->wp->getCalls('update_option'), 'Should not call update_option at all when version is already current.'); | ||
| } | ||
|
|
||
| public function test_run_sets_backoff_transient_when_migration_fails() | ||
| { | ||
| $this->wp->stub('update', function() { return false; }); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $set_transient_calls = $this->wp->getCalls('set_transient'); | ||
| $this->assertCount(1, $set_transient_calls, 'A backoff transient should be set after a failed migration.'); | ||
| $this->assertEquals(Tiny_Migrate::MIGRATION_BACKOFF_KEY, $set_transient_calls[0][0]); | ||
| $this->assertEquals(HOUR_IN_SECONDS, $set_transient_calls[0][2]); | ||
| } | ||
|
|
||
| public function test_run_skips_migration_when_backoff_transient_is_set() | ||
| { | ||
| $this->wp->stub('get_transient', function($key) { | ||
| return Tiny_Migrate::MIGRATION_BACKOFF_KEY === $key ? 1 : false; | ||
| }); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $this->assertCount(0, $this->wp->getCalls('update'), 'DB update should not be attempted during the backoff period.'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.