@@ -22,8 +22,6 @@ final class MysqlDatabaseBackup extends AbstractDatabaseBackup implements Databa
2222{
2323 protected static $ referenceData ;
2424
25- protected static $ sql ;
26-
2725 protected static $ metadata ;
2826
2927 protected static $ schemaUpdatedFlag = false ;
@@ -40,11 +38,7 @@ public function getReferenceBackupFilePath(): string
4038
4139 protected function getBackup ()
4240 {
43- if (empty (self ::$ sql )) {
44- self ::$ sql = file_get_contents ($ this ->getBackupFilePath ());
45- }
46-
47- return self ::$ sql ;
41+ return file_get_contents ($ this ->getBackupFilePath ());
4842 }
4943
5044 protected function getReferenceBackup (): string
@@ -84,7 +78,7 @@ public function backup(AbstractExecutor $executor): void
8478 $ executor ->getReferenceRepository ()->save ($ this ->getBackupFilePath ());
8579 self ::$ metadata = $ em ->getMetadataFactory ()->getLoadedMetadata ();
8680
87- exec ("mysqldump -h $ dbHost -u $ dbUser -p $ dbPass --no-create-info --skip-triggers --no-create-db --no-tablespaces --compact $ dbName > {$ this ->getBackupFilePath ()}" );
81+ exec ("MYSQL_PWD= $ dbPass mysqldump --host $ dbHost --port= $ dbPort --user $ dbUser --no-create-info --skip-triggers --no-create-db --no-tablespaces --compact $ dbName > {$ this ->getBackupFilePath ()}" );
8882 }
8983
9084 protected function updateSchemaIfNeed (EntityManager $ em )
@@ -100,7 +94,7 @@ protected function updateSchemaIfNeed(EntityManager $em)
10094 }
10195 }
10296
103- public function restore (AbstractExecutor $ executor ): void
97+ public function restore (AbstractExecutor $ executor, array $ excludedTables = [] ): void
10498 {
10599 /** @var EntityManager $em */
106100 $ em = $ executor ->getReferenceRepository ()->getManager ();
@@ -110,10 +104,23 @@ public function restore(AbstractExecutor $executor): void
110104 $ this ->updateSchemaIfNeed ($ em );
111105 $ truncateSql = [];
112106 foreach ($ this ->metadatas as $ classMetadata ) {
113- $ truncateSql [] = 'DELETE FROM ' .$ classMetadata ->table ['name ' ]; // in small tables it's really faster than truncate
107+ $ tableName = $ classMetadata ->table ['name ' ];
108+
109+ if (!in_array ($ tableName , $ excludedTables )) {
110+ $ truncateSql [] = 'DELETE FROM ' .$ tableName ; // in small tables it's really faster than truncate
111+ }
114112 }
115- $ connection ->query (implode ('; ' , $ truncateSql ));
116- $ connection ->query ($ this ->getBackup ());
113+ if (!empty ($ truncateSql )) {
114+ $ connection ->query (implode ('; ' , $ truncateSql ));
115+ }
116+
117+ // Only run query if it exists, to avoid the following exception:
118+ // SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty
119+ $ backup = $ this ->getBackup ();
120+ if (!empty ($ backup )) {
121+ $ connection ->query ($ backup );
122+ }
123+
117124 $ connection ->query ('SET FOREIGN_KEY_CHECKS = 1; ' );
118125
119126 if (self ::$ metadata ) {
0 commit comments