@@ -152,79 +152,57 @@ public function createColumnSchemaBuilder($type, $length = null): ColumnSchemaBu
152152 */
153153 public function getDefaultBackupCommand (?array $ ignoreTables = null ): string
154154 {
155- $ useSingleTransaction = true ;
156- $ serverVersion = App::normalizeVersion ($ this ->getServerVersion ());
157-
155+ $ baseCommand = (new ShellCommand ('mysqldump ' ))
156+ ->addArg ('--defaults-file= ' , $ this ->_createDumpConfigFile ())
157+ ->addArg ('--add-drop-table ' )
158+ ->addArg ('--comments ' )
159+ ->addArg ('--create-options ' )
160+ ->addArg ('--dump-date ' )
161+ ->addArg ('--no-autocommit ' )
162+ ->addArg ('--routines ' )
163+ ->addArg ('--default-character-set= ' , Craft::$ app ->getConfig ()->getDb ()->charset )
164+ ->addArg ('--set-charset ' )
165+ ->addArg ('--triggers ' )
166+ ->addArg ('--no-tablespaces ' );
167+
168+ $ serverVersion = App::normalizeVersion (Craft::$ app ->getDb ()->getServerVersion ());
158169 $ isMySQL5 = version_compare ($ serverVersion , '8 ' , '< ' );
159170 $ isMySQL8 = version_compare ($ serverVersion , '8 ' , '>= ' );
171+ $ ignoreTables = $ ignoreTables ?? Craft::$ app ->getDb ()->getIgnoredBackupTables ();
172+ $ commandFromConfig = Craft::$ app ->getConfig ()->getGeneral ()->backupCommand ;
160173
161174 // https://bugs.mysql.com/bug.php?id=109685
162- if (($ isMySQL5 && version_compare ($ serverVersion , '5.7.41 ' , '>= ' )) ||
163- ($ isMySQL8 && version_compare ($ serverVersion , '8.0.32 ' , '>= ' ))) {
164- $ useSingleTransaction = false ;
165- }
166-
167- $ defaultArgs =
168- ' --defaults-file=" ' . $ this ->_createDumpConfigFile () . '" ' .
169- ' --add-drop-table ' .
170- ' --comments ' .
171- ' --create-options ' .
172- ' --dump-date ' .
173- ' --no-autocommit ' .
174- ' --routines ' .
175- ' --default-character-set= ' . Craft::$ app ->getConfig ()->getDb ()->charset .
176- ' --set-charset ' .
177- ' --triggers ' .
178- ' --no-tablespaces ' ;
175+ $ useSingleTransaction =
176+ ($ isMySQL5 && version_compare ($ serverVersion , '5.7.41 ' , '>= ' )) ||
177+ ($ isMySQL8 && version_compare ($ serverVersion , '8.0.32 ' , '>= ' ));
179178
180179 if ($ useSingleTransaction ) {
181- $ defaultArgs .= ' --single-transaction ' ;
180+ $ baseCommand -> addArg ( ' --single-transaction ') ;
182181 }
183182
184- // Find out if the db/dump client supports column-statistics
185- $ shellCommand = new ShellCommand ();
186-
187- if (Platform::isWindows ()) {
188- $ shellCommand ->setCommand ('mysqldump --help | findstr "column-statistics" ' );
189- } else {
190- $ shellCommand ->setCommand ('mysqldump --help | grep "column-statistics" ' );
183+ if ($ this ->supportsColumnStatistics ()) {
184+ $ baseCommand ->addArg ('--column-statistics= ' , '0 ' );
191185 }
192186
193- // If we don't have proc_open, maybe we've got exec
194- if (!function_exists ('proc_open ' ) && function_exists ('exec ' )) {
195- $ shellCommand ->useExec = true ;
196- }
197-
198- $ success = $ shellCommand ->execute ();
187+ $ schemaDump = (clone $ baseCommand )
188+ ->addArg ('--no-data ' )
189+ ->addArg ('--result-file= ' , '{file} ' )
190+ ->addArg ('{database} ' );
199191
200- // if there was output, then column-statistics is supported and we should disable it
201- if ($ success && $ shellCommand ->getOutput ()) {
202- $ defaultArgs .= ' --column-statistics=0 ' ;
203- }
192+ $ dataDump = (clone $ baseCommand )
193+ ->addArg ('--no-create-info ' );
204194
205- if ($ ignoreTables === null ) {
206- $ ignoreTables = $ this ->db ->getIgnoredBackupTables ();
207- }
208- $ ignoreTableArgs = [];
209195 foreach ($ ignoreTables as $ table ) {
210- $ table = $ this ->getRawTableName ($ table );
211- $ ignoreTableArgs [] = " -- ignore-table={database }.$ table " ;
196+ $ table = Craft:: $ app -> getDb ()-> getSchema () ->getRawTableName ($ table );
197+ $ dataDump -> addArg ( ' -- ignore-table' , " {schema }.$ table ") ;
212198 }
213199
214- $ schemaDump = 'mysqldump ' .
215- $ defaultArgs .
216- ' --no-data ' .
217- ' --result-file="{file}" ' .
218- ' {database} ' ;
219-
220- $ dataDump = 'mysqldump ' .
221- $ defaultArgs .
222- ' --no-create-info ' .
223- ' ' . implode (' ' , $ ignoreTableArgs ) .
224- ' {database} ' .
225- ' >> "{file}" ' ;
200+ if ($ commandFromConfig instanceof \Closure) {
201+ $ schemaDump = $ commandFromConfig ($ schemaDump );
202+ $ dataDump = $ commandFromConfig ($ dataDump );
203+ }
226204
227- return $ schemaDump . ' && ' . $ dataDump ;
205+ return "{ $ schemaDump-> getExecCommand ()} && { $ dataDump-> getExecCommand ()} >> {file} " ;
228206 }
229207
230208 /**
@@ -235,10 +213,16 @@ public function getDefaultBackupCommand(?array $ignoreTables = null): string
235213 */
236214 public function getDefaultRestoreCommand (): string
237215 {
238- return 'mysql ' .
239- ' --defaults-file=" ' . $ this ->_createDumpConfigFile () . '" ' .
240- ' {database} ' .
241- ' < "{file}" ' ;
216+ $ commandFromConfig = Craft::$ app ->getConfig ()->getGeneral ()->backupCommand ;
217+ $ command = (new ShellCommand ('mysql ' ))
218+ ->addArg ('--defaults-file= ' , $ this ->_createDumpConfigFile ())
219+ ->addArg ('{database} ' );
220+
221+ if ($ commandFromConfig instanceof \Closure) {
222+ $ command = $ commandFromConfig ($ command );
223+ }
224+
225+ return $ command ->getExecCommand () . ' < "{file}" ' ;
242226 }
243227
244228 /**
@@ -383,6 +367,28 @@ protected function findConstraints($table): void
383367 }
384368 }
385369
370+ protected function supportsColumnStatistics (): bool
371+ {
372+ // Find out if the db/dump client supports column-statistics
373+ $ shellCommand = new ShellCommand ();
374+
375+ if (Platform::isWindows ()) {
376+ $ shellCommand ->setCommand ('mysqldump --help | findstr "column-statistics" ' );
377+ } else {
378+ $ shellCommand ->setCommand ('mysqldump --help | grep "column-statistics" ' );
379+ }
380+
381+ // If we don't have proc_open, maybe we've got exec
382+ if (!function_exists ('proc_open ' ) && function_exists ('exec ' )) {
383+ $ shellCommand ->useExec = true ;
384+ }
385+
386+ $ success = $ shellCommand ->execute ();
387+
388+ // if there was output, then column-statistics is supported
389+ return $ success && $ shellCommand ->getOutput ();
390+ }
391+
386392 /**
387393 * Creates a temporary my.cnf file based on the DB config settings.
388394 *
0 commit comments