Skip to content

Commit

Permalink
Make skip-definer work with routines. Added some tests.Closes ifsnop#168
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsnop committed Aug 5, 2019
1 parent 579fcf8 commit de456d9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ public function start($filename = '')
$this->exportTables();
$this->exportTriggers();
$this->exportFunctions();
$this->exportViews();
$this->exportProcedures();
$this->exportViews();
$this->exportEvents();

// Restore saved parameters.
Expand Down Expand Up @@ -1937,6 +1937,16 @@ public function create_procedure($row)
"Please check 'https://bugs.mysql.com/bug.php?id=14564'");
}
$procedureStmt = $row['Create Procedure'];
if ( $this->dumpSettings['skip-definer'] ) {
if ($procedureStmtReplaced = preg_replace(
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(PROCEDURE\s.*)$/s',
'\1 \3',
$procedureStmt,
1
)) {
$procedureStmt = $procedureStmtReplaced;
}
}

$ret .= "/*!50003 DROP PROCEDURE IF EXISTS `".
$row['Procedure']."` */;".PHP_EOL.
Expand All @@ -1958,6 +1968,16 @@ public function create_function($row)
"Please check 'https://bugs.mysql.com/bug.php?id=14564'");
}
$functionStmt = $row['Create Function'];
if ( $this->dumpSettings['skip-definer'] ) {
if ($functionStmtReplaced = preg_replace(
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(FUNCTION\s.*)$/s',
'\1 \3',
$functionStmt,
1
)) {
$functionStmt = $functionStmtReplaced;
}
}

$ret .= "/*!50003 DROP FUNCTION IF EXISTS `".
$row['Function']."` */;".PHP_EOL.
Expand Down
9 changes: 8 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test012",
"travis",
"",
array("events" => true));
array("events" => true,
'skip-triggers' => false,
'routines' => true,
'add-drop-trigger' => true,
));
$dump->start("mysqldump-php_test012.sql");

print "starting mysql-php_test012b_no-definer.sql" . PHP_EOL;
Expand All @@ -140,6 +144,9 @@
"",
array(
"events" => true,
'skip-triggers' => false,
'routines' => true,
'add-drop-trigger' => true,
'skip-definer' => true,
));
$dump->start("mysqldump-php_test012_no-definer.sql");
Expand Down
5 changes: 3 additions & 2 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mysqldump -utravis test012 \
--skip-extended-insert \
--hex-blob \
--events \
--routines \
> mysqldump_test012.sql
errCode=$?; ret[((index++))]=$errCode

Expand Down Expand Up @@ -147,7 +148,7 @@ cat mysqldump_test001.sql | grep ^INSERT > mysqldump_test001.filtered.sql
cat mysqldump_test001_complete.sql | grep ^INSERT > mysqldump_test001_complete.filtered.sql
cat mysqldump_test002.sql | grep ^INSERT > mysqldump_test002.filtered.sql
cat mysqldump_test005.sql | grep ^INSERT > mysqldump_test005.filtered.sql
cat mysqldump_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'TRIGGER' | grep -v -e 'TABLE' -e 'CREATE VIEW' > mysqldump_test012.filtered.sql
cat mysqldump_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'TRIGGER' -e 'FUNCTION' -e 'PROCEDURE' | grep -v -e 'TABLE' -e 'CREATE VIEW' > mysqldump_test012.filtered.sql
cat mysqldump_test013.sql | grep "INSERT" > mysqldump_test013.filtered.sql
cat mysqldump-php_test001.sql | grep ^INSERT > mysqldump-php_test001.filtered.sql
cat mysqldump-php_test001_complete.sql | grep ^INSERT > mysqldump-php_test001_complete.filtered.sql
Expand All @@ -164,7 +165,7 @@ else
echo "test011 disabled, only valid for mysql server version > 5.7.0"
fi

cat mysqldump-php_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'CREATE.*TRIGGER' > mysqldump-php_test012.filtered.sql
cat mysqldump-php_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'CREATE.*TRIGGER' -e 'FUNCTION' -e 'PROCEDURE' > mysqldump-php_test012.filtered.sql
cat mysqldump-php_test013.sql | grep INSERT > mysqldump-php_test013.filtered.sql

test="test $index diff test001.filtered.sql mysqldump_test001.filtered.sql"
Expand Down
14 changes: 14 additions & 0 deletions tests/test012.src.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ CREATE TRIGGER `test012_trigger`
BEFORE insert ON `test`
FOR EACH ROW set NEW.col = NEW.col + 1;

DELIMITER ;;
CREATE FUNCTION `test012_function`(i INT) RETURNS INT
DETERMINISTIC
BEGIN
RETURN (i);
END ;;
DELIMITER ;

DELIMITER ;;
CREATE PROCEDURE test012_procedure()
BEGIN
SELECT * FROM test012;
END ;;
DELIMITER ;

0 comments on commit de456d9

Please sign in to comment.