Skip to content

Instantly share code, notes, and snippets.

@willboudle
Created November 19, 2020 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willboudle/4fb62bac2ed895bb0172202e6288ee74 to your computer and use it in GitHub Desktop.
Save willboudle/4fb62bac2ed895bb0172202e6288ee74 to your computer and use it in GitHub Desktop.
github-25479-json-fields-support-patch.patch
diff --git a/app/etc/di.xml b/app/etc/di.xml
index f8818de2af84..167471dbd039 100644
--- a/app/etc/di.xml
+++ b/app/etc/di.xml
@@ -1453,6 +1453,7 @@
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Primary</item>
<item name="foreign" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Foreign</item>
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Index</item>
+ <item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Json</item>
</argument>
</arguments>
</type>
@@ -1480,6 +1481,7 @@
<item name="varchar" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
<item name="binary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
<item name="varbinary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
+ <item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\Json</item>
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Index</item>
<item name="unique" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
@@ -1593,6 +1595,7 @@
<item name="datetime" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\TimestampDefinition</item>
<item name="date" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\DateDefinition</item>
<item name="boolean" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\BooleanDefinition</item>
+ <item name="json" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\JsonDefinition</item>
</argument>
</arguments>
</type>
diff --git a/Setup/Declaration/Schema/Db/MySQL/Definition/Columns/Json.php b/Setup/Declaration/Schema/Db/MySQL/Definition/Columns/Json.php
new file mode 100644
index 000000000000..2ed65a9ff9d0
--- /dev/null
+++ b/Setup/Declaration/Schema/Db/MySQL/Definition/Columns/Json.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns;
+
+use Magento\Framework\App\ResourceConnection;
+use Magento\Framework\Setup\Declaration\Schema\Db\DbDefinitionProcessorInterface;
+use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;
+
+/**
+ * Process json data type.
+ */
+class Json implements DbDefinitionProcessorInterface
+{
+ /**
+ * @var Nullable
+ */
+ private $nullable;
+
+ /**
+ * @var ResourceConnection
+ */
+ private $resourceConnection;
+
+ /**
+ * @var Comment
+ */
+ private $comment;
+
+ /**
+ * Blob constructor.
+ *
+ * @param Nullable $nullable
+ * @param Comment $comment
+ * @param ResourceConnection $resourceConnection
+ */
+ public function __construct(
+ Nullable $nullable,
+ Comment $comment,
+ ResourceConnection $resourceConnection
+ ) {
+ $this->nullable = $nullable;
+ $this->resourceConnection = $resourceConnection;
+ $this->comment = $comment;
+ }
+
+ /**
+ * Returns an array of column definitions
+ *
+ * @param array $data
+ * @return array+
+ */
+ public function toDefinition(ElementInterface $column)
+ {
+ return sprintf(
+ '%s %s %s %s',
+ $this->resourceConnection->getConnection()->quoteIdentifier($column->getName()),
+ $column->getType(),
+ $this->nullable->toDefinition($column),
+ $this->comment->toDefinition($column)
+ );
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function fromDefinition(array $data)
+ {
+ return $data;
+ }
+}
diff --git a/Setup/Declaration/Schema/Dto/Columns/Json.php b/Setup/Declaration/Schema/Dto/Columns/Json.php
new file mode 100644
index 000000000000..8b8de071068a
--- /dev/null
+++ b/Setup/Declaration/Schema/Dto/Columns/Json.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Framework\Setup\Declaration\Schema\Dto\Columns;
+
+use Magento\Framework\Setup\Declaration\Schema\Dto\Column;
+use Magento\Framework\Setup\Declaration\Schema\Dto\ElementDiffAwareInterface;
+use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
+
+/**
+ * Json
+ *
+ * Text column.
+ * Declared in SQL, like: JSON
+ */
+class Json extends Column implements ElementDiffAwareInterface, ColumnNullableAwareInterface
+{
+ /**
+ * @var bool
+ */
+ private $nullable;
+
+ /**
+ * Constructor.
+ *
+ * @param string $name
+ * @param string $type
+ * @param Table $table
+ * @param bool $nullable
+ * @param string|null $comment
+ * @param string|null $onCreate
+ */
+ public function __construct(
+ string $name,
+ string $type,
+ Table $table,
+ bool $nullable = true,
+ string $comment = null,
+ string $onCreate = null
+ ) {
+ parent::__construct($name, $type, $table, $comment, $onCreate);
+ $this->nullable = $nullable;
+ }
+
+ /**
+ * Check whether column can be nullable.
+ *
+ * @return bool
+ */
+ public function isNullable()
+ {
+ return $this->nullable;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getDiffSensitiveParams()
+ {
+ return [
+ 'type' => $this->getType(),
+ 'nullable' => $this->isNullable(),
+ 'comment' => $this->getComment()
+ ];
+ }
+}
diff --git a/Setup/Declaration/Schema/Dto/Factories/Json.php b/Setup/Declaration/Schema/Dto/Factories/Json.php
new file mode 100644
index 000000000000..f778b048413d
--- /dev/null
+++ b/Setup/Declaration/Schema/Dto/Factories/Json.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Framework\Setup\Declaration\Schema\Dto\Factories;
+
+use Magento\Framework\ObjectManagerInterface;
+
+/**
+ * Class Json
+ */
+class Json implements FactoryInterface
+{
+ /**
+ * @var ObjectManagerInterface
+ */
+ private $objectManager;
+
+ /**
+ * @var string
+ */
+ private $className;
+
+ /**
+ * Constructor.
+ *
+ * @param ObjectManagerInterface $objectManager
+ * @param string $className
+ */
+ public function __construct(
+ ObjectManagerInterface $objectManager,
+ $className = \Magento\Framework\Setup\Declaration\Schema\Dto\Columns\Blob::class
+ ) {
+ $this->objectManager = $objectManager;
+ $this->className = $className;
+ }
+
+ /**
+ * Create element using definition data array.
+ *
+ * @param array $data
+ * @return \Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface|mixed
+ */
+ public function create(array $data)
+ {
+ return $this->objectManager->create($this->className, $data);
+ }
+}
diff --git a/Setup/Declaration/Schema/etc/schema.xsd b/Setup/Declaration/Schema/etc/schema.xsd
index e3c54413f810..bb9136d8a9ae 100644
--- a/Setup/Declaration/Schema/etc/schema.xsd
+++ b/Setup/Declaration/Schema/etc/schema.xsd
@@ -20,6 +20,7 @@
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/longtext.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/mediumtext.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/varchar.xsd" />
+ <xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/json.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/blob.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/mediumblob.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/longblob.xsd" />
diff --git a/Setup/Declaration/Schema/etc/types/texts/json.xsd b/Setup/Declaration/Schema/etc/types/texts/json.xsd
new file mode 100644
index 000000000000..690f84a5ef43
--- /dev/null
+++ b/Setup/Declaration/Schema/etc/types/texts/json.xsd
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+-->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
+ <xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/column.xsd"/>
+
+ <xs:complexType name="json">
+ <xs:complexContent>
+ <xs:extension base="abstractColumnType">
+ <xs:annotation>
+ <xs:documentation>
+ Well formatted Json object
+ </xs:documentation>
+ </xs:annotation>
+ <xs:attribute name="nullable" type="xs:boolean" />
+ </xs:extension>
+ </xs:complexContent>
+ </xs:complexType>
+</xs:schema>
diff --git a/Setup/SchemaListenerDefinition/JsonDefinition.php b/Setup/SchemaListenerDefinition/JsonDefinition.php
new file mode 100644
index 000000000000..04866dde943f
--- /dev/null
+++ b/Setup/SchemaListenerDefinition/JsonDefinition.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Framework\Setup\SchemaListenerDefinition;
+
+/**
+ * Json type definition.
+ */
+class JsonDefinition implements DefinitionConverterInterface
+{
+ /**
+ * @inheritdoc
+ */
+ public function convertToDefinition(array $definition)
+ {
+ return [
+ 'xsi:type' => $definition['type'],
+ 'name' => $definition['name'],
+ 'nullable' => $definition['nullable'] ?? true
+ ];
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment