66
77namespace Magento \Framework \Setup \Declaration \Schema \Db ;
88
9+ use Magento \Framework \App \ResourceConnection ;
910use Magento \Framework \Setup \Declaration \Schema \Dto \ElementInterface ;
1011
1112/**
1213 * Holds different definitions and apply them depends on column, constraint, index types.
13- * Converts object to definition, and definition to array.
14- *
15- * @inheritdoc
1614 */
1715class DefinitionAggregator implements DbDefinitionProcessorInterface
1816{
@@ -21,14 +19,28 @@ class DefinitionAggregator implements DbDefinitionProcessorInterface
2119 */
2220 private $ definitionProcessors ;
2321
22+ /**
23+ * @var ResourceConnection
24+ */
25+ private $ resourceConnection ;
26+
27+ /**
28+ * @var string
29+ */
30+ private $ dbVersion ;
31+
2432 /**
2533 * Constructor.
2634 *
35+ * @param ResourceConnection $resourceConnection
2736 * @param DbDefinitionProcessorInterface[] $definitionProcessors
2837 */
29- public function __construct (array $ definitionProcessors )
30- {
38+ public function __construct (
39+ ResourceConnection $ resourceConnection ,
40+ array $ definitionProcessors
41+ ) {
3142 $ this ->definitionProcessors = $ definitionProcessors ;
43+ $ this ->resourceConnection = $ resourceConnection ;
3244 }
3345
3446 /**
@@ -60,6 +72,61 @@ public function fromDefinition(array $data)
6072 }
6173
6274 $ definitionProcessor = $ this ->definitionProcessors [$ type ];
75+ if (isset ($ data ['default ' ])) {
76+ $ data ['default ' ] = $ this ->processDefaultValue ($ data );
77+ }
78+
6379 return $ definitionProcessor ->fromDefinition ($ data );
6480 }
81+
82+ /**
83+ * Get DB version
84+ *
85+ * @return string
86+ */
87+ private function getDatabaseVersion (): string
88+ {
89+ if (!$ this ->dbVersion ) {
90+ $ this ->dbVersion = $ this ->resourceConnection ->getConnection ('default ' )
91+ ->fetchPairs ("SHOW variables LIKE 'version' " )['version ' ];
92+ }
93+
94+ return $ this ->dbVersion ;
95+ }
96+
97+ /**
98+ * Processes `$value` to be compatible with MySQL.
99+ *
100+ * @param array $data
101+ * @return string|null|bool
102+ */
103+ protected function processDefaultValue (array $ data )
104+ {
105+ $ defaultValue = $ data ['default ' ];
106+ if ($ defaultValue === null || $ data ['default ' ] === false ) {
107+ return $ defaultValue ;
108+ }
109+ if ($ defaultValue === "'NULL' " ) {
110+ return "NULL " ;
111+ }
112+ if ($ defaultValue === "NULL " && strpos ($ this ->getDatabaseVersion (), 'MariaDB ' ) !== false ) {
113+ return null ;
114+ }
115+ /*
116+ * MariaDB replaces some defaults by their respective functions, e.g. `DEFAULT CURRENT_TIMESTAMP` ends up being
117+ * `current_timestamp()` in the information schema.
118+ */
119+ $ defaultValue = strtr (
120+ $ defaultValue ,
121+ [
122+ 'current_timestamp() ' => 'CURRENT_TIMESTAMP ' ,
123+ 'curdate() ' => 'CURRENT_DATE ' ,
124+ 'curtime() ' => 'CURRENT_TIME '
125+ ]
126+ );
127+ //replace escaped single quotes
128+ $ defaultValue = str_replace ("' " , "" , $ defaultValue );
129+
130+ return $ defaultValue ;
131+ }
65132}
0 commit comments