From de456d984d7b2f8a5360faecd07e41f22c87861e Mon Sep 17 00:00:00 2001 From: Diego Torres Date: Mon, 5 Aug 2019 12:07:47 +0000 Subject: [PATCH] Make skip-definer work with routines. Added some tests.Closes #168 --- src/Ifsnop/Mysqldump/Mysqldump.php | 22 +++++++++++++++++++++- tests/test.php | 9 ++++++++- tests/test.sh | 5 +++-- tests/test012.src.sql | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 3c13559c..c97bb5b5 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -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. @@ -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. @@ -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. diff --git a/tests/test.php b/tests/test.php index 14fa650e..0c581f1b 100644 --- a/tests/test.php +++ b/tests/test.php @@ -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; @@ -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"); diff --git a/tests/test.sh b/tests/test.sh index 3fa659a2..9a2b786a 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -101,6 +101,7 @@ mysqldump -utravis test012 \ --skip-extended-insert \ --hex-blob \ --events \ + --routines \ > mysqldump_test012.sql errCode=$?; ret[((index++))]=$errCode @@ -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 @@ -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" diff --git a/tests/test012.src.sql b/tests/test012.src.sql index 954705d3..360a1436 100644 --- a/tests/test012.src.sql +++ b/tests/test012.src.sql @@ -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 ;