Skip to content

Database backup doesn't include triggers #9036 #11369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/code/Magento/Backup/Model/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu
}
}
$backup->write($this->getResource()->getTableForeignKeysSql());
$backup->write($this->getResource()->getTableTriggersSql());
$backup->write($this->getResource()->getFooter());

$this->getResource()->commitTransaction();
Expand Down
24 changes: 24 additions & 0 deletions app/code/Magento/Backup/Model/ResourceModel/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ public function getTableForeignKeysSql($tableName = null)
return $fkScript;
}

/**
* Return triggers fro table(s)
*
* @param string|null $tableName
* @param bool $addDropIfExists
* @return string
*/
public function getTableTriggersSql($tableName = null, $addDropIfExists = true)
{
$triggerScript = '';
if (!$tableName) {
$tables = $this->getTables();
foreach ($tables as $table) {
$tableTriggerScript = $this->_resourceHelper->getTableTriggersSql($table, $addDropIfExists);
if (!empty($tableTriggerScript)) {
$triggerScript .= "\n" . $tableTriggerScript;
}
}
} else {
$triggerScript = $this->getTableTriggersSql($tableName, $addDropIfExists);
}
return $triggerScript;
}

/**
* Retrieve table status
*
Expand Down
36 changes: 36 additions & 0 deletions app/code/Magento/Backup/Model/ResourceModel/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,40 @@ public function restoreTransactionIsolationLevel()
{
$this->getConnection()->query('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ');
}

/**
* Get create script for triggers
*
* @param string $tableName
* @param boolean $addDropIfExists
* @param boolean $stripDefiner
* @return string
*/
public function getTableTriggersSql($tableName, $addDropIfExists = false, $stripDefiner = true)
{
$script = "--\n-- Triggers structure for table `{$tableName}`\n--\n";
$triggers = $this->getConnection()->query('SHOW TRIGGERS LIKE \''. $tableName . '\'')->fetchAll();

if (!$triggers) {
return '';
}
foreach ($triggers as $trigger) {
if ($addDropIfExists) {
$script .= 'DROP TRIGGER IF EXISTS ' . $trigger['Trigger'] . ";\n";
}
$script .= "delimiter ;;\n";

$triggerData = $this->getConnection()->query('SHOW CREATE TRIGGER '. $trigger['Trigger'])->fetch();
if ($stripDefiner) {
$cleanedScript = preg_replace('/DEFINER=[^\s]*/', '', $triggerData['SQL Original Statement']);
$script .= $cleanedScript . "\n";
} else {
$script .= $triggerData['SQL Original Statement'] . "\n";
}
$script .= ";;\n";
$script .= "delimiter ;\n";
}

return $script;
}
}