Skip to content

Commit

Permalink
[2.3-develop] Forwardport of #11369
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-engcom-team committed Feb 2, 2018
1 parent 3881f04 commit 78c2c21
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
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
25 changes: 25 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,31 @@ public function getTableForeignKeysSql($tableName = null)
return $fkScript;
}

/**
* Return triggers for 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Backup;

use Magento\Backup\Helper\Data;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Module\Setup;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Provide tests for \Magento\Framework\Backup\Db.
*/
class DbTest extends TestCase
{
/**
* Test db backup includes triggers.
*
* @magentoDataFixture Magento/Framework/Backup/_files/trigger.php
*/
public function testBackupIncludesCustomTriggers()
{
$helper = Bootstrap::getObjectManager()->get(Data::class);
$time = time();
$backupManager = Bootstrap::getObjectManager()->get(Factory::class)->create(
Factory::TYPE_DB
)->setBackupExtension(
$helper->getExtensionByType(Factory::TYPE_DB)
)->setTime(
$time
)->setBackupsDir(
$helper->getBackupsDir()
)->setName('test_backup');
$backupManager->create();
$write = Bootstrap::getObjectManager()->get(Filesystem::class)->getDirectoryWrite(DirectoryList::VAR_DIR);
$content = $write->readFile('/backups/' . $time . '_db_testbackup.sql');
$tableName = Bootstrap::getObjectManager()->get(Setup::class)
->getTable('test_table_with_custom_trigger');
$this->assertRegExp(
'/CREATE TRIGGER test_custom_trigger AFTER INSERT ON '. $tableName . ' FOR EACH ROW/',
$content
);
//Clean up.
$write->delete('/backups/' . $time . '_db_testbackup.sql');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\DB\Ddl\Trigger;
use Magento\Framework\DB\Ddl\TriggerFactory;
use Magento\Framework\Module\Setup;
use Magento\TestFramework\Helper\Bootstrap;

$setup = Bootstrap::getObjectManager()->get(Setup::class);
$tableName = $setup->getTable('test_table_with_custom_trigger');
$table = $setup->getConnection()->newTable(
$tableName
)->addColumn(
'id',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'ID'
)->setComment(
'Test table with test custom trigger'
);
$setup->getConnection()->createTable($table);

$trigger = Bootstrap::getObjectManager()->get(TriggerFactory::class)->create()
->setName('test_custom_trigger')
->setTime(Trigger::TIME_AFTER)
->setEvent(Trigger::EVENT_INSERT)
->setTable($tableName)
->addStatement($setup->getConnection()->quoteInto('SET @test_variable = ?', 'test_value'));
$setup->getConnection()->createTrigger($trigger);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\Module\Setup;
use Magento\TestFramework\Helper\Bootstrap;

$setup = Bootstrap::getObjectManager()->get(Setup::class);
$tableName = $setup->getTable('test_table_with_custom_trigger');
$setup->getConnection()->dropTrigger('test_custom_trigger');
$setup->getConnection()->dropTable($tableName);

0 comments on commit 78c2c21

Please sign in to comment.