-
Notifications
You must be signed in to change notification settings - Fork 40
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
Merged
rkoopmans
merged 34 commits into
tinify:master
from
wcreateweb:migrate-public-to-private-metakey
Jun 26, 2026
Merged
Changes from 11 commits
Commits
Show all changes
34 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 d2641ac
change docs
tijmenbruggeman c5073f3
add wp_cache_flush to prevent stale meta data keys
tijmenbruggeman bc3c83d
migrate in batches of 2500
tijmenbruggeman 3404849
fall back to legacy key when migrating
tijmenbruggeman dbf6aea
require php 5.6
tijmenbruggeman 2c94ac8
define when undefined
tijmenbruggeman b602bdc
format
tijmenbruggeman 7e2551d
stop swallwing errs
tijmenbruggeman 4786c2e
use self as it is within class
tijmenbruggeman 7538b01
revert php require
tijmenbruggeman 60415a8
fix tests after moving func
tijmenbruggeman e514714
add legacy key to tests
tijmenbruggeman 022e581
run migration on admin_init
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
Some comments aren't visible on the classic Files Changed page.
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,102 @@ | ||
| <?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 `tinypng_db_version` option. | ||
|
tijmenbruggeman marked this conversation as resolved.
Outdated
|
||
| * | ||
| * @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.
|
||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
|
|
||
| if ( $stored_version < 1 && ! self::migrate_meta_key_to_private() ) { | ||
| return; | ||
| } | ||
|
|
||
| update_option( self::DB_VERSION_OPTION, self::DB_VERSION ); | ||
| } | ||
|
tijmenbruggeman marked this conversation as resolved.
tijmenbruggeman marked this conversation as resolved.
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 boolean | ||
| */ | ||
| 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' ), | ||
|
tijmenbruggeman marked this conversation as resolved.
Outdated
|
||
| array( '%s' ), | ||
| array( '%s' ) | ||
| ); | ||
|
|
||
| if ( false === $result ) { | ||
| return false; | ||
| } | ||
|
tijmenbruggeman marked this conversation as resolved.
Outdated
|
||
|
|
||
| return true; | ||
|
tijmenbruggeman marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
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,65 @@ | ||
| <?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(); | ||
| $this->wp->stub('update', 1); | ||
| } | ||
|
tijmenbruggeman marked this conversation as resolved.
|
||
|
|
||
|
tijmenbruggeman marked this conversation as resolved.
|
||
| /** | ||
| * 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 (($call[0] ?? null) === $option && ($call[1] ?? null) === $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(['meta_key' => '_tiny_compress_images'], $data); | ||
| $this->assertEquals(['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_option_if_unnecessary() | ||
| { | ||
| $this->wp->addOption(Tiny_Migrate::DB_VERSION_OPTION, Tiny_Migrate::DB_VERSION); | ||
|
|
||
| Tiny_Migrate::run(); | ||
|
|
||
| $option_calls = $this->wp->getCalls('update_option'); | ||
| $version_updates = array_filter($option_calls, fn($call) => $call[0] === Tiny_Migrate::DB_VERSION_OPTION); | ||
|
|
||
| $this->assertEmpty($version_updates, 'Should not re-save the version if already current.'); | ||
| } | ||
| } | ||
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.