Skip to content

Commit

Permalink
Add option 'skip-definer' to ommit DEFINER clauses in sql dump
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Sauvat committed Jan 30, 2018
1 parent 462366c commit 7dcebe1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ $dumpSettingsDefault = array(
'skip-tz-utc' => false,
'skip-comments' => false,
'skip-dump-date' => false,
'skip-definer' => false,
'where' => '',
/* deprecated */
'disable-foreign-keys-check' => true
Expand Down Expand Up @@ -215,6 +216,8 @@ $this->_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dump
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_triggers
- **skip-tz-utc**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc
- **skip-definer**
- https://dev.mysql.com/doc/refman/5.7/en/mysqlpump.html#option_mysqlpump_skip-definer
- **where**
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where

Expand Down
11 changes: 8 additions & 3 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function __construct(
'skip-tz-utc' => false,
'skip-comments' => false,
'skip-dump-date' => false,
'skip-definer' => false,
'where' => '',
/* deprecated */
'disable-foreign-keys-check' => true
Expand Down Expand Up @@ -1550,10 +1551,12 @@ public function create_view($row)

$viewStmt = $row['Create View'];

$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50013 \2 */'.PHP_EOL;

if ($viewStmtReplaced = preg_replace(
'/^(CREATE(?:\s+ALGORITHM=(?:UNDEFINED|MERGE|TEMPTABLE))?)\s+('
.self::DEFINER_RE.'(?:\s+SQL SECURITY DEFINER|INVOKER)?)?\s+(VIEW .+)$/',
'/*!50001 \1 */'.PHP_EOL.'/*!50013 \2 */'.PHP_EOL.'/*!50001 \3 */',
'/*!50001 \1 */'.PHP_EOL.$definerStr.'/*!50001 \3 */',
$viewStmt,
1
)) {
Expand All @@ -1572,9 +1575,10 @@ public function create_trigger($row)
}

$triggerStmt = $row['SQL Original Statement'];
$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50017 \2*/ ';
if ($triggerStmtReplaced = preg_replace(
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(TRIGGER\s.*)$/s',
'/*!50003 \1*/ /*!50017 \2*/ /*!50003 \3 */',
'/*!50003 \1*/ '.$definerStr.'/*!50003 \3 */',
$triggerStmt,
1
)) {
Expand Down Expand Up @@ -1618,10 +1622,11 @@ public function create_event($row)
$eventName = $row['Event'];
$eventStmt = $row['Create Event'];
$sqlMode = $row['sql_mode'];
$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50117 \2*/ ';

if ($eventStmtReplaced = preg_replace(
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(EVENT .*)$/',
'/*!50106 \1*/ /*!50117 \2*/ /*!50106 \3 */',
'/*!50106 \1*/ '.$definerStr.'/*!50106 \3 */',
$eventStmt,
1
)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@
array("events" => true));
$dump->start("mysqldump-php_test012.sql");

$dump = new IMysqldump\Mysqldump(
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test012",
"travis",
"",
array(
"events" => true,
'skip-definer' => true,
));
$dump->start("mysqldump-php_test012_no-definer.sql");

exit;
3 changes: 3 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ ret[((index++))]=$?
diff mysqldump_test012.filtered.sql mysqldump-php_test012.filtered.sql
ret[((index++))]=$?

# Make sure we do not fine a DEFINER
! grep 'DEFINER' mysqldump-php_test012_no-definer.sql
ret[((index++))]=$?

rm *.checksum 2> /dev/null
rm *.filtered.sql 2> /dev/null
Expand Down

0 comments on commit 7dcebe1

Please sign in to comment.