Skip to content

[BUGFIX] Preserve raw strings in database snapshots - #756

Open
wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:fix/json-snapshot-restoration
Open

wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:fix/json-snapshot-restoration

Conversation

@wouter90

@wouter90 wouter90 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Database snapshots export raw database values. Since #530, a snapshot on Doctrine DBAL 4 can store the Doctrine JSON type name. During restore, bulkInsert() uses that type to encode the raw JSON again. JSON objects, arrays, and scalars can become JSON strings.

Export string-bound columns as the plain string type so restore binds the raw values directly. Other columns retain their Doctrine type names.

Reproduce and verify

  1. In a TYPO3 Core checkout, use a Git clone of this PR branch at vendor/typo3/testing-framework. Composer's distribution archive omits Tests/.

  2. Run the round-trip regression from the Core checkout:

    ./Build/Scripts/runTests.sh -s functional -d sqlite --
    vendor/typo3/testing-framework/Tests/Functional/

  3. To reproduce the bug, use the parent commit's DatabaseAccessor.php while keeping the new test. The restore assertion fails on double-encoded JSON. Restore the PR version of the file and rerun to verify the fix.

The framework's own CI runs the unit suite. The functional regression is run manually from Core.

Verification

  • TYPO3 Core main (32b101bc881), PHP 8.5. The unit suite completed with 13,280 tests and one skip.
  • Core functional tests that call withDatabaseSnapshot(). All 1,252 passed on SQLite.
  • Framework regression from Core. Passed on SQLite, PostgreSQL, MariaDB, and MySQL. Failed on SQLite with the parent version of DatabaseAccessor.php, as expected.
  • Testing framework. All 132 unit tests, PHP lint, and the coding standards check passed. PHPStan reported the same unmatched baseline entry in Testbase.php before and after the review changes

@CybotTM CybotTM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I tested the fix on TYPO3 Core main. The change to DatabaseAccessor does what it should. The new functional test fails without the fix and passes with it on SQLite, PostgreSQL, MariaDB and MySQL. Without the fix, only the JSON column differs: the restore encodes every value a second time, so 42 becomes "42".

I approve. These points are suggestions:

  1. Unit test: keep only the JSON case. The plain text case passes with and without the fix. The database datetime case fails without the fix only because a unit test uses the Doctrine DateTimeType. In a TYPO3 run, ConnectionPool replaces datetime with the TYPO3 DateTimeType, which keeps strings unchanged. The functional test shows the same result: the created_at column comes back unchanged on main too.
  2. Description: the sentence "Other types can likewise receive values already converted" is not true for datetime in TYPO3. CONTRIBUTING.rst also asks for an issue link, steps to reproduce and the results of the Core test runs.
  3. Backport to branch 9: branch 9 has the same lookupName() and bulkInsert() code. TYPO3 13.4 requires ^9.2.1 and TYPO3 14.3 requires ^9.7.0. Recent bugfixes, for example 480a74d, went to main, 9.
  4. CI: the CI of this repository does not run the functional test, because ci.yml runs only -s unit. A manual step is acceptable, but please state it in the description.

Comment thread CONTRIBUTING.rst Resolved

@CybotTM CybotTM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Follow-up to my review above. If you prefer to keep all three unit test cases, use the inline suggestion below instead of my earlier suggestion to delete two of them.

CybotTM added a commit to netresearch/typo3-testing-skill that referenced this pull request Nov 15, 2026
Core installs typo3/testing-framework as the GitHub zip archive, without
.git and, because of export-ignore, without Tests/. A change to the
framework therefore needs the git clone that the framework's
CONTRIBUTING.rst describes.

Describe that setup and what it adds:

- check the clone against the installed copy,
- run Core's own suites,
- run the functional tests that a pull request adds, if any,
- run the same suites without the fix.

Step 5 writes the base
version into the clone, because a redirect resolves against the
current directory.

Also name a trap from TYPO3/testing-framework#756. A worktree with a
linked .Build directory loads the classes of the other worktree. Both
arms of the A/B run were then green.

Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
CybotTM added a commit to netresearch/typo3-testing-skill that referenced this pull request Nov 15, 2026
Core installs typo3/testing-framework as the GitHub zip archive, without
.git and, because of export-ignore, without Tests/. A change to the
framework therefore needs the git clone that the framework's
CONTRIBUTING.rst describes.

Describe that setup and what it adds:

- check the clone against the installed copy,
- run Core's own suites,
- run the functional tests that a pull request adds, if any,
- run the same suites without the fix.

Step 5 writes the base
version into the clone, because a redirect resolves against the
current directory.

Also name a trap from TYPO3/testing-framework#756. A worktree with a
linked .Build directory loads the classes of the other worktree. Both
arms of the A/B run were then green.

Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
CybotTM added a commit to netresearch/typo3-testing-skill that referenced this pull request Nov 15, 2026
Core installs typo3/testing-framework as the GitHub zip archive, without
.git and, because of export-ignore, without Tests/. A change to the
framework therefore needs the git clone that the framework's
CONTRIBUTING.rst describes.

Describe that setup and what it adds:

- check the clone against the installed copy,
- run Core's own suites,
- run the functional tests that a pull request adds, if any,
- run the same suites without the fix.

Step 5 writes the base
version into the clone, because a redirect resolves against the
current directory.

Also name a trap from TYPO3/testing-framework#756. A worktree with a
linked .Build directory loads the classes of the other worktree. Both
arms of the A/B run were then green.

Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>

@sbuerk sbuerk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @wouter90 for tracking this down and for the fix, and thanks @CybotTM for the thorough review and the test runs on all four databases.

The change to DatabaseAccessor does the right thing. On Doctrine DBAL 4 the export stores the Doctrine type name, and bulkInsert() converts the raw database value with it again, which double-encodes JSON. Binding string columns as string skips that second conversion without changing the bound parameter type.

Some background I found while reviewing: prepareColumns() was designed to export the binding type (#97), so the import binds the raw values without any conversion. #530 switched to Type::lookupName() for Doctrine DBAL 4, because the ParameterType enum can't be JSON-encoded. That is where the regression comes from, and it also left $bindingType assigned but never used.

I verified the change against TYPO3 Core main (v15), with the PR cloned into vendor/typo3/testing-framework:

Run Result
runTests.sh -s functional -d sqlite -- vendor/typo3/testing-framework/Tests/Functional/ OK
same without the fix fails, 10 of 11 JSON rows double-encoded, SQL NULL row unchanged
-d postgres with / without the fix OK / fails
-d mariadb with the fix OK
this repo: cgl -n, lint, phpstan, unit (PHP 8.2, core 14.3) green
unit DatabaseAccessorTest without the fix 3 of 4 fail as intended

I have a few suggestions before this can go in. Please let me know what you think about them, @wouter90. If you see it differently on any point, I'm happy to discuss.

  1. Use the binding type in the return. Instead of the additional early return, the binding type can decide the return value directly, and the Doctrine DBAL 3 fallback is dead code on main. Details and a suggestion are inline.

  2. introspectTables() instead of listTables(). Doctrine DBAL 4.4 deprecated AbstractSchemaManager::listTables() (and Table::getName()) in favour of introspectTables() and getObjectName(). Since this PR touches DatabaseAccessor anyway, what do you think about switching export() as well? It has to stay conditional, because TYPO3 v14.0 to v14.2 still allow Doctrine DBAL 4.3, which has no introspectTables() yet:

    $schemaManager = $this->connection->createSchemaManager();
    // Doctrine DBAL 4.4 deprecated listTables(), TYPO3 v14.0 to v14.2 still allow Doctrine DBAL 4.3.
    // @todo Use introspectTables() unconditionally once Doctrine DBAL 4.4 is the minimum.
    // @phpstan-ignore function.alreadyNarrowedType (Doctrine DBAL < 4.4)
    $tables = method_exists($schemaManager, 'introspectTables')
        ? $schemaManager->introspectTables()
        : $schemaManager->listTables();
    $export = [];
    foreach ($tables as $table) {
        $tableName = $table->getObjectName()->getUnqualifiedName()->getValue();

    A few things I checked before suggesting it:

    • getObjectName()->toString() returns the quoted name ("pages"), so it has to be getUnqualifiedName()->getValue(). No platform returned a schema qualifier.
    • introspectTables() bypasses the TYPO3 schema managers, so some column types differ from listTables(): guid becomes string on MySQL, MariaDB and SQLite, and json becomes text on SQLite. All of them are string-bound, so with the binding type deciding the export (item 1) the snapshot is identical.
    • The unit test then needs to stub introspectTables instead of listTables.
    • With that change, the functional test of this PR passes on SQLite, PostgreSQL, MariaDB and MySQL, and the 28 Core functional test classes using withDatabaseSnapshot() (971 tests) pass on PostgreSQL and MariaDB. cgl, lint, phpstan and unit of this repo are green.

    Fine with me in this PR or as a separate follow-up, your call.

  3. Functional test and CI. The functional test can't run inside this repository. There is no functional phpunit config, no bootstrap and no database container in runTests.sh, and ci.yml only runs -s unit. Running it in-repo just skips it ("Functional tests must be called through phpunit on CLI"). From a Core checkout it only works with a git clone of the testing framework, because /Tests/ is export-ignored and the package installed by composer has no Tests/ folder. I'm fine with keeping it as a manual test for now. Could you mention that in the description? I'll open a follow-up issue for a CI job that runs it against a Core checkout.

  4. One commit. Could you squash the three commits into one? The repository squash setting uses the commit messages, so the merge would otherwise concatenate all three bodies. The body of the first commit is also outdated after the last one, the unit test now asserts the exported type names and no longer performs conversions.

  5. Commit message footer. Please use Releases: main, 10. If you're fine with backports, we can add 9 and 8: branch 9 has the same code (v13/v14), and so does branch 8, which is affected on v13 since that runs Doctrine DBAL 4. The backports would go as separate PRs against 9 and 8 after this one is merged. Branch 8 still supports v12 with Doctrine DBAL 3, so the fallback has to stay there, see the inline comment. With item 2, the 9 and 8 ports also need a guard for getObjectName(), since TYPO3 v13.4.0 allows Doctrine DBAL 4.2 and v12 runs Doctrine DBAL 3.

  6. Description. As @CybotTM wrote, "Other types can likewise receive values already converted" doesn't hold in TYPO3. ConnectionPool overrides date, datetime and time with types that pass strings through unchanged, so JSON is the only type with a visible effect. Could you narrow that sentence and add the reproduction steps and the Core test results, as CONTRIBUTING.rst asks?

  7. CONTRIBUTING.rst and a few smaller points are inline.

A possible commit message for items 1, 4 and 5, adjust as you like (it needs one more paragraph if item 2 goes into this PR):

[BUGFIX] Preserve raw strings in database snapshots

DatabaseAccessor::export() reads rows as raw database values and was
meant to export the binding type of each column, so the import binds
them unchanged. With ba5af3f the export switched to the Doctrine type
name for Doctrine DBAL 4, since the ParameterType enum cannot be JSON
encoded. On import, bulkInsert() now converts the raw values with that
type again. For JSON columns this encodes the already encoded JSON a
second time, so objects, arrays and scalars are restored as JSON
strings.

The binding type now decides the exported type again. String-bound
columns are exported with the string type, which binds the raw value
without a conversion. Other binding types keep their type name. The
Doctrine DBAL 3 fallback is removed, TYPO3 v13 and newer require
Doctrine DBAL 4.

The unit test asserts the exported column types. A functional test
covers the export and import round trip. It cannot run inside this
repository and is executed from a TYPO3 Core checkout, as documented
in CONTRIBUTING.rst.

Releases: main, 10, 9, 8

Thanks again for the work on this!

Comment thread CONTRIBUTING.rst Outdated
Comment on lines 81 to 91
Run the framework's own functional regression tests from that Core
checkout as well. They test the snapshot export and import of
``DatabaseAccessor`` and run on every database, SQLite included:

.. code-block:: bash

./Build/Scripts/runTests.sh -s functional -d sqlite -- \
vendor/typo3/testing-framework/Tests/Functional/

Confirm that the Core test runs have the same result as without the
changes,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The command works, I ran it on SQLite, PostgreSQL and MariaDB. It only finds the tests with a git clone of the testing framework in vendor/, though. /Tests/ is export-ignored, so the package installed by composer has no Tests/ folder. I think the example should contain the commands for that, so it can be copied as is. It clones the fork with the change, same as "Getting started", step 3. Naming DatabaseAccessor here would also go stale with the next functional test. And I'd keep the original "and confirming that ..." so the numbered list still reads as one sentence. Maybe like this:

Suggested change
Run the framework's own functional regression tests from that Core
checkout as well. They test the snapshot export and import of
``DatabaseAccessor`` and run on every database, SQLite included:
.. code-block:: bash
./Build/Scripts/runTests.sh -s functional -d sqlite -- \
vendor/typo3/testing-framework/Tests/Functional/
Confirm that the Core test runs have the same result as without the
changes,
* testing-framework functional tests. They need a git clone of the
testing framework in ``vendor/typo3/testing-framework``, the package
installed by composer does not contain the ``Tests/`` folder:
.. code-block:: bash
rm -rf vendor/typo3/testing-framework && \
git clone git@github.com:[YOUR_USERNAME]/testing-framework.git \
vendor/typo3/testing-framework && \
./Build/Scripts/runTests.sh -s functional -d sqlite -- \
vendor/typo3/testing-framework/Tests/Functional/
and confirming that the test runs have the same result as without the
changes,

use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Snapshot\DatabaseAccessor;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class DatabaseAccessorTest extends FunctionalTestCase

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is the first functional test in this repository, and nothing here runs it: Build/phpunit/ only has the unit test config, runTests.sh has no functional suite and ci.yml only runs -s unit. Only cgl and phpstan see this file. Fine for now as a manual test (see the review summary), I'll create a follow-up issue for a CI job against a Core checkout.

Comment on lines 118 to 127
// @todo Simplify this after Doctine DBAL v3 support can be dropped.
$type = $table->getColumn($columnName)->getType();
$bindingType = $type->getBindingType();
// Snapshot strings already contain database values, including encoded JSON.
if ($bindingType === ParameterType::STRING) {
return Types::STRING;
}
return (enum_exists(ParameterType::class))
? Type::lookupName($type)
: $type->getBindingType();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Originally this method exported the binding type (#97). #530 switched to Type::lookupName() for Doctrine DBAL 4 and left $bindingType unused, which is where the double conversion comes from. So I'd let the binding type decide the return directly, instead of adding an early return in front of the old ternary, which still calls getBindingType() a second time.

On main the Doctrine DBAL 3 fallback can go as well. All TYPO3 versions supported here (v14, v15) require Doctrine DBAL 4, so enum_exists(ParameterType::class) is always true. That also resolves the @todo, and the long DBAL 3/4 comment can describe the actual decision. GitHub only lets me suggest on the changed lines, so the six comment lines above the @todo (from "Doctrine DBAL v4 converted ..." to the @see line) would need to be removed by hand as well:

Suggested change
// @todo Simplify this after Doctine DBAL v3 support can be dropped.
$type = $table->getColumn($columnName)->getType();
$bindingType = $type->getBindingType();
// Snapshot strings already contain database values, including encoded JSON.
if ($bindingType === ParameterType::STRING) {
return Types::STRING;
}
return (enum_exists(ParameterType::class))
? Type::lookupName($type)
: $type->getBindingType();
// Rows are exported as raw database values. Doctrine types of string-bound columns would
// convert them a second time on import (JsonType encodes the JSON again), so these columns are
// exported as plain `string` type. `ParameterType` is a non-backed enum and cannot be
// json_encoded, therefore other binding types export the Doctrine type name.
$type = $table->getColumn($columnName)->getType();
return $type->getBindingType() === ParameterType::STRING
? Types::STRING
: Type::lookupName($type);

It behaves the same as your version on Doctrine DBAL 4. With it, cgl, lint, phpstan and unit here and the functional test from Core main on SQLite and PostgreSQL are green.

For the backport to 8 (v12 runs Doctrine DBAL 3), the fallback has to stay. There the same idea would look like this:

$type = $table->getColumn($columnName)->getType();
$bindingType = $type->getBindingType();
return match (true) {
    $bindingType === ParameterType::STRING => Types::STRING,
    enum_exists(ParameterType::class) => Type::lookupName($type),
    default => $bindingType,
};

$result = self::createStub(Result::class);
$result->method('fetchAllAssociative')->willReturn([['payload' => $value]]);
$queryBuilder = self::createStub(QueryBuilder::class);
$queryBuilder->method('getRestrictions')->willReturn(self::createStub(DefaultRestrictionContainer::class));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: getRestrictions() is declared to return QueryRestrictionContainerInterface. Stubbing the interface keeps the test independent from DefaultRestrictionContainer, the class that caused the 14.3 trouble in the first place:

use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
// ...
$queryBuilder->method('getRestrictions')->willReturn(self::createStub(QueryRestrictionContainerInterface::class));

(plus removing the DefaultRestrictionContainer import, otherwise cgl complains)

DatabaseAccessor exports raw database values. When a snapshot stores
the Doctrine JSON type name, bulkInsert() encodes the raw JSON again
during restore. Objects, arrays and scalars become JSON strings.

Store the plain string type for string-bound columns so the import
binds raw values directly. Keep Doctrine type names for other columns
because DBAL 4 binding types are enums that cannot be JSON-encoded.

Assert the exported types in a unit test and cover the JSON round trip
with a functional test run from a TYPO3 Core checkout.

Releases: main, 10
@wouter90
wouter90 force-pushed the fix/json-snapshot-restoration branch from 5856314 to b1bf4a9 Compare November 25, 2026 17:58
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.

3 participants