Repository navigation
fix(sql): detect failed dbDelta queries masked by later ones - #16
Merged
Merged
Conversation
dbDelta() runs DESCRIBE, SHOW INDEX and several ALTERs for an existing table, and every wpdb::query() resets last_error. Checking last_error only after the call missed a failed early ALTER (e.g. a UNIQUE KEY on duplicate data) when a later ALTER succeeded, so the migration was recorded as applied while the index was missing. Inspect last_error before each next query through a temporary 'query' filter and once more after dbDelta() returns. The DESCRIBE probe that dbDelta() runs for new tables is expected to fail and is ignored. A dbDelta($sql, false) convergence check was not used because dbDelta reports perpetual diffs for valid definitions (e.g. int vs int(11) on MariaDB, json vs longtext), which would fail working migrations.
Unmarking a migration makes the next `wp migration migrate` run its up() again, which is not idempotent (e.g. duplicate seed rows). That is a rollback in effect, yet `version --delete` was allowed in every environment while `rollback` and `execute --down` are limited to local/development. Apply the same environment guard; `version --add` stays available everywhere.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
WordPressSqlExecutorjudged aCREATE TABLE(routed throughdbDelta()) only by$wpdb->last_errorafter the call. For an existing tabledbDelta()runs several ALTERs and everywpdb::query()resetslast_error, so a failed early ALTER (e.g. aUNIQUE KEYon duplicate data) was masked by a later successful one. The migration was recorded as applied while the index or column was missing.wp migration version --deletewas allowed in every environment, whilerollbackandexecute --downare limited to local/development. Unmarking a migration makes the nextmigraterun its non-idempotentup()again.Fix
dbDelta(), a temporaryqueryfilter checkslast_errorbefore each next query (the filter runs beforewpdb::flush()), and the executor checks once more after the call. TheDESCRIBEprobe thatdbDelta()runs for new tables is expected to fail and is ignored.dbDelta($sql, false) === []convergence check was not used:dbDelta()reports perpetual diffs for valid definitions (intvsint(11)on MariaDB,jsonvslongtext,boolvstinyint(1), ...), so working migrations would start to fail.version --deletenow uses the existingrequireRollbackEnvironment()guard.version --addis unchanged. README, architecture doc and command help are updated.manage_optionscheck that onlyadoptuses (Info Roadmap feedback #3). Requiring--useron every mutating command would break deploy scripts, so that needs an owner decision.Tests
WordPressSqlExecutorTestcovers a failed ALTER followed by a successful one (fails before the fix) and checks that the DESCRIBE probe for a new table is ignored. The testwpdb/dbDelta()stubs now apply thequeryfilter and can replay several queries.testDbDeltaReportsAFailedAlterFollowedByASuccessfulOneruns against real WordPress and MariaDB 11.8 with aUNIQUE KEYon duplicate data followed by aKEY. Run locally, it fails before the fix and passes after it.MigrationRollbackPolicyTest:version --deleteis rejected in production/staging/unknown/empty/DEVELOPMENTwithout changing state (fails before the fix) and still works in local/development.composer qa(phpcs, PHPStan max, PHPUnit 81 tests) passes, as docomposer tests:database(10 tests on MariaDB 11.8) andcomposer validate --strict.Release
Patch 1.0.10: CHANGELOG entry and plugin header bump in
chore(release): prepare 1.0.10. It also lists the unreleased workflow pin commit.