Skip to content

Restore session variables with COALESCE in exported dumps, so split imports keep the current value - #42

Merged
JanJakes merged 2 commits into
mainfrom
fix-export-session-variable-restore
Nov 25, 2026
Merged

JanJakes merged 2 commits into
mainfrom
fix-export-session-variable-restore

Conversation

@youknowriad

@youknowriad youknowriad commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Exported dumps keep the mysqldump header from #40, but the footer now falls back to the current value when the saved one is missing:

SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
...
SET FOREIGN_KEY_CHECKS=COALESCE(@OLD_FOREIGN_KEY_CHECKS, @@FOREIGN_KEY_CHECKS);
SET SQL_MODE=COALESCE(@OLD_SQL_MODE, @@SQL_MODE);

Why

A user variable only lives in the connection that set it, and some importers don't run a dump in a single connection. Jetpack Backup's restore, which WordPress Studio's push to WordPress.com goes through, splits each table's statements into several batches. It sends those batches to the site over separate requests, each with its own MySQL connection. So SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; runs where the variable is NULL, and MySQL rejects it with error 1231 (Variable 'foreign_key_checks' can't be set to the value of 'NULL').

Since v1.1.11, Jetpack's first restore method fails on Studio pushes. The fallback then restarts the whole restore, which adds hours on large sites and sometimes fails too.

With COALESCE, a dump imported over one connection restores the original values exactly as mysqldump does. On a new connection, the footer keeps that connection's current values.

Testing

  • Pushed a Studio site (database only) to a WordPress.com test site with a Studio CLI that bundles this change. Jetpack's first restore method succeeded on its first attempt. The day before, the same site's push failed that method on this statement with v1.1.11.
  • The export scenario from Disable foreign key checks and strict SQL modes in exported dumps #40 now checks for the new footer. It still exports and re-imports tables whose foreign key references a table created later in the dump, so the SQLite import accepts the COALESCE statements.
  • WP_CLI_TEST_DBTYPE=sqlite vendor/bin/behat: all scenarios pass. composer phpcs and composer lint are clean.

🤖 Generated with Claude Code

…, so importers that split statements across connections don't restore NULL

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@youknowriad
youknowriad requested a review from JanJakes November 24, 2026 15:50
@youknowriad

Copy link
Copy Markdown
Contributor Author

@senadir @JanJakes unfortunately pushes for medium/large sites are now very unreliable in Studio because of this. I'd appreciate if we can ship this quickly

@JanJakes JanJakes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad Checking 👀

Could we keep the mysqldump behavior and make the restore safer? For example, SET SQL_MODE=COALESCE(@OLD_SQL_MODE, @@SQL_MODE); would restore the original mode on the same connection and keep the current mode on a fresh one. We could do the same for FOREIGN_KEY_CHECKS.

Two questions about Jetpack Backup’s restore:

  1. Would a standard mysqldump export hit the same error if Jetpack runs its footer on a different connection from its header?
  2. When a table spans several chunks, does Jetpack reapply SQL_MODE and FOREIGN_KEY_CHECKS=0 on each new connection? If it doesn’t, could an INSERT in a later chunk fail before we even reach the footer?

I’d prefer to stay close to mysqldump here. With COALESCE, I guess that's close enough, but we'd need to document why it differs from mysqldump.

…ements across connections keep the current value

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@youknowriad youknowriad changed the title Don't save session variables in exported dumps, so split imports can't restore NULL Restore session variables with COALESCE in exported dumps, so split imports keep the current value Nov 25, 2026
@youknowriad

Copy link
Copy Markdown
Contributor Author

Good call, I switched to COALESCE for both, with a comment on why the footer differs from mysqldump. On one connection it restores the original values like mysqldump, and on a new one it keeps the current values. I tested it with a real push: Jetpack's first restore method succeeded on a site where it failed yesterday on this statement.

  1. Probably yes, unless Jetpack's parser drops /*!40014 … */ statements, which I haven't checked. Jetpack's own backups keep these statements together, so it rarely sees mysqldump files.
  2. Right, later batches don't get the header's settings. In the failed restores, the rows still imported and only the footer failed, so the server's own mode was enough there. Applying the settings on every connection would be a fix for Jetpack, not for the dump.

@JanJakes
JanJakes merged commit d670da0 into main Nov 25, 2026
46 checks passed
youknowriad added a commit to Automattic/studio that referenced this pull request Nov 25, 2026
…st try (#6185)

## Related issues

- Related to Automattic/wp-cli-sqlite-command#42
- Follow-up to #6103

## How AI was used in this PR

Claude investigated the failed fvm.house push in the Jetpack Backup
restore logs, wrote the upstream fix, and ran the test pushes below. I
reviewed the change and the results.

## Proposed Changes

- Pushes from Studio builds with `wp-cli-sqlite-command` v1.1.11 (from
#6103, in 2.4.0) fail Jetpack Backup's first restore method. The dump
ends with `SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;`. Jetpack
runs a dump's statements over several connections, so the variable is
NULL by the time that statement runs, and MySQL rejects it (error 1231).
- Jetpack then restarts the restore with its slower fallback method.
That fallback usually succeeds but adds hours on large sites, and it
sometimes fails, as on fvm.house, where the push ended with "Importing
SQL dump" / "All enabled restore methods failed!".
- v1.1.12 restores the session variables with `COALESCE`, so the dump
also loads when its statements run over several connections. This PR
moves the fallback version to v1.1.12, like #6103 did for v1.1.11.
Builds already download the latest release, so this mainly keeps the
fallback in step.

## Testing Instructions

- `npm run postinstall && npm run cli:build`, then check that
`apps/cli/dist/cli/wp-files/sqlite-command/version` is `v1.1.12`.
- Push a local site to a WordPress.com test site: `node
apps/cli/dist/cli/main.mjs push --path <site> --remote-site <id>`.
- In log2logstash, the site's restore should log `DPC_RESTORE_SUCCEEDED`
with no fallback. With v1.1.11 the first method failed on the statement
above.

I did this with a WooCommerce site pushed to a WordPress.com test site,
which failed the first method with v1.1.11 the day before. The full push
(files and database) restored on the first method: 6,017 items, 0
errors, about 5 minutes end to end.

## Pre-merge Checklist

- [ ] Have you checked for TypeScript, React or other console errors?

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants