Mysql to studio sanitiser

From lightspeedwp/.github · View interactive version

Mysql to studio sanitiser is an AI agent skill from the GitHub repository lightspeedwp/.github. Make a MySQL database export (mysqldump, host backup or phpMyAdmin on a MySQL server) importable into a WordPress Studio local site, which runs on SQLite. Use when a Studio import fails with "Could not execute statement", or before importing a live or staging database into Studio. Writes a cleaned c

How to use this skill

Skill file

MySQL to Studio Sanitiser

Make a MySQL export of a WordPress database importable into a WordPress Studio

local site (SQLite through the SQLite Database Integration plugin). This skill

writes a cleaned copy beside the original and reports every change.

It packages the fixes for real failed imports:

_binary literals in WP All Import's wp_pmxi_hash table.

`DEFAULT x AS 0000... ` in ten Wordfence tables. A hand-made native

SQLite conversion of the same dump then failed on its first line,

PRAGMA foreign_keys=OFF.

When to use this skill

Error during import of ... Could not execute statement: ....

problems first.

Rules

phpMyAdmin bundled with Studio) send every statement through the SQLite

Database Integration plugin, which parses MySQL and translates it to SQLite

itself. Never convert a dump to SQLite's own dialect (PRAGMA,

AUTOINCREMENT, WITHOUT ROWID, BEGIN TRANSACTION, INSERT OR ..., replace(...,char(13)),

a sqlite3 .dump), and never write a custom converter: it fails with

Could not execute statement: PRAGMA .... The -sqlite.sql output name

means "ready for Studio", not "SQLite syntax".

<name>-sqlite.sql.

else stays byte-for-byte identical.

stops with "illegal byte sequence" and silently leaves a truncated file. Use

LC_ALL=C for sed, grep and diff, or Python in rb/wb mode, and

always compare line counts afterwards.

dump. Refer to tables, lines and counts.

Procedure

1. Prepare the input and ask the user

first (for example gunzip -k export.sql.gz). Never overwrite the archive.

replaces that site's database, so the name matters.

```bash

node agents/wp-db-migration-agent/lib/sql-tool.cjs site-urls <export.sql> --studio-site ~/Studio/<site>

node agents/wp-db-migration-agent/lib/sql-tool.cjs tables <export.sql>

```

site-urls shows the export's WordPress table prefix next to the site's

$table_prefix, siteurl and home, and the Studio site's own URL (its

custom domain from Studio's site list, such as https://<site>.local) with

a planned URL replacement and how many links it would change. tables lists every table with

its rows, size and group (WordPress core, accounts, Wordfence, plugin or

other).

Then ask these questions before running the sanitiser, one message with

the facts you found, and wait for the answers:

  1. Table prefix. Say what the export uses and what the Studio site uses.

If they differ (prefix-differs), ask: "Rename the tables to `<site

prefix> (recommended), keep <export prefix> and change $table_prefix`

in the site's wp-config.php, or another prefix?" If they already match,

say so and ask only if the user wants a different prefix. Never guess: an

import under the wrong prefix leaves WordPress unable to see the data.

  1. Local user accounts. Ask: "Keep the Studio site's own user accounts

(--keep-users), or replace them with the live site's accounts (the

default)?" Explain the trade-off:

live site's author IDs, which may match a different local user or none.

log in with a live-site account.

--keep-users only works when the imported tables end up with the Studio

site's prefix, because local roles are stored under it

(<prefix>capabilities). With --studio-site, the tool refuses a

mismatch; add --prefix <site prefix>.

  1. Other tables to leave out. Tell the user that Wordfence's tables are

left out by default (name them and their size from tables). Then ask:

"Are there any other tables to leave out?" Suggest likely candidates from

the tables output without deciding for them: large log, statistics or

cache tables (for example bad_behavior), tables of plugins that are

gone or won't run locally, and backup-plugin tables. Never suggest WordPress

core tables. Pass each answer as --exclude <name>; names may omit the

prefix and use * and ? (for example --exclude 'gf_*'). Quote

patterns with wildcards so the shell leaves them alone.

  1. Site URL replacement. Show the export's URL, the Studio site's custom

URL and the link count from the URL replacement section, then ask:

"After import, replace <export URL> (http or https, with or without

www) with <Studio URL> throughout the database?" Also ask whether any

other hosts should be replaced too, such as a CDN, a media domain or an old

domain still in content. If site-urls showed no Studio URL (the site is

not in Studio's list), get it with

studio status --path ~/Studio/<site> --format json (siteUrl) or ask

the user. Record the confirmed pairs: the replacement runs in step 5,

after import, never on the dump.

2. Run the sanitiser

In a clone of lightspeedwp/.github (Node.js 20+), run:

node agents/wp-db-migration-agent/lib/sql-tool.cjs sanitise <path/to/export.sql>

Add the options that match the user's answers from step 1, and always pass

--studio-site ~/Studio/<site> so the report confirms the prefixes match.

To change the table prefix (the user's answer to question 1), add

--prefix <prefix>. It renames every table and the

keys WordPress looks up by prefix (<prefix>user_roles in options and

<prefix>* keys in usermeta, such as <prefix>capabilities). It refuses an

export with more than one WordPress prefix (multisite) or a rename that would

collide with an existing table. The site URL is replaced afterwards on the

imported site (step 5), never by editing the dump.

The tool applies the known fixes, verifies the result, and prints the edit

table, findings and site URLs. Exit code 0 means done, 1 means there are

findings to review (the output is still written), and 2 means an error (no

output is left behind).

If the tool is not available, apply the known fixes by hand (step 3), then

verify (step 4).

3. Known fixes (what the tool does)

  1. Delete the GTID replication lines. These only exist on a MySQL server

with replication on:

  1. Delete the binary-log lines:
  1. Convert every _binary '...' literal to a hex literal X'<hex>'.

Decode the MySQL escapes first: \0→0x00, \b→0x08, \n→0x0A,

\r→0x0D, \t→0x09, \Z→0x1A. A backslash followed by any other byte

means that byte, which covers \\, \' and \". Check that each decoded

value's length matches its column (for example binary(16) → 16 bytes) and

that primary-key values stay unique. If any length disagrees, stop and write

nothing: a wrong decode corrupts data silently.

  1. Rewrite MariaDB's broken binary defaults. MariaDB 11+ dumps a binary

column's default as `DEFAULT x AS <hex> `, which no parser accepts

(Studio, MySQL or MariaDB). Rewrite it as DEFAULT X'<hex>', checking the

byte count matches binary(N). Seen on Wordfence tables (wfblocks7,

wfcrawlers, wffilemods, wflocs and others).

  1. Add DROP TABLE IF EXISTS before each CREATE TABLE that has none.

phpMyAdmin exports often leave it out. Every Studio site already has

WordPress's own tables, so without it the import stops at the first clash

(for example Could not execute statement: CREATE TABLE wp_commentmeta).

mysqldump adds these by default.

Tables left out by default

Wordfence (<prefix>wf* and <prefix>wfls_*, such as wfconfig, wfhits

and wfls_settings): its logs, scan results and blocks are not needed locally,

and we don't run Wordfence on Studio sites. The tool reports each table left

out. Wordfence's settings in the options table stay, so keep the plugin

deactivated locally (`studio wp --path ~/Studio/<site> plugin deactivate

wordfence). Pass --keep-wordfence` only if the user asks for its tables.

The tool refuses (exit 2, nothing written) a file that is already in SQLite's

own dialect. Go back to the original MySQL export and sanitise that.

Leave these alone; the importer handles them: the /*!40101 SET ... */ header

and footer lines, LOCK TABLES, DISABLE KEYS, ENGINE=InnoDB, character

set and collation clauses (including MariaDB-only ones such as

utf8mb4_general1400_as_ci), ALTER TABLE ... ADD PRIMARY KEY / `MODIFY ...

AUTO_INCREMENT blocks from phpMyAdmin, and ON UPDATE CURRENT_TIMESTAMP`.

4. Scan and verify

Report, before changing, anything else SQLite is likely to reject: other

SET @@GLOBAL statements, CREATE DATABASE / USE, DELIMITER,

DEFINER=, triggers, stored procedures or functions, views, events,

FULLTEXT / SPATIAL indexes and generated columns.

Then verify and report:

showing only the intended lines changed;

must match);

DEFAULT x AS;

5. Report

End with a table of every edit:

| # | Edit | Line | Why |

| --- | -------------------------------------- | ---- | ------------------------------------- |

| 1 | Removed SET @@GLOBAL.GTID_PURGED=... | 24 | Replication setting SQLite cannot run |

Then tell the user:

studio import <file> --path ~/Studio/<site>. Importing replaces the site's

database, so confirm the site first.

CODE0 skill with the pairs

confirmed in step 1, question 4. Start with the preview command from the

URL replacement section of the report (it has --dry-run), show the

per-table counts, and apply only after the user confirms. Studio may set

siteurl and home to the local URL on import, but links in posts,

postmeta, options and plugin tables still point at the source until this

runs. If the user said no to question 4, say which links still point at the

source.

local site's accounts; log in with an account from the source site.

6. If the import still fails

Ask the user for the Studio error log (~/Library/Logs/Studio/). Check the

[CLI - <id>] on the error line and the theme path logged beside it to confirm

the import went to the intended site. If the

statement it names is PRAGMA, BEGIN TRANSACTION or similar SQLite syntax,

the file was converted to the wrong dialect: sanitise the original MySQL export

instead of patching the converted file. Find the

Could not execute statement: line, identify the statement it names, explain

why SQLite rejects it, and propose a targeted fix. Apply it only after the user

agrees, verify again (step 4), and add it to the edit table. If the fix is

generic, suggest adding it to this skill's known fixes.

Prevention

When the user controls the export, these mysqldump options avoid both known

problems at the source:

mysqldump --set-gtid-purged=OFF --hex-blob <database> > export.sql

_Built by 🧱 LightSpeedWP with ☕, 🚀, and open-source spirit!_

Contributors

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.