-
Notifications
You must be signed in to change notification settings - Fork 26
Tame doctrine migrations / determine which we want to apply. #1917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/OpenConext/EngineBlockBundle/Doctrine/Type/LegacyJsonType.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright 2026 SURFnet B.V. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace OpenConext\EngineBlockBundle\Doctrine\Type; | ||
|
|
||
| use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
| use Doctrine\DBAL\Types\Exception\SerializationFailed; | ||
| use Doctrine\DBAL\Types\Exception\ValueNotConvertible; | ||
| use Doctrine\DBAL\Types\Type; | ||
| use JsonException; | ||
|
|
||
| use function is_resource; | ||
| use function json_decode; | ||
| use function json_encode; | ||
| use function stream_get_contents; | ||
|
|
||
| use const JSON_PRESERVE_ZERO_FRACTION; | ||
| use const JSON_THROW_ON_ERROR; | ||
|
|
||
| /** | ||
| * This type stores JSON data in a longtext column, deliberately avoiding migration to a native MySQL JSON column type. | ||
| * The native JSON column type (declared via getJsonTypeDeclarationSQL) was introduced when upgrading to doctrine/dbal:^4, | ||
| * but migrating existing longtext columns to JSON columns is not desirable at this time. | ||
| * | ||
| * Use this type instead of Doctrine\DBAL\Types\Types::JSON for columns that should remain as longtext. | ||
| * | ||
| * Note: Doctrine column definition data passed to the SQL declaration are not taken into account to | ||
| * ensure a consistent schema. | ||
| */ | ||
| class LegacyJsonType extends Type | ||
johanib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public const NAME = 'legacy_json'; | ||
|
|
||
| public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
| { | ||
| return $platform->getClobTypeDeclarationSQL( | ||
| ['length' => null] | ||
| ); | ||
| } | ||
|
|
||
| public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string | ||
| { | ||
| if ($value === null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return json_encode($value, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION); | ||
| } catch (JsonException $e) { | ||
| throw SerializationFailed::new($value, 'json', $e->getMessage(), $e); | ||
| } | ||
| } | ||
|
|
||
| public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed | ||
| { | ||
| if ($value === null || $value === '') { | ||
| return null; | ||
| } | ||
|
|
||
| if (is_resource($value)) { | ||
| $value = stream_get_contents($value); | ||
| } | ||
|
|
||
| try { | ||
| return json_decode($value, true, 512, JSON_THROW_ON_ERROR); | ||
| } catch (JsonException $e) { | ||
| throw ValueNotConvertible::new($value, self::NAME, $e->getMessage(), $e); | ||
| } | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return self::NAME; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
tests/unit/OpenConext/EngineBlockBundle/Doctrine/Type/LegacyJsonTypeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright 2026 SURFnet B.V. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace OpenConext\EngineBlockBundle\Doctrine\Type; | ||
|
|
||
| use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
| use Doctrine\DBAL\Types\ConversionException; | ||
| use Doctrine\DBAL\Types\Type; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class LegacyJsonTypeTest extends TestCase | ||
| { | ||
| private MySQLPlatform $platform; | ||
|
|
||
| public static function setUpBeforeClass(): void | ||
| { | ||
| if (!Type::hasType(LegacyJsonType::NAME)) { | ||
| Type::addType(LegacyJsonType::NAME, LegacyJsonType::class); | ||
| } | ||
| } | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| $this->platform = new MySQLPlatform(); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function null_converts_to_null_in_database(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $result = $type->convertToDatabaseValue(null, $this->platform); | ||
|
|
||
| $this->assertNull($result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function a_value_round_trips_correctly(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
| $input = ['foo' => 'bar', 'baz' => [1, 2, 3]]; | ||
|
|
||
| $dbValue = $type->convertToDatabaseValue($input, $this->platform); | ||
| $phpValue = $type->convertToPHPValue($dbValue, $this->platform); | ||
|
|
||
| $this->assertSame($input, $phpValue); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function a_scalar_value_is_encoded_to_json(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $result = $type->convertToDatabaseValue('hello', $this->platform); | ||
|
|
||
| $this->assertSame('"hello"', $result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function a_float_with_zero_fraction_preserves_decimal_point(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| // JSON_PRESERVE_ZERO_FRACTION ensures 1.0 encodes as "1.0", not "1" | ||
| $result = $type->convertToDatabaseValue(1.0, $this->platform); | ||
|
|
||
| $this->assertSame('1.0', $result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function an_unserializable_value_throws_a_conversion_exception(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| // A recursive reference cannot be JSON-encoded and triggers SerializationFailed | ||
| $recursive = []; | ||
| $recursive[] = &$recursive; | ||
|
|
||
| $this->expectException(ConversionException::class); | ||
| $type->convertToDatabaseValue($recursive, $this->platform); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function null_from_database_converts_to_null(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $result = $type->convertToPHPValue(null, $this->platform); | ||
|
|
||
| $this->assertNull($result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function empty_string_from_database_converts_to_null(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $result = $type->convertToPHPValue('', $this->platform); | ||
|
|
||
| $this->assertNull($result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function valid_json_from_database_converts_to_php_value(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $result = $type->convertToPHPValue('{"key":"value","num":42}', $this->platform); | ||
|
|
||
| $this->assertSame(['key' => 'value', 'num' => 42], $result); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function invalid_json_from_database_throws_a_conversion_exception(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $this->expectException(ConversionException::class); | ||
| $type->convertToPHPValue('this is not valid json }{', $this->platform); | ||
| } | ||
|
|
||
| #[Group('EngineBlockBundle')] | ||
| #[Group('Doctrine')] | ||
| #[Test] | ||
| public function sql_declaration_uses_clob_not_native_json(): void | ||
| { | ||
| $type = Type::getType(LegacyJsonType::NAME); | ||
|
|
||
| $declaration = $type->getSQLDeclaration([], $this->platform); | ||
|
|
||
| // Must produce longtext, not the native MySQL JSON column type | ||
| $this->assertSame('LONGTEXT', $declaration); | ||
| $this->assertStringNotContainsStringIgnoringCase('json', $declaration); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.