7
7
namespace Magento \Framework \Setup \Declaration \Schema \Db \MySQL ;
8
8
9
9
use Magento \Framework \App \ResourceConnection ;
10
+ use Magento \Framework \DB \Adapter \ConnectionException ;
11
+ use Magento \Framework \DB \Adapter \SqlVersionProvider ;
10
12
use Magento \Framework \Setup \Declaration \Schema \Db \DbSchemaWriterInterface ;
11
13
use Magento \Framework \Setup \Declaration \Schema \Db \Statement ;
12
14
use Magento \Framework \Setup \Declaration \Schema \Db \StatementAggregator ;
@@ -59,22 +61,30 @@ class DbSchemaWriter implements DbSchemaWriterInterface
59
61
*/
60
62
private $ dryRunLogger ;
61
63
64
+ /**
65
+ * @var SqlVersionProvider
66
+ */
67
+ private $ sqlVersionProvider ;
68
+
62
69
/**
63
70
* @param ResourceConnection $resourceConnection
64
- * @param StatementFactory $statementFactory
65
- * @param DryRunLogger $dryRunLogger
66
- * @param array $tableOptions
71
+ * @param StatementFactory $statementFactory
72
+ * @param DryRunLogger $dryRunLogger
73
+ * @param SqlVersionProvider $sqlVersionProvider
74
+ * @param array $tableOptions
67
75
*/
68
76
public function __construct (
69
77
ResourceConnection $ resourceConnection ,
70
78
StatementFactory $ statementFactory ,
71
79
DryRunLogger $ dryRunLogger ,
80
+ SqlVersionProvider $ sqlVersionProvider ,
72
81
array $ tableOptions = []
73
82
) {
74
83
$ this ->resourceConnection = $ resourceConnection ;
75
84
$ this ->statementFactory = $ statementFactory ;
76
85
$ this ->dryRunLogger = $ dryRunLogger ;
77
86
$ this ->tableOptions = array_replace ($ this ->tableOptions , $ tableOptions );
87
+ $ this ->sqlVersionProvider = $ sqlVersionProvider ;
78
88
}
79
89
80
90
/**
@@ -288,13 +298,25 @@ public function compile(StatementAggregator $statementAggregator, $dryRun)
288
298
)
289
299
);
290
300
} else {
291
- $ adapter ->query (
292
- sprintf (
293
- $ this ->statementDirectives [$ statement ->getType ()],
294
- $ adapter ->quoteIdentifier ($ statement ->getTableName ()),
295
- implode (", " , $ statementsSql )
296
- )
297
- );
301
+ if ($ this ->isNeedToSplitSql ()) {
302
+ foreach ($ statementsSql as $ statementSql ) {
303
+ $ adapter ->query (
304
+ sprintf (
305
+ $ this ->statementDirectives [$ statement ->getType ()],
306
+ $ adapter ->quoteIdentifier ($ statement ->getTableName ()),
307
+ $ statementSql
308
+ )
309
+ );
310
+ }
311
+ } else {
312
+ $ adapter ->query (
313
+ sprintf (
314
+ $ this ->statementDirectives [$ statement ->getType ()],
315
+ $ adapter ->quoteIdentifier ($ statement ->getTableName ()),
316
+ implode (", " , $ statementsSql )
317
+ )
318
+ );
319
+ }
298
320
//Do post update, like SQL DML operations or etc...
299
321
foreach ($ statement ->getTriggers () as $ trigger ) {
300
322
call_user_func ($ trigger );
@@ -303,6 +325,20 @@ public function compile(StatementAggregator $statementAggregator, $dryRun)
303
325
}
304
326
}
305
327
328
+ /**
329
+ * Check if we can concatenate sql into one statement
330
+ *
331
+ * Due to issues with some versions of MariaBD such statements
332
+ * may produce errors, e.g. with foreign key definition with column modification
333
+ *
334
+ * @return bool
335
+ * @throws ConnectionException
336
+ */
337
+ private function isNeedToSplitSql () : bool
338
+ {
339
+ return str_contains ($ this ->sqlVersionProvider ->getSqlVersion (), SqlVersionProvider::MARIA_DB_10_VERSION );
340
+ }
341
+
306
342
/**
307
343
* Retrieve next value for AUTO_INCREMENT column.
308
344
*
0 commit comments