Skip to content

[4.x] Keep special characters, binary data and NULL in export/import round trips - #406

Open
Hackwar wants to merge 2 commits into
joomla-framework:4.x-devfrom
Hackwar:4.x-export-import-roundtrip
Open

Hackwar wants to merge 2 commits into
joomla-framework:4.x-devfrom
Hackwar:4.x-export-import-roundtrip

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Exporting a table and importing the file again changed or lost data:

  1. Unescaped structure attributes. MysqlExporter, MysqliExporter and PgsqlExporter concatenated column types, defaults, comments and key definitions into XML attributes without escaping. A default such as a"b or x&y produced a document that could not be parsed. All attributes now go through a new DatabaseExporter::escapeXml() (htmlspecialchars() with ENT_QUOTES | ENT_XML1). The PostgreSQL key Query attribute is now double-quoted and escaped like the others (it was single-quoted and unescaped).
  2. One-way blob encoding. DatabaseExporter base64-encoded mediumblob columns only, and DatabaseImporter::importData() never decoded anything, so blobs were imported as base64 text. Now all binary column types (tinyblob, blob, mediumblob, longblob, binary(n), varbinary(n)) are encoded, the field is marked encoding="base64", and the importer decodes marked fields. NULL in a binary column is exported as NULL instead of an empty string.
  3. PostgreSQL NULL and bytea. PgsqlExporter called htmlspecialchars($value) on NULL values (a deprecation since PHP 8.1, and NULL became '') and wrote bytea values with stream_get_contents() as raw bytes into the XML, which is invalid for non-text bytes. NULL is now written with value_is_null="1" like the base exporter, and bytea is base64 encoded and marked. On import, PgsqlDriver::sqlValue() renders bytea values with quoteBinary() (decode('…', 'hex')), because a binary string cannot be inserted as a text literal.

Old exports stay importable: fields without encoding are imported as before.

Depends on #379 (well-formed value_is_null="1"), which changes a line next to the blob handling; this branch is based on it, so the diff shows that commit until #379 is merged. The trust question for imported structure (types/defaults ending up in DDL) is handled separately.

Testing Instructions

  • MysqliExporterTest::testRoundTripWithSpecialCharactersAndBinaryData exports a column with default a"b&c<d and a blob value containing NUL, \xFF, quotes and <&>, parses the XML, imports it with MysqliImporter into a stub and compares the values. It fails before the change (the XML does not parse).
  • PgsqlExporterTest::testNullAndByteaValues exports a NULL and a bytea stream; before the change the XML does not parse.

Both need no database (vendor/bin/phpunit Tests/Mysqli/MysqliExporterTest.php Tests/Pgsql/PgsqlExporterTest.php). PgsqlImporterTest has one failure on Windows that is unrelated (it compares \r\n against \n) and fails on 4.x-dev too.

Documentation Changes Required

None on 4.x-dev. #373's docs/schema-tools.md → "Exporting"/"Importing" does not describe the XML format; nothing there becomes wrong.

🤖 Generated with Claude Code

Hackwar and others added 2 commits November 12, 2026 22:55
DatabaseExporter wrote NULL values as <field name="x" value_is_null></field>.
An attribute without a value is not well-formed XML, so any export of a table
containing NULL could not be read back by DatabaseImporter or the import
command. The attribute now has a value; the importer only checks isset().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ta and NULL

- The structure attributes written by the MySQL, MySQLi and PostgreSQL
  exporters (types, defaults, comments, key queries) were not escaped, so a
  default such as a"b or x&y broke the XML.
- Only mediumblob columns were base64 encoded, and the importer never
  decoded them, so blobs came back as base64 text. All binary column types
  are now encoded and marked with encoding="base64", which the importer
  decodes.
- PgsqlExporter passed NULL to htmlspecialchars() (deprecated, exported as
  an empty string) and wrote bytea values as raw bytes into the XML. NULL is
  now marked with value_is_null, bytea is base64 encoded, and PgsqlDriver
  inserts bytea values with quoteBinary().

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.

1 participant