6
6
7
7
namespace Magento \Framework \Setup \Declaration \Schema \Db ;
8
8
9
+ use Magento \Framework \App \ResourceConnection ;
9
10
use Magento \Framework \Setup \Declaration \Schema \Dto \ElementInterface ;
10
11
11
12
/**
12
13
* Holds different definitions and apply them depends on column, constraint, index types.
13
- * Converts object to definition, and definition to array.
14
- *
15
- * @inheritdoc
16
14
*/
17
15
class DefinitionAggregator implements DbDefinitionProcessorInterface
18
16
{
@@ -21,14 +19,28 @@ class DefinitionAggregator implements DbDefinitionProcessorInterface
21
19
*/
22
20
private $ definitionProcessors ;
23
21
22
+ /**
23
+ * @var ResourceConnection
24
+ */
25
+ private $ resourceConnection ;
26
+
27
+ /**
28
+ * @var string
29
+ */
30
+ private $ dbVersion ;
31
+
24
32
/**
25
33
* Constructor.
26
34
*
35
+ * @param ResourceConnection $resourceConnection
27
36
* @param DbDefinitionProcessorInterface[] $definitionProcessors
28
37
*/
29
- public function __construct (array $ definitionProcessors )
30
- {
38
+ public function __construct (
39
+ ResourceConnection $ resourceConnection ,
40
+ array $ definitionProcessors
41
+ ) {
31
42
$ this ->definitionProcessors = $ definitionProcessors ;
43
+ $ this ->resourceConnection = $ resourceConnection ;
32
44
}
33
45
34
46
/**
@@ -60,6 +72,61 @@ public function fromDefinition(array $data)
60
72
}
61
73
62
74
$ definitionProcessor = $ this ->definitionProcessors [$ type ];
75
+ if (isset ($ data ['default ' ])) {
76
+ $ data ['default ' ] = $ this ->processDefaultValue ($ data );
77
+ }
78
+
63
79
return $ definitionProcessor ->fromDefinition ($ data );
64
80
}
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
+ }
65
132
}
0 commit comments