77namespace Magento \Framework \Setup \Declaration \Schema \Db \MySQL ;
88
99use Magento \Framework \App \ResourceConnection ;
10+ use Magento \Framework \DB \Adapter \ConnectionException ;
11+ use Magento \Framework \DB \Adapter \SqlVersionProvider ;
1012use Magento \Framework \Setup \Declaration \Schema \Db \DbSchemaWriterInterface ;
1113use Magento \Framework \Setup \Declaration \Schema \Db \Statement ;
1214use Magento \Framework \Setup \Declaration \Schema \Db \StatementAggregator ;
@@ -59,22 +61,30 @@ class DbSchemaWriter implements DbSchemaWriterInterface
5961 */
6062 private $ dryRunLogger ;
6163
64+ /**
65+ * @var SqlVersionProvider
66+ */
67+ private $ sqlVersionProvider ;
68+
6269 /**
6370 * @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
6775 */
6876 public function __construct (
6977 ResourceConnection $ resourceConnection ,
7078 StatementFactory $ statementFactory ,
7179 DryRunLogger $ dryRunLogger ,
80+ SqlVersionProvider $ sqlVersionProvider ,
7281 array $ tableOptions = []
7382 ) {
7483 $ this ->resourceConnection = $ resourceConnection ;
7584 $ this ->statementFactory = $ statementFactory ;
7685 $ this ->dryRunLogger = $ dryRunLogger ;
7786 $ this ->tableOptions = array_replace ($ this ->tableOptions , $ tableOptions );
87+ $ this ->sqlVersionProvider = $ sqlVersionProvider ;
7888 }
7989
8090 /**
@@ -288,13 +298,25 @@ public function compile(StatementAggregator $statementAggregator, $dryRun)
288298 )
289299 );
290300 } 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+ }
298320 //Do post update, like SQL DML operations or etc...
299321 foreach ($ statement ->getTriggers () as $ trigger ) {
300322 call_user_func ($ trigger );
@@ -303,6 +325,20 @@ public function compile(StatementAggregator $statementAggregator, $dryRun)
303325 }
304326 }
305327
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+
306342 /**
307343 * Retrieve next value for AUTO_INCREMENT column.
308344 *
0 commit comments