File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
app/code/Magento/Backup/Model Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -173,6 +173,7 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu
173173 }
174174 }
175175 $ backup ->write ($ this ->getResource ()->getTableForeignKeysSql ());
176+ $ backup ->write ($ this ->getResource ()->getTableTriggersSql ());
176177 $ backup ->write ($ this ->getResource ()->getFooter ());
177178
178179 $ this ->getResource ()->commitTransaction ();
Original file line number Diff line number Diff line change @@ -114,6 +114,30 @@ public function getTableForeignKeysSql($tableName = null)
114114 return $ fkScript ;
115115 }
116116
117+ /**
118+ * Return triggers fro table(s)
119+ *
120+ * @param string|null $tableName
121+ * @param bool $addDropIfExists
122+ * @return string
123+ */
124+ public function getTableTriggersSql ($ tableName = null , $ addDropIfExists = true )
125+ {
126+ $ triggerScript = '' ;
127+ if (!$ tableName ) {
128+ $ tables = $ this ->getTables ();
129+ foreach ($ tables as $ table ) {
130+ $ tableTriggerScript = $ this ->_resourceHelper ->getTableTriggersSql ($ table , $ addDropIfExists );
131+ if (!empty ($ tableTriggerScript )) {
132+ $ triggerScript .= "\n" . $ tableTriggerScript ;
133+ }
134+ }
135+ } else {
136+ $ triggerScript = $ this ->getTableTriggersSql ($ tableName , $ addDropIfExists );
137+ }
138+ return $ triggerScript ;
139+ }
140+
117141 /**
118142 * Retrieve table status
119143 *
Original file line number Diff line number Diff line change @@ -337,4 +337,40 @@ public function restoreTransactionIsolationLevel()
337337 {
338338 $ this ->getConnection ()->query ('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ' );
339339 }
340+
341+ /**
342+ * Get create script for triggers
343+ *
344+ * @param string $tableName
345+ * @param boolean $addDropIfExists
346+ * @param boolean $stripDefiner
347+ * @return string
348+ */
349+ public function getTableTriggersSql ($ tableName , $ addDropIfExists = false , $ stripDefiner = true )
350+ {
351+ $ script = "-- \n-- Triggers structure for table ` {$ tableName }` \n-- \n" ;
352+ $ triggers = $ this ->getConnection ()->query ('SHOW TRIGGERS LIKE \'' . $ tableName . '\'' )->fetchAll ();
353+
354+ if (!$ triggers ) {
355+ return '' ;
356+ }
357+ foreach ($ triggers as $ trigger ) {
358+ if ($ addDropIfExists ) {
359+ $ script .= 'DROP TRIGGER IF EXISTS ' . $ trigger ['Trigger ' ] . "; \n" ;
360+ }
361+ $ script .= "delimiter ;; \n" ;
362+
363+ $ triggerData = $ this ->getConnection ()->query ('SHOW CREATE TRIGGER ' . $ trigger ['Trigger ' ])->fetch ();
364+ if ($ stripDefiner ) {
365+ $ cleanedScript = preg_replace ('/DEFINER=[^\s]*/ ' , '' , $ triggerData ['SQL Original Statement ' ]);
366+ $ script .= $ cleanedScript . "\n" ;
367+ } else {
368+ $ script .= $ triggerData ['SQL Original Statement ' ] . "\n" ;
369+ }
370+ $ script .= ";; \n" ;
371+ $ script .= "delimiter ; \n" ;
372+ }
373+
374+ return $ script ;
375+ }
340376}
You can’t perform that action at this time.
0 commit comments