You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c156cf1
Browse filesBrowse the repository at this point in the historyBrowse files
Literal defaults on `BINARY` and `VARBINARY` columns are stored as
written and then treated as text, which breaks them in two ways:
- **Exports don't import.** `SHOW CREATE TABLE` turns `x'00…'` into the
string `'x''00…'''`, and MySQL and MariaDB reject it with `Invalid
default value`. A shorter value can load but then stores the text
`0xDEADBEEF` rather than those bytes. This is blocking a WordPress
Studio user from pushing a site with Wordfence tables.
- **Inserts fail.** An insert that omits such a column fails with
`cannot store TEXT value in BLOB column`, whatever form the default
takes, including `''` and `'\0…'`. See the [comment on
#551](#551 (comment)).
**This change handles these defaults as bytes, following the approach
suggested on #551.**
- Defaults are read from the parsed literal, so the text `'0x41'` stays
distinct from the hex literal `0x41`, and stored in hex as `0x41`, like
MySQL's `INFORMATION_SCHEMA`.
- They are written to SQLite as blobs, so inserts work.
- `SHOW CREATE TABLE` matches MySQL: a quoted string such as `'\0\0'`
for valid UTF-8, and `0xDEADBEEF` otherwise.
- Rebuilding missing table metadata reads these defaults back as the
same bytes.
**Existing tables are migrated when the driver is upgraded.** Older
versions stored these defaults as written (`x'…'`, `0x…`, `b'…'`, `0b…`,
text and raw bytes), and they gave the SQLite tables text defaults. The
upgrade stores these defaults in hex and rebuilds the affected tables,
including tables with binary expression defaults, so their exports and
inserts work.
**Three neighbouring bugs are fixed as well, since they'd otherwise
undermine this one:**
- **Charset prefixes:** `_binary'abc'` and `N'abc'` kept their prefix in
the default, on every column type. With binary defaults now working,
inserts would have silently stored `_binaryabc`. MySQL stores `abc`, and
now so does the driver.
- **National strings:** `N'abc'` in a query failed with `no such column:
N` or a syntax error. It now works like `'abc'`, and its column is named
`abc`, as in MySQL.
- **Generated defaults:** `DEFAULT ('abc')` on a `BINARY`, `VARBINARY`
or `BLOB` column failed on insert with the same `cannot store TEXT value
in BLOB column` error. These are now cast to a blob.
This only covers defaults on binary columns and these cases, so it's
related to #551 but doesn't close it.
## Limitations
- **Older stored text:** a text default stored by an older version that
looks like a literal, such as `x'41'`, can't be told apart from the hex
literal, so the migration reads it as hex.
- **Existing rows:** the migration doesn't change them. Only the unsafe
mode for SQLite < 3.37, which doesn't check types, can have rows that
got the old text defaults.
- **Migration timing:** the migration runs when the driver version
changes. A build of this branch before the next version bump handles old
defaults as before.
- **`BINARY` padding:** defaults aren't padded to the column length,
which matches how the driver stores other `BINARY` values. MySQL shows
`'A\0\0\0'` for `BINARY(4) DEFAULT 'A'` where this shows `'A'`, and the
padding is applied when that's imported into MySQL.
- **Zero bytes:** `INFORMATION_SCHEMA` keeps the full value, while MySQL
cuts it at the first zero byte, such as `0x41` for `'A\0'` and `0x` for
`'\0\0'`.
## API changes
- `INFORMATION_SCHEMA.COLUMNS` and `SHOW COLUMNS` now report these
defaults in hex.
- `WP_SQLite_Information_Schema_Builder` gains a public static
`get_binary_default_bytes()`, which the migration uses to read the forms
older versions stored.
- `WP_MySQL_Token::get_value()` now unquotes `NCHAR_TEXT` tokens, in
both `mysql-on-sqlite` and `mysql-parser`.
## Testing
- Added tests for each literal form, `ALTER COLUMN … SET DEFAULT`, the
migration of stored forms from older versions, metadata recovery,
charset prefixes, national strings and generated defaults.
- I compared the expected output with real MySQL 8.4 and MariaDB 10.11,
and loaded the patched `SHOW CREATE TABLE` output into both. MariaDB
also needed the `utf8mb4_0900_ai_ci` collation swapped, which is a
separate known issue. Default bytes were exact on both.
- With an earlier version of this change, which read the old forms
directly instead of migrating them, real Wordfence 9.0.2 tables created
with hex defaults by the old driver exported correctly end to end
through WordPress Studio. The full dump restored on both servers.
`INSERT … VALUES (…, DEFAULT)` fails with a syntax error for every
column type on `trunk`, including `INT`. That's separate, so it isn't
covered here. So is `DEFAULT (_utf8mb4'abc')`, the form MySQL 8 prints
for expression defaults, which fails at `CREATE TABLE` with a syntax
error for every string column type I tried, on `trunk` too.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
- **Bug Fixes**
- Improved handling of binary-column defaults, including hexadecimal,
bit-pattern, and expression-based defaults. Values display consistently
in table definitions and column metadata.
- Binary defaults with valid UTF-8 bytes display as text; other byte
values remain in hexadecimal. Defaults from older versions are handled
correctly, and affected existing databases are updated.
- National string literals are decoded consistently in queries and
defaults, including when their contents resemble SQL syntax.
- **Tests**
- Added coverage for binary defaults, default changes, database
migrations, and national string literals.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jan Jakeš <jan@jakes.pro>
0 commit comments