Repository navigation
Conversation
CybotTM
left a comment
There was a problem hiding this comment.
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:
- Unit test: keep only the JSON case. The
plain textcase passes with and without the fix. Thedatabase datetimecase fails without the fix only because a unit test uses the DoctrineDateTimeType. In a TYPO3 run,ConnectionPoolreplacesdatetimewith the TYPO3DateTimeType, which keeps strings unchanged. The functional test shows the same result: thecreated_atcolumn comes back unchanged onmaintoo. - Description: the sentence "Other types can likewise receive values already converted" is not true for
datetimein TYPO3. CONTRIBUTING.rst also asks for an issue link, steps to reproduce and the results of the Core test runs. - Backport to branch 9: branch 9 has the same
lookupName()andbulkInsert()code. TYPO3 13.4 requires^9.2.1and TYPO3 14.3 requires^9.7.0. Recent bugfixes, for example 480a74d, went tomain, 9. - CI: the CI of this repository does not run the functional test, because
ci.ymlruns only-s unit. A manual step is acceptable, but please state it in the description.
CybotTM
left a comment
There was a problem hiding this comment.
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.
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>
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>
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
left a comment
There was a problem hiding this comment.
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.
-
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. -
introspectTables()instead oflistTables(). Doctrine DBAL 4.4 deprecatedAbstractSchemaManager::listTables()(andTable::getName()) in favour ofintrospectTables()andgetObjectName(). Since this PR touchesDatabaseAccessoranyway, what do you think about switchingexport()as well? It has to stay conditional, because TYPO3 v14.0 to v14.2 still allow Doctrine DBAL 4.3, which has nointrospectTables()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 begetUnqualifiedName()->getValue(). No platform returned a schema qualifier.introspectTables()bypasses the TYPO3 schema managers, so some column types differ fromlistTables():guidbecomesstringon MySQL, MariaDB and SQLite, andjsonbecomestexton 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
introspectTablesinstead oflistTables. - 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,phpstanandunitof this repo are green.
Fine with me in this PR or as a separate follow-up, your call.
-
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, andci.ymlonly 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/isexport-ignored and the package installed by composer has noTests/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. -
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.
-
Commit message footer. Please use
Releases: main, 10. If you're fine with backports, we can add9and8: branch9has the same code (v13/v14), and so does branch8, which is affected on v13 since that runs Doctrine DBAL 4. The backports would go as separate PRs against9and8after this one is merged. Branch8still supports v12 with Doctrine DBAL 3, so the fallback has to stay there, see the inline comment. With item 2, the9and8ports also need a guard forgetObjectName(), since TYPO3 v13.4.0 allows Doctrine DBAL 4.2 and v12 runs Doctrine DBAL 3. -
Description. As @CybotTM wrote, "Other types can likewise receive values already converted" doesn't hold in TYPO3.
ConnectionPooloverridesdate,datetimeandtimewith 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, asCONTRIBUTING.rstasks? -
CONTRIBUTING.rstand 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!
| 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, |
There was a problem hiding this comment.
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:
| 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 |
There was a problem hiding this comment.
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.
| // @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(); |
There was a problem hiding this comment.
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:
| // @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)); |
There was a problem hiding this comment.
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
5856314 to
b1bf4a9
Compare
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
In a TYPO3 Core checkout, use a Git clone of this PR branch at vendor/typo3/testing-framework. Composer's distribution archive omits Tests/.
Run the round-trip regression from the Core checkout:
./Build/Scripts/runTests.sh -s functional -d sqlite --
vendor/typo3/testing-framework/Tests/Functional/
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