From 9ceeadc67e56a0163f14735d7544005f621eb4a5 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 11:40:15 +0300 Subject: [PATCH 01/19] Require ext-pdo --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 751ab506..ba86a762 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ ], "require": { "php": "^7.4 || ^8.0", - "composer-runtime-api": "^2" + "composer-runtime-api": "^2", + "ext-pdo": "*" }, "require-dev": { "squizlabs/php_codesniffer": "3.*", From 4cb6847a4c0e33e7a3b7e34a5b6b3e30872ab63d Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 12:52:09 +0300 Subject: [PATCH 02/19] Break code classes to their own files --- .travis.yml | 9 +- .../Mysqldump/Compress/CompressBzip2.php | 45 + .../Mysqldump/Compress/CompressGzip.php | 44 + .../Mysqldump/Compress/CompressGzipstream.php | 42 + .../Mysqldump/Compress/CompressInterface.php | 12 + .../Compress/CompressManagerFactory.php | 21 + .../Mysqldump/Compress/CompressMethod.php | 27 + .../Mysqldump/Compress/CompressNone.php | 37 + src/Ifsnop/Mysqldump/Mysqldump.php | 1025 +---------------- .../Mysqldump/TypeAdapter/TypeAdapter.php | 19 + .../TypeAdapter/TypeAdapterDblib.php | 7 + .../TypeAdapter/TypeAdapterFactory.php | 251 ++++ .../TypeAdapter/TypeAdapterMysql.php | 547 +++++++++ .../TypeAdapter/TypeAdapterPgsql.php | 7 + .../TypeAdapter/TypeAdapterSqlite.php | 7 + 15 files changed, 1070 insertions(+), 1030 deletions(-) create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressBzip2.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressGzip.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressGzipstream.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressInterface.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressManagerFactory.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressMethod.php create mode 100644 src/Ifsnop/Mysqldump/Compress/CompressNone.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapter.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterDblib.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterFactory.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterMysql.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterPgsql.php create mode 100644 src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterSqlite.php diff --git a/.travis.yml b/.travis.yml index 4a83f626..4a68d17d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,16 +7,13 @@ matrix: sudo: required services: - mysql + before_install: + - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" before_script: - curl -s http://getcomposer.org/installer | php - php composer.phar install - mysql -V - mysqldump -V - - tests/create_users.sh + - tests/scripts/create_users.sh script: - - php -l src/Ifsnop/Mysqldump/Mysqldump.php - - php src/Ifsnop/Mysqldump/Mysqldump.php - - vendor/bin/phpunit - cd tests && ./test.sh - before_install: - - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" diff --git a/src/Ifsnop/Mysqldump/Compress/CompressBzip2.php b/src/Ifsnop/Mysqldump/Compress/CompressBzip2.php new file mode 100644 index 00000000..3db49685 --- /dev/null +++ b/src/Ifsnop/Mysqldump/Compress/CompressBzip2.php @@ -0,0 +1,45 @@ +fileHandler = bzopen($filename, "w"); + + if (false === $this->fileHandler) { + throw new Exception("Output file is not writable"); + } + + return true; + } + + public function write(string $str): int + { + $bytesWritten = bzwrite($this->fileHandler, $str); + + if (false === $bytesWritten) { + throw new Exception("Writting to file failed! Probably, there is no more free space left?"); + } + + return $bytesWritten; + } + + public function close(): bool + { + return bzclose($this->fileHandler); + } +} diff --git a/src/Ifsnop/Mysqldump/Compress/CompressGzip.php b/src/Ifsnop/Mysqldump/Compress/CompressGzip.php new file mode 100644 index 00000000..e1388cca --- /dev/null +++ b/src/Ifsnop/Mysqldump/Compress/CompressGzip.php @@ -0,0 +1,44 @@ +fileHandler = gzopen($filename, "wb"); + + if (false === $this->fileHandler) { + throw new Exception("Output file is not writable"); + } + + return true; + } + + public function write(string $str): int + { + $bytesWritten = gzwrite($this->fileHandler, $str); + + if (false === $bytesWritten) { + throw new Exception("Writing to file failed! Probably, there is no more free space left?"); + } + + return $bytesWritten; + } + + public function close(): bool + { + return gzclose($this->fileHandler); + } +} diff --git a/src/Ifsnop/Mysqldump/Compress/CompressGzipstream.php b/src/Ifsnop/Mysqldump/Compress/CompressGzipstream.php new file mode 100644 index 00000000..6c948790 --- /dev/null +++ b/src/Ifsnop/Mysqldump/Compress/CompressGzipstream.php @@ -0,0 +1,42 @@ +fileHandler = fopen($filename, "wb"); + + if (false === $this->fileHandler) { + throw new Exception("Output file is not writable"); + } + + $this->compressContext = deflate_init(ZLIB_ENCODING_GZIP, ['level' => 9]); + + return true; + } + + public function write(string $str): int + { + $bytesWritten = fwrite($this->fileHandler, deflate_add($this->compressContext, $str, ZLIB_NO_FLUSH)); + + if (false === $bytesWritten) { + throw new Exception("Writing to file failed! Probably, there is no more free space left?"); + } + + return $bytesWritten; + } + + public function close(): bool + { + fwrite($this->fileHandler, deflate_add($this->compressContext, '', ZLIB_FINISH)); + + return fclose($this->fileHandler); + } +} diff --git a/src/Ifsnop/Mysqldump/Compress/CompressInterface.php b/src/Ifsnop/Mysqldump/Compress/CompressInterface.php new file mode 100644 index 00000000..ae7b9696 --- /dev/null +++ b/src/Ifsnop/Mysqldump/Compress/CompressInterface.php @@ -0,0 +1,12 @@ +fileHandler = fopen($filename, "wb"); + + if (false === $this->fileHandler) { + throw new Exception("Output file is not writable"); + } + + return true; + } + + public function write(string $str): int + { + $bytesWritten = fwrite($this->fileHandler, $str); + + if (false === $bytesWritten) { + throw new Exception("Writing to file failed! Probably, there is no more free space left?"); + } + + return $bytesWritten; + } + + public function close(): bool + { + return fclose($this->fileHandler); + } +} diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index c069398a..03c81e60 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -16,6 +16,7 @@ namespace Ifsnop\Mysqldump; use Exception; +use Ifsnop\Mysqldump\Compress\CompressManagerFactory; use PDO; use PDOException; @@ -1302,1027 +1303,3 @@ public function getColumnNames($tableName) return $colNames; } } - -/** - * Enum with all available compression methods - * - */ -abstract class CompressMethod -{ - public static $enums = array( - Mysqldump::NONE, - Mysqldump::GZIP, - Mysqldump::BZIP2, - Mysqldump::GZIPSTREAM, - ); - - /** - * @param string $c - * @return boolean - */ - public static function isValid($c) - { - return in_array($c, self::$enums); - } -} - -abstract class CompressManagerFactory -{ - /** - * @param string $c - * @return CompressBzip2|CompressGzip|CompressNone - */ - public static function create($c) - { - $c = ucfirst(strtolower($c)); - if (!CompressMethod::isValid($c)) { - throw new Exception("Compression method ($c) is not defined yet"); - } - - $method = __NAMESPACE__."\\"."Compress".$c; - - return new $method; - } -} - -class CompressBzip2 extends CompressManagerFactory -{ - private $fileHandler = null; - - public function __construct() - { - if (!function_exists("bzopen")) { - throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly"); - } - } - - /** - * @param string $filename - */ - public function open($filename) - { - $this->fileHandler = bzopen($filename, "w"); - if (false === $this->fileHandler) { - throw new Exception("Output file is not writable"); - } - - return true; - } - - public function write($str) - { - $bytesWritten = bzwrite($this->fileHandler, $str); - if (false === $bytesWritten) { - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); - } - return $bytesWritten; - } - - public function close() - { - return bzclose($this->fileHandler); - } -} - -class CompressGzip extends CompressManagerFactory -{ - private $fileHandler = null; - - public function __construct() - { - if (!function_exists("gzopen")) { - throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly"); - } - } - - /** - * @param string $filename - */ - public function open($filename) - { - $this->fileHandler = gzopen($filename, "wb"); - if (false === $this->fileHandler) { - throw new Exception("Output file is not writable"); - } - - return true; - } - - public function write($str) - { - $bytesWritten = gzwrite($this->fileHandler, $str); - if (false === $bytesWritten) { - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); - } - return $bytesWritten; - } - - public function close() - { - return gzclose($this->fileHandler); - } -} - -class CompressNone extends CompressManagerFactory -{ - private $fileHandler = null; - - /** - * @param string $filename - */ - public function open($filename) - { - $this->fileHandler = fopen($filename, "wb"); - if (false === $this->fileHandler) { - throw new Exception("Output file is not writable"); - } - - return true; - } - - public function write($str) - { - $bytesWritten = fwrite($this->fileHandler, $str); - if (false === $bytesWritten) { - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); - } - return $bytesWritten; - } - - public function close() - { - return fclose($this->fileHandler); - } -} - -class CompressGzipstream extends CompressManagerFactory -{ - private $fileHandler = null; - - private $compressContext; - - /** - * @param string $filename - */ - public function open($filename) - { - $this->fileHandler = fopen($filename, "wb"); - if (false === $this->fileHandler) { - throw new Exception("Output file is not writable"); - } - - $this->compressContext = deflate_init(ZLIB_ENCODING_GZIP, array('level' => 9)); - return true; - } - - public function write($str) - { - - $bytesWritten = fwrite($this->fileHandler, deflate_add($this->compressContext, $str, ZLIB_NO_FLUSH)); - if (false === $bytesWritten) { - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); - } - return $bytesWritten; - } - - public function close() - { - fwrite($this->fileHandler, deflate_add($this->compressContext, '', ZLIB_FINISH)); - return fclose($this->fileHandler); - } -} - -/** - * Enum with all available TypeAdapter implementations - * - */ -abstract class TypeAdapter -{ - public static $enums = array( - "Sqlite", - "Mysql" - ); - - /** - * @param string $c - * @return boolean - */ - public static function isValid($c) - { - return in_array($c, self::$enums); - } -} - -/** - * TypeAdapter Factory - * - */ -abstract class TypeAdapterFactory -{ - protected $dbHandler = null; - protected $dumpSettings = array(); - - /** - * @param string $c Type of database factory to create (Mysql, Sqlite,...) - * @param PDO $dbHandler - */ - public static function create($c, $dbHandler = null, $dumpSettings = array()) - { - $c = ucfirst(strtolower($c)); - if (!TypeAdapter::isValid($c)) { - throw new Exception("Database type support for ($c) not yet available"); - } - $method = __NAMESPACE__."\\"."TypeAdapter".$c; - return new $method($dbHandler, $dumpSettings); - } - - public function __construct($dbHandler = null, $dumpSettings = array()) - { - $this->dbHandler = $dbHandler; - $this->dumpSettings = $dumpSettings; - } - - /** - * function databases Add sql to create and use database - * @todo make it do something with sqlite - */ - public function databases() - { - return ""; - } - - public function show_create_table($tableName) - { - return "SELECT tbl_name as 'Table', sql as 'Create Table' ". - "FROM sqlite_master ". - "WHERE type='table' AND tbl_name='$tableName'"; - } - - /** - * function create_table Get table creation code from database - * @todo make it do something with sqlite - */ - public function create_table($row) - { - return ""; - } - - public function show_create_view($viewName) - { - return "SELECT tbl_name as 'View', sql as 'Create View' ". - "FROM sqlite_master ". - "WHERE type='view' AND tbl_name='$viewName'"; - } - - /** - * function create_view Get view creation code from database - * @todo make it do something with sqlite - */ - public function create_view($row) - { - return ""; - } - - /** - * function show_create_trigger Get trigger creation code from database - * @todo make it do something with sqlite - */ - public function show_create_trigger($triggerName) - { - return ""; - } - - /** - * function create_trigger Modify trigger code, add delimiters, etc - * @todo make it do something with sqlite - */ - public function create_trigger($triggerName) - { - return ""; - } - - /** - * function create_procedure Modify procedure code, add delimiters, etc - * @todo make it do something with sqlite - */ - public function create_procedure($procedureName) - { - return ""; - } - - /** - * function create_function Modify function code, add delimiters, etc - * @todo make it do something with sqlite - */ - public function create_function($functionName) - { - return ""; - } - - public function show_tables() - { - return "SELECT tbl_name FROM sqlite_master WHERE type='table'"; - } - - public function show_views() - { - return "SELECT tbl_name FROM sqlite_master WHERE type='view'"; - } - - public function show_triggers() - { - return "SELECT name FROM sqlite_master WHERE type='trigger'"; - } - - public function show_columns() - { - if (func_num_args() != 1) { - return ""; - } - - $args = func_get_args(); - - return "pragma table_info(${args[0]})"; - } - - public function show_procedures() - { - return ""; - } - - public function show_functions() - { - return ""; - } - - public function show_events() - { - return ""; - } - - public function setup_transaction() - { - return ""; - } - - public function start_transaction() - { - return "BEGIN EXCLUSIVE"; - } - - public function commit_transaction() - { - return "COMMIT"; - } - - public function lock_table() - { - return ""; - } - - public function unlock_table() - { - return ""; - } - - public function start_add_lock_table() - { - return PHP_EOL; - } - - public function end_add_lock_table() - { - return PHP_EOL; - } - - public function start_add_disable_keys() - { - return PHP_EOL; - } - - public function end_add_disable_keys() - { - return PHP_EOL; - } - - public function start_disable_foreign_keys_check() - { - return PHP_EOL; - } - - public function end_disable_foreign_keys_check() - { - return PHP_EOL; - } - - public function add_drop_database() - { - return PHP_EOL; - } - - public function add_drop_trigger() - { - return PHP_EOL; - } - - public function drop_table() - { - return PHP_EOL; - } - - public function drop_view() - { - return PHP_EOL; - } - - /** - * Decode column metadata and fill info structure. - * type, is_numeric and is_blob will always be available. - * - * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" - * @return array - */ - public function parseColumnType($colType) - { - return array(); - } - - public function backup_parameters() - { - return PHP_EOL; - } - - public function restore_parameters() - { - return PHP_EOL; - } -} - -class TypeAdapterPgsql extends TypeAdapterFactory -{ -} - -class TypeAdapterDblib extends TypeAdapterFactory -{ -} - -class TypeAdapterSqlite extends TypeAdapterFactory -{ -} - -class TypeAdapterMysql extends TypeAdapterFactory -{ - const DEFINER_RE = 'DEFINER=`(?:[^`]|``)*`@`(?:[^`]|``)*`'; - - - // Numerical Mysql types - public $mysqlTypes = array( - 'numerical' => array( - 'bit', - 'tinyint', - 'smallint', - 'mediumint', - 'int', - 'integer', - 'bigint', - 'real', - 'double', - 'float', - 'decimal', - 'numeric' - ), - 'blob' => array( - 'tinyblob', - 'blob', - 'mediumblob', - 'longblob', - 'binary', - 'varbinary', - 'bit', - 'geometry', /* http://bugs.mysql.com/bug.php?id=43544 */ - 'point', - 'linestring', - 'polygon', - 'multipoint', - 'multilinestring', - 'multipolygon', - 'geometrycollection', - ) - ); - - public function databases() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - $databaseName = $args[0]; - - $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';"); - $characterSet = $resultSet->fetchColumn(1); - $resultSet->closeCursor(); - - $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';"); - $collationDb = $resultSet->fetchColumn(1); - $resultSet->closeCursor(); - $ret = ""; - - $ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`". - " /*!40100 DEFAULT CHARACTER SET ${characterSet} ". - " COLLATE ${collationDb} */;".PHP_EOL.PHP_EOL. - "USE `${databaseName}`;".PHP_EOL.PHP_EOL; - - return $ret; - } - - public function show_create_table($tableName) - { - return "SHOW CREATE TABLE `$tableName`"; - } - - public function show_create_view($viewName) - { - return "SHOW CREATE VIEW `$viewName`"; - } - - public function show_create_trigger($triggerName) - { - return "SHOW CREATE TRIGGER `$triggerName`"; - } - - public function show_create_procedure($procedureName) - { - return "SHOW CREATE PROCEDURE `$procedureName`"; - } - - public function show_create_function($functionName) - { - return "SHOW CREATE FUNCTION `$functionName`"; - } - - public function show_create_event($eventName) - { - return "SHOW CREATE EVENT `$eventName`"; - } - - public function create_table($row) - { - if (!isset($row['Create Table'])) { - throw new Exception("Error getting table code, unknown output"); - } - - $createTable = $row['Create Table']; - if ($this->dumpSettings['reset-auto-increment']) { - $match = "/AUTO_INCREMENT=[0-9]+/s"; - $replace = ""; - $createTable = preg_replace($match, $replace, $createTable); - } - - if ($this->dumpSettings['if-not-exists'] ) { - $createTable = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $createTable); - } - - $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. - "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. - $createTable.";".PHP_EOL. - "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL. - PHP_EOL; - return $ret; - } - - public function create_view($row) - { - $ret = ""; - if (!isset($row['Create View'])) { - throw new Exception("Error getting view structure, unknown output"); - } - - $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.$definerStr.'/*!50001 \3 */', - $viewStmt, - 1 - )) { - $viewStmt = $viewStmtReplaced; - }; - - $ret .= $viewStmt.';'.PHP_EOL.PHP_EOL; - return $ret; - } - - public function create_trigger($row) - { - $ret = ""; - if (!isset($row['SQL Original Statement'])) { - throw new Exception("Error getting trigger code, unknown output"); - } - - $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*/ '.$definerStr.'/*!50003 \3 */', - $triggerStmt, - 1 - )) { - $triggerStmt = $triggerStmtReplaced; - } - - $ret .= "DELIMITER ;;".PHP_EOL. - $triggerStmt.";;".PHP_EOL. - "DELIMITER ;".PHP_EOL.PHP_EOL; - return $ret; - } - - public function create_procedure($row) - { - $ret = ""; - if (!isset($row['Create Procedure'])) { - throw new Exception("Error getting procedure code, unknown output. ". - "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. - "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. - "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. - "DELIMITER ;;".PHP_EOL. - $procedureStmt." ;;".PHP_EOL. - "DELIMITER ;".PHP_EOL. - "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL.PHP_EOL; - - return $ret; - } - - public function create_function($row) - { - $ret = ""; - if (!isset($row['Create Function'])) { - throw new Exception("Error getting function code, unknown output. ". - "Please check 'https://bugs.mysql.com/bug.php?id=14564'"); - } - $functionStmt = $row['Create Function']; - $characterSetClient = $row['character_set_client']; - $collationConnection = $row['collation_connection']; - $sqlMode = $row['sql_mode']; - 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. - "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. - "/*!50003 SET @saved_cs_results = @@character_set_results */ ;".PHP_EOL. - "/*!50003 SET @saved_col_connection = @@collation_connection */ ;".PHP_EOL. - "/*!40101 SET character_set_client = ".$characterSetClient." */;".PHP_EOL. - "/*!40101 SET character_set_results = ".$characterSetClient." */;".PHP_EOL. - "/*!50003 SET collation_connection = ".$collationConnection." */ ;".PHP_EOL. - "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. - "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. - "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. - "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. - "DELIMITER ;;".PHP_EOL. - $functionStmt." ;;".PHP_EOL. - "DELIMITER ;".PHP_EOL. - "/*!50003 SET sql_mode = @saved_sql_mode */ ;".PHP_EOL. - "/*!50003 SET character_set_client = @saved_cs_client */ ;".PHP_EOL. - "/*!50003 SET character_set_results = @saved_cs_results */ ;".PHP_EOL. - "/*!50003 SET collation_connection = @saved_col_connection */ ;".PHP_EOL. - "/*!50106 SET TIME_ZONE= @saved_time_zone */ ;".PHP_EOL.PHP_EOL; - - - return $ret; - } - - public function create_event($row) - { - $ret = ""; - if (!isset($row['Create Event'])) { - throw new Exception("Error getting event code, unknown output. ". - "Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'"); - } - $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*/ '.$definerStr.'/*!50106 \3 */', - $eventStmt, - 1 - )) { - $eventStmt = $eventStmtReplaced; - } - - $ret .= "/*!50106 SET @save_time_zone= @@TIME_ZONE */ ;".PHP_EOL. - "/*!50106 DROP EVENT IF EXISTS `".$eventName."` */;".PHP_EOL. - "DELIMITER ;;".PHP_EOL. - "/*!50003 SET @saved_cs_client = @@character_set_client */ ;;".PHP_EOL. - "/*!50003 SET @saved_cs_results = @@character_set_results */ ;;".PHP_EOL. - "/*!50003 SET @saved_col_connection = @@collation_connection */ ;;".PHP_EOL. - "/*!50003 SET character_set_client = utf8 */ ;;".PHP_EOL. - "/*!50003 SET character_set_results = utf8 */ ;;".PHP_EOL. - "/*!50003 SET collation_connection = utf8_general_ci */ ;;".PHP_EOL. - "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. - "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. - "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. - "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. - $eventStmt." ;;".PHP_EOL. - "/*!50003 SET time_zone = @saved_time_zone */ ;;".PHP_EOL. - "/*!50003 SET sql_mode = @saved_sql_mode */ ;;".PHP_EOL. - "/*!50003 SET character_set_client = @saved_cs_client */ ;;".PHP_EOL. - "/*!50003 SET character_set_results = @saved_cs_results */ ;;".PHP_EOL. - "/*!50003 SET collation_connection = @saved_col_connection */ ;;".PHP_EOL. - "DELIMITER ;".PHP_EOL. - "/*!50106 SET TIME_ZONE= @save_time_zone */ ;".PHP_EOL.PHP_EOL; - // Commented because we are doing this in restore_parameters() - // "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL . PHP_EOL; - - return $ret; - } - - public function show_tables() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SELECT TABLE_NAME AS tbl_name ". - "FROM INFORMATION_SCHEMA.TABLES ". - "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; - } - - public function show_views() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SELECT TABLE_NAME AS tbl_name ". - "FROM INFORMATION_SCHEMA.TABLES ". - "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; - } - - public function show_triggers() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SHOW TRIGGERS FROM `${args[0]}`;"; - } - - public function show_columns() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SHOW COLUMNS FROM `${args[0]}`;"; - } - - public function show_procedures() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SELECT SPECIFIC_NAME AS procedure_name ". - "FROM INFORMATION_SCHEMA.ROUTINES ". - "WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'"; - } - - public function show_functions() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SELECT SPECIFIC_NAME AS function_name ". - "FROM INFORMATION_SCHEMA.ROUTINES ". - "WHERE ROUTINE_TYPE='FUNCTION' AND ROUTINE_SCHEMA='${args[0]}'"; - } - - /** - * Get query string to ask for names of events from current database. - * - * @param string Name of database - * @return string - */ - public function show_events() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "SELECT EVENT_NAME AS event_name ". - "FROM INFORMATION_SCHEMA.EVENTS ". - "WHERE EVENT_SCHEMA='${args[0]}'"; - } - - public function setup_transaction() - { - return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; - } - - public function start_transaction() - { - return "START TRANSACTION ". - "/*!40100 WITH CONSISTENT SNAPSHOT */"; - } - - - public function commit_transaction() - { - return "COMMIT"; - } - - public function lock_table() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL"); - } - - public function unlock_table() - { - return $this->dbHandler->exec("UNLOCK TABLES"); - } - - public function start_add_lock_table() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "LOCK TABLES `${args[0]}` WRITE;".PHP_EOL; - } - - public function end_add_lock_table() - { - return "UNLOCK TABLES;".PHP_EOL; - } - - public function start_add_disable_keys() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;". - PHP_EOL; - } - - public function end_add_disable_keys() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;". - PHP_EOL; - } - - public function start_disable_autocommit() - { - return "SET autocommit=0;".PHP_EOL; - } - - public function end_disable_autocommit() - { - return "COMMIT;".PHP_EOL; - } - - public function add_drop_database() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;". - PHP_EOL.PHP_EOL; - } - - public function add_drop_trigger() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "DROP TRIGGER IF EXISTS `${args[0]}`;".PHP_EOL; - } - - public function drop_table() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL; - } - - public function drop_view() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL. - "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;".PHP_EOL; - } - - public function getDatabaseHeader() - { - $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); - $args = func_get_args(); - return "--".PHP_EOL. - "-- Current Database: `${args[0]}`".PHP_EOL. - "--".PHP_EOL.PHP_EOL; - } - - /** - * Decode column metadata and fill info structure. - * type, is_numeric and is_blob will always be available. - * - * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" - * @return array - */ - public function parseColumnType($colType) - { - $colInfo = array(); - $colParts = explode(" ", $colType['Type']); - - if ($fparen = strpos($colParts[0], "(")) { - $colInfo['type'] = substr($colParts[0], 0, $fparen); - $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen + 1)); - $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : null; - } else { - $colInfo['type'] = $colParts[0]; - } - $colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']); - $colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']); - // for virtual columns that are of type 'Extra', column type - // could by "STORED GENERATED" or "VIRTUAL GENERATED" - // MySQL reference: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html - $colInfo['is_virtual'] = strpos($colType['Extra'], "VIRTUAL GENERATED") !== false || strpos($colType['Extra'], "STORED GENERATED") !== false; - - return $colInfo; - } - - public function backup_parameters() - { - $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;".PHP_EOL. - "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;".PHP_EOL. - "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;".PHP_EOL. - "/*!40101 SET NAMES ".$this->dumpSettings['default-character-set']." */;".PHP_EOL; - - if (false === $this->dumpSettings['skip-tz-utc']) { - $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;".PHP_EOL. - "/*!40103 SET TIME_ZONE='+00:00' */;".PHP_EOL; - } - - if ($this->dumpSettings['no-autocommit']) { - $ret .= "/*!40101 SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT */;".PHP_EOL; - } - - $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;".PHP_EOL. - "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;".PHP_EOL. - "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;".PHP_EOL. - "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".PHP_EOL.PHP_EOL; - - return $ret; - } - - public function restore_parameters() - { - $ret = ""; - - if (false === $this->dumpSettings['skip-tz-utc']) { - $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;".PHP_EOL; - } - - if ($this->dumpSettings['no-autocommit']) { - $ret .= "/*!40101 SET AUTOCOMMIT=@OLD_AUTOCOMMIT */;".PHP_EOL; - } - - $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;".PHP_EOL. - "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;".PHP_EOL. - "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;".PHP_EOL. - "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;".PHP_EOL. - "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;".PHP_EOL. - "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;".PHP_EOL. - "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;".PHP_EOL.PHP_EOL; - - return $ret; - } - - /** - * Check number of parameters passed to function, useful when inheriting. - * Raise exception if unexpected. - * - * @param integer $num_args - * @param integer $expected_num_args - * @param string $method_name - */ - private function check_parameters($num_args, $expected_num_args, $method_name) - { - if ($num_args != $expected_num_args) { - throw new Exception("Unexpected parameter passed to $method_name"); - } - return; - } -} diff --git a/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapter.php b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapter.php new file mode 100644 index 00000000..de8fb176 --- /dev/null +++ b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapter.php @@ -0,0 +1,19 @@ +dbHandler = $dbHandler; + $this->dumpSettings = $dumpSettings; + } + + /** + * function databases Add sql to create and use database + * @todo make it do something with sqlite + */ + public function databases(): string + { + return ""; + } + + public function show_create_table($tableName): string + { + return "SELECT tbl_name as 'Table', sql as 'Create Table' ". + "FROM sqlite_master ". + "WHERE type='table' AND tbl_name='$tableName'"; + } + + /** + * function create_table Get table creation code from database + * @todo make it do something with sqlite + */ + public function create_table($row): string + { + return ""; + } + + public function show_create_view($viewName): string + { + return "SELECT tbl_name as 'View', sql as 'Create View' ". + "FROM sqlite_master ". + "WHERE type='view' AND tbl_name='$viewName'"; + } + + /** + * function create_view Get view creation code from database + * @todo make it do something with sqlite + */ + public function create_view($row): string + { + return ""; + } + + /** + * function show_create_trigger Get trigger creation code from database + * @todo make it do something with sqlite + */ + public function show_create_trigger($triggerName): string + { + return ""; + } + + /** + * function create_trigger Modify trigger code, add delimiters, etc + * @todo make it do something with sqlite + */ + public function create_trigger($triggerName): string + { + return ""; + } + + /** + * function create_procedure Modify procedure code, add delimiters, etc + * @todo make it do something with sqlite + */ + public function create_procedure($procedureName): string + { + return ""; + } + + /** + * function create_function Modify function code, add delimiters, etc + * @todo make it do something with sqlite + */ + public function create_function($functionName): string + { + return ""; + } + + public function show_tables(): string + { + return "SELECT tbl_name FROM sqlite_master WHERE type='table'"; + } + + public function show_views(): string + { + return "SELECT tbl_name FROM sqlite_master WHERE type='view'"; + } + + public function show_triggers(): string + { + return "SELECT name FROM sqlite_master WHERE type='trigger'"; + } + + public function show_columns(): string + { + if (func_num_args() != 1) { + return ""; + } + + $args = func_get_args(); + + return "pragma table_info(${args[0]})"; + } + + public function show_procedures(): string + { + return ""; + } + + public function show_functions(): string + { + return ""; + } + + public function show_events(): string + { + return ""; + } + + public function setup_transaction(): string + { + return ""; + } + + public function start_transaction(): string + { + return "BEGIN EXCLUSIVE"; + } + + public function commit_transaction(): string + { + return "COMMIT"; + } + + public function lock_table(): string + { + return ""; + } + + public function unlock_table(): string + { + return ""; + } + + public function start_add_lock_table(): string + { + return PHP_EOL; + } + + public function end_add_lock_table(): string + { + return PHP_EOL; + } + + public function start_add_disable_keys(): string + { + return PHP_EOL; + } + + public function end_add_disable_keys(): string + { + return PHP_EOL; + } + + public function start_disable_foreign_keys_check(): string + { + return PHP_EOL; + } + + public function end_disable_foreign_keys_check(): string + { + return PHP_EOL; + } + + public function add_drop_database(): string + { + return PHP_EOL; + } + + public function add_drop_trigger(): string + { + return PHP_EOL; + } + + public function drop_table(): string + { + return PHP_EOL; + } + + public function drop_view(): string + { + return PHP_EOL; + } + + /** + * Decode column metadata and fill info structure. + * type, is_numeric and is_blob will always be available. + * + * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" + * @return array + */ + public function parseColumnType(array $colType): array + { + return []; + } + + public function backup_parameters(): string + { + return PHP_EOL; + } + + public function restore_parameters(): string + { + return PHP_EOL; + } +} diff --git a/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterMysql.php b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterMysql.php new file mode 100644 index 00000000..735b6e7f --- /dev/null +++ b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterMysql.php @@ -0,0 +1,547 @@ + [ + 'bit', + 'tinyint', + 'smallint', + 'mediumint', + 'int', + 'integer', + 'bigint', + 'real', + 'double', + 'float', + 'decimal', + 'numeric' + ], + 'blob' => [ + 'tinyblob', + 'blob', + 'mediumblob', + 'longblob', + 'binary', + 'varbinary', + 'bit', + 'geometry', /* http://bugs.mysql.com/bug.php?id=43544 */ + 'point', + 'linestring', + 'polygon', + 'multipoint', + 'multilinestring', + 'multipolygon', + 'geometrycollection', + ] + ]; + + public function databases(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + $databaseName = $args[0]; + + $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';"); + $characterSet = $resultSet->fetchColumn(1); + $resultSet->closeCursor(); + + $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';"); + $collationDb = $resultSet->fetchColumn(1); + $resultSet->closeCursor(); + + return "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`" . + " /*!40100 DEFAULT CHARACTER SET ${characterSet} " . + " COLLATE ${collationDb} */;" . PHP_EOL . PHP_EOL . + "USE `${databaseName}`;" . PHP_EOL . PHP_EOL; + } + + public function show_create_table($tableName): string + { + return "SHOW CREATE TABLE `$tableName`"; + } + + public function show_create_view($viewName): string + { + return "SHOW CREATE VIEW `$viewName`"; + } + + public function show_create_trigger($triggerName): string + { + return "SHOW CREATE TRIGGER `$triggerName`"; + } + + public function show_create_procedure($procedureName): string + { + return "SHOW CREATE PROCEDURE `$procedureName`"; + } + + public function show_create_function($functionName): string + { + return "SHOW CREATE FUNCTION `$functionName`"; + } + + public function show_create_event($eventName): string + { + return "SHOW CREATE EVENT `$eventName`"; + } + + public function create_table($row): string + { + if (!isset($row['Create Table'])) { + throw new Exception("Error getting table code, unknown output"); + } + + $createTable = $row['Create Table']; + if ($this->dumpSettings['reset-auto-increment']) { + $match = "/AUTO_INCREMENT=[0-9]+/s"; + $replace = ""; + $createTable = preg_replace($match, $replace, $createTable); + } + + if ($this->dumpSettings['if-not-exists'] ) { + $createTable = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $createTable); + } + + return "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. + "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. + $createTable.";".PHP_EOL. + "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL. + PHP_EOL; + } + + public function create_view($row): string + { + $ret = ""; + if (!isset($row['Create View'])) { + throw new Exception("Error getting view structure, unknown output"); + } + + $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.$definerStr.'/*!50001 \3 */', + $viewStmt, + 1 + )) { + $viewStmt = $viewStmtReplaced; + }; + + $ret .= $viewStmt.';'.PHP_EOL.PHP_EOL; + return $ret; + } + + public function create_trigger($row): string + { + $ret = ""; + if (!isset($row['SQL Original Statement'])) { + throw new Exception("Error getting trigger code, unknown output"); + } + + $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*/ '.$definerStr.'/*!50003 \3 */', + $triggerStmt, + 1 + )) { + $triggerStmt = $triggerStmtReplaced; + } + + $ret .= "DELIMITER ;;".PHP_EOL. + $triggerStmt.";;".PHP_EOL. + "DELIMITER ;".PHP_EOL.PHP_EOL; + return $ret; + } + + public function create_procedure($row): string + { + $ret = ""; + if (!isset($row['Create Procedure'])) { + throw new Exception("Error getting procedure code, unknown output. ". + "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. + "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. + "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. + "DELIMITER ;;".PHP_EOL. + $procedureStmt." ;;".PHP_EOL. + "DELIMITER ;".PHP_EOL. + "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL.PHP_EOL; + + return $ret; + } + + public function create_function($row): string + { + $ret = ""; + if (!isset($row['Create Function'])) { + throw new Exception("Error getting function code, unknown output. ". + "Please check 'https://bugs.mysql.com/bug.php?id=14564'"); + } + $functionStmt = $row['Create Function']; + $characterSetClient = $row['character_set_client']; + $collationConnection = $row['collation_connection']; + $sqlMode = $row['sql_mode']; + 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. + "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. + "/*!50003 SET @saved_cs_results = @@character_set_results */ ;".PHP_EOL. + "/*!50003 SET @saved_col_connection = @@collation_connection */ ;".PHP_EOL. + "/*!40101 SET character_set_client = ".$characterSetClient." */;".PHP_EOL. + "/*!40101 SET character_set_results = ".$characterSetClient." */;".PHP_EOL. + "/*!50003 SET collation_connection = ".$collationConnection." */ ;".PHP_EOL. + "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. + "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. + "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. + "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. + "DELIMITER ;;".PHP_EOL. + $functionStmt." ;;".PHP_EOL. + "DELIMITER ;".PHP_EOL. + "/*!50003 SET sql_mode = @saved_sql_mode */ ;".PHP_EOL. + "/*!50003 SET character_set_client = @saved_cs_client */ ;".PHP_EOL. + "/*!50003 SET character_set_results = @saved_cs_results */ ;".PHP_EOL. + "/*!50003 SET collation_connection = @saved_col_connection */ ;".PHP_EOL. + "/*!50106 SET TIME_ZONE= @saved_time_zone */ ;".PHP_EOL.PHP_EOL; + + + return $ret; + } + + public function create_event($row): string + { + $ret = ""; + if (!isset($row['Create Event'])) { + throw new Exception("Error getting event code, unknown output. ". + "Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'"); + } + $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*/ '.$definerStr.'/*!50106 \3 */', + $eventStmt, + 1 + )) { + $eventStmt = $eventStmtReplaced; + } + + $ret .= "/*!50106 SET @save_time_zone= @@TIME_ZONE */ ;".PHP_EOL. + "/*!50106 DROP EVENT IF EXISTS `".$eventName."` */;".PHP_EOL. + "DELIMITER ;;".PHP_EOL. + "/*!50003 SET @saved_cs_client = @@character_set_client */ ;;".PHP_EOL. + "/*!50003 SET @saved_cs_results = @@character_set_results */ ;;".PHP_EOL. + "/*!50003 SET @saved_col_connection = @@collation_connection */ ;;".PHP_EOL. + "/*!50003 SET character_set_client = utf8 */ ;;".PHP_EOL. + "/*!50003 SET character_set_results = utf8 */ ;;".PHP_EOL. + "/*!50003 SET collation_connection = utf8_general_ci */ ;;".PHP_EOL. + "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. + "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. + "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. + "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. + $eventStmt." ;;".PHP_EOL. + "/*!50003 SET time_zone = @saved_time_zone */ ;;".PHP_EOL. + "/*!50003 SET sql_mode = @saved_sql_mode */ ;;".PHP_EOL. + "/*!50003 SET character_set_client = @saved_cs_client */ ;;".PHP_EOL. + "/*!50003 SET character_set_results = @saved_cs_results */ ;;".PHP_EOL. + "/*!50003 SET collation_connection = @saved_col_connection */ ;;".PHP_EOL. + "DELIMITER ;".PHP_EOL. + "/*!50106 SET TIME_ZONE= @save_time_zone */ ;".PHP_EOL.PHP_EOL; + // Commented because we are doing this in restore_parameters() + // "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL . PHP_EOL; + + return $ret; + } + + public function show_tables(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SELECT TABLE_NAME AS tbl_name ". + "FROM INFORMATION_SCHEMA.TABLES ". + "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; + } + + public function show_views(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SELECT TABLE_NAME AS tbl_name ". + "FROM INFORMATION_SCHEMA.TABLES ". + "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; + } + + public function show_triggers(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SHOW TRIGGERS FROM `${args[0]}`;"; + } + + public function show_columns(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SHOW COLUMNS FROM `${args[0]}`;"; + } + + public function show_procedures(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SELECT SPECIFIC_NAME AS procedure_name ". + "FROM INFORMATION_SCHEMA.ROUTINES ". + "WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'"; + } + + public function show_functions(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SELECT SPECIFIC_NAME AS function_name ". + "FROM INFORMATION_SCHEMA.ROUTINES ". + "WHERE ROUTINE_TYPE='FUNCTION' AND ROUTINE_SCHEMA='${args[0]}'"; + } + + /** + * Get query string to ask for names of events from current database. + * + * @return string + */ + public function show_events(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "SELECT EVENT_NAME AS event_name ". + "FROM INFORMATION_SCHEMA.EVENTS ". + "WHERE EVENT_SCHEMA='${args[0]}'"; + } + + public function setup_transaction(): string + { + return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; + } + + public function start_transaction(): string + { + return "START TRANSACTION ". + "/*!40100 WITH CONSISTENT SNAPSHOT */"; + } + + public function commit_transaction(): string + { + return "COMMIT"; + } + + public function lock_table(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL"); + } + + public function unlock_table(): string + { + return $this->dbHandler->exec("UNLOCK TABLES"); + } + + public function start_add_lock_table(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "LOCK TABLES `${args[0]}` WRITE;".PHP_EOL; + } + + public function end_add_lock_table(): string + { + return "UNLOCK TABLES;".PHP_EOL; + } + + public function start_add_disable_keys(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;". PHP_EOL; + } + + public function end_add_disable_keys(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;". PHP_EOL; + } + + public function start_disable_autocommit(): string + { + return "SET autocommit=0;".PHP_EOL; + } + + public function end_disable_autocommit(): string + { + return "COMMIT;".PHP_EOL; + } + + public function add_drop_database(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;". PHP_EOL.PHP_EOL; + } + + public function add_drop_trigger(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "DROP TRIGGER IF EXISTS `${args[0]}`;".PHP_EOL; + } + + public function drop_table(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL; + } + + public function drop_view(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL. + "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;".PHP_EOL; + } + + public function getDatabaseHeader(): string + { + $this->check_parameters(func_num_args(), 1, __METHOD__); + $args = func_get_args(); + return "--".PHP_EOL. + "-- Current Database: `${args[0]}`".PHP_EOL. + "--".PHP_EOL.PHP_EOL; + } + + /** + * Decode column metadata and fill info structure. + * type, is_numeric and is_blob will always be available. + * + * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" + * @return array + */ + public function parseColumnType(array $colType): array + { + $colInfo = []; + $colParts = explode(" ", $colType['Type']); + + if ($fparen = strpos($colParts[0], "(")) { + $colInfo['type'] = substr($colParts[0], 0, $fparen); + $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen + 1)); + $colInfo['attributes'] = $colParts[1] ?? null; + } else { + $colInfo['type'] = $colParts[0]; + } + $colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']); + $colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']); + // for virtual columns that are of type 'Extra', column type + // could by "STORED GENERATED" or "VIRTUAL GENERATED" + // MySQL reference: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html + $colInfo['is_virtual'] = strpos($colType['Extra'], "VIRTUAL GENERATED") !== false || strpos($colType['Extra'], "STORED GENERATED") !== false; + + return $colInfo; + } + + public function backup_parameters(): string + { + $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;".PHP_EOL. + "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;".PHP_EOL. + "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;".PHP_EOL. + "/*!40101 SET NAMES ".$this->dumpSettings['default-character-set']." */;".PHP_EOL; + + if (false === $this->dumpSettings['skip-tz-utc']) { + $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;".PHP_EOL. + "/*!40103 SET TIME_ZONE='+00:00' */;".PHP_EOL; + } + + if ($this->dumpSettings['no-autocommit']) { + $ret .= "/*!40101 SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT */;".PHP_EOL; + } + + $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;".PHP_EOL. + "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;".PHP_EOL. + "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;".PHP_EOL. + "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".PHP_EOL.PHP_EOL; + + return $ret; + } + + public function restore_parameters(): string + { + $ret = ""; + + if (false === $this->dumpSettings['skip-tz-utc']) { + $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;".PHP_EOL; + } + + if ($this->dumpSettings['no-autocommit']) { + $ret .= "/*!40101 SET AUTOCOMMIT=@OLD_AUTOCOMMIT */;".PHP_EOL; + } + + $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;".PHP_EOL. + "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;".PHP_EOL. + "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;".PHP_EOL. + "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;".PHP_EOL. + "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;".PHP_EOL. + "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;".PHP_EOL. + "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;".PHP_EOL.PHP_EOL; + + return $ret; + } + + /** + * Check number of parameters passed to function, useful when inheriting. + * Raise exception if unexpected. + */ + private function check_parameters(int $num_args, int $expected_num_args, string $method_name) + { + if ($num_args != $expected_num_args) { + throw new Exception("Unexpected parameter passed to $method_name"); + } + } +} diff --git a/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterPgsql.php b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterPgsql.php new file mode 100644 index 00000000..75fd157e --- /dev/null +++ b/src/Ifsnop/Mysqldump/TypeAdapter/TypeAdapterPgsql.php @@ -0,0 +1,7 @@ + Date: Thu, 18 Aug 2022 15:36:36 +0300 Subject: [PATCH 03/19] Try initing mysql in GHA --- .github/workflows/tests.yml | 15 +++++++++++ tests/scripts/create_users.sh | 50 +++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bdcc3325..02a0cb17 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,6 +13,18 @@ jobs: runs-on: ubuntu-latest + services: + mysql: + image: mysql:5.7 + env: + MYSQL_DATABASE: test_db + MYSQL_USER: user + MYSQL_PASSWORD: password + MYSQL_ROOT_PASSWORD: rootpassword + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + strategy: matrix: php-versions: ['7.4', '8.0', '8.1'] @@ -38,3 +50,6 @@ jobs: - name: Run PHPunit tests run: vendor/bin/phpunit + + - name: Create user and databases for testing + run: ./tests/scripts/create_users.sh diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index 124955ee..30fddf20 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,26 +1,30 @@ #!/bin/bash -mysql -u root -e "CREATE USER 'travis'@'%';" -mysql -u root -e "CREATE DATABASE test001;" -mysql -u root -e "CREATE DATABASE test002;" -mysql -u root -e "CREATE DATABASE test005;" -mysql -u root -e "CREATE DATABASE test006a;" -mysql -u root -e "CREATE DATABASE test006b;" -mysql -u root -e "CREATE DATABASE test008;" -mysql -u root -e "CREATE DATABASE test009;" -mysql -u root -e "CREATE DATABASE test010;" -mysql -u root -e "CREATE DATABASE test011;" -mysql -u root -e "CREATE DATABASE test012;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test001.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test002.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test005.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test006a.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test006b.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test008.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO 'travis'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';" -mysql -u root -e "GRANT SELECT ON mysql.proc to 'travis'@'%';" +USER=${1:-drupal} +#mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" +mysql -u root -e "CREATE USER IF NOT EXISTS '$USER'@'%';" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test001;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test002;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test005;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test006a;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test006b;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test008;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test009;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test010;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test011;" +mysql -u root -e "CREATE DATABASE IF NOT EXISTS test012;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test001.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test002.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test005.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test006a.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test006b.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test008.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" +mysql -u root -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" mysql -u root -e "FLUSH PRIVILEGES;" + +echo "Done" From 6fb50759a1d0476a24af51eb4a5097fd54a2f2b2 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 15:44:15 +0300 Subject: [PATCH 04/19] Mysql envs in GHA --- .github/workflows/tests.yml | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 02a0cb17..bc222d66 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,22 +13,29 @@ jobs: runs-on: ubuntu-latest + strategy: + matrix: + mysql-versions: ['5.7'] + php-versions: ['7.4', '8.0', '8.1'] + + env: + MYSQL_DATABASE: test + MYSQL_USER: user + MYSQL_PASSWORD: secret + MYSQL_ROOT_PASSWORD: secretroot + services: mysql: - image: mysql:5.7 + image: mysql:${{ matrix.mysql-versions }} env: - MYSQL_DATABASE: test_db - MYSQL_USER: user - MYSQL_PASSWORD: password - MYSQL_ROOT_PASSWORD: rootpassword + MYSQL_DATABASE: ${{ env.DB_DATABASE }} + MYSQL_USER: ${{ env.MYSQL_USER }} + MYSQL_PASSWORD: ${{ env.MYSQL_PASSWORD }} + MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PASSWORD }} ports: - 3306:3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 - strategy: - matrix: - php-versions: ['7.4', '8.0', '8.1'] - steps: - name: Checkout code From 2190ed404d43dac8491d90c4e33c9c76d24cf512 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 15:49:33 +0300 Subject: [PATCH 05/19] Mysql root pass in GHA --- tests/scripts/create_users.sh | 49 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index 30fddf20..2dd604ca 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,30 +1,29 @@ #!/bin/bash USER=${1:-drupal} -#mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" -mysql -u root -e "CREATE USER IF NOT EXISTS '$USER'@'%';" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test001;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test002;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test005;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test006a;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test006b;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test008;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test009;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test010;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test011;" -mysql -u root -e "CREATE DATABASE IF NOT EXISTS test012;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test001.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test002.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test005.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test006a.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test006b.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test008.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" -mysql -u root -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" -mysql -u root -e "FLUSH PRIVILEGES;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test001;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test002;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test005;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test006a;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test006b;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test008;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test009;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test010;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test011;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test012;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test001.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test002.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test005.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test006a.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test006b.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test008.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" +mysql -u root -p$MYSQL_ROOT_PASSWORD -e "FLUSH PRIVILEGES;" echo "Done" From cb8511470c6376ff03da081f49f64840ae0f6466 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 16:08:46 +0300 Subject: [PATCH 06/19] MySQL 5.7 gha --- .github/workflows/tests.yml | 9 ++------- tests/scripts/create_users.sh | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bc222d66..a13d8b77 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -25,13 +25,8 @@ jobs: MYSQL_ROOT_PASSWORD: secretroot services: - mysql: - image: mysql:${{ matrix.mysql-versions }} - env: - MYSQL_DATABASE: ${{ env.DB_DATABASE }} - MYSQL_USER: ${{ env.MYSQL_USER }} - MYSQL_PASSWORD: ${{ env.MYSQL_PASSWORD }} - MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PASSWORD }} + db: + image: druidfi/mysql:${{ matrix.mysql-versions }}-drupal ports: - 3306:3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index 2dd604ca..d2df8c38 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,6 +1,6 @@ #!/bin/bash -USER=${1:-drupal} +USER=${1:-travis} mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test001;" mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test002;" From 1a8878dc21ad1a4e0c7402ffff5a073c0d54ac42 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 16:17:26 +0300 Subject: [PATCH 07/19] Use Mysql host 127.0.0.1 --- .gitignore | 2 +- tests/scripts/create_users.sh | 50 ++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index c54dc480..26762b99 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ /.settings # Vim swap files .*.sw* - +**/*.checksum /composer.lock /composer.phar /vendor/ diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index d2df8c38..8a7007b2 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,29 +1,31 @@ #!/bin/bash +MYSQL_CMD="mysql -h 127.0.0.1 -u root -p$MYSQL_ROOT_PASSWORD" USER=${1:-travis} -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test001;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test002;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test005;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test006a;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test006b;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test008;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test009;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test010;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test011;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS test012;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test001.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test002.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test005.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test006a.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test006b.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test008.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" -mysql -u root -p$MYSQL_ROOT_PASSWORD -e "FLUSH PRIVILEGES;" + +$MYSQL_CMD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test001;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test002;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test005;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test006a;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test006b;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test008;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test009;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test010;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test011;" +$MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test012;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test001.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test002.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test005.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test006a.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test006b.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test008.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" +$MYSQL_CMD -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" +$MYSQL_CMD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" +$MYSQL_CMD -e "FLUSH PRIVILEGES;" echo "Done" From d9e50c26732155fdcd3ad040386a36b155fc97be Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 18 Aug 2022 16:20:02 +0300 Subject: [PATCH 08/19] Remove env from gha --- .github/workflows/tests.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a13d8b77..0b7db85e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,12 +18,6 @@ jobs: mysql-versions: ['5.7'] php-versions: ['7.4', '8.0', '8.1'] - env: - MYSQL_DATABASE: test - MYSQL_USER: user - MYSQL_PASSWORD: secret - MYSQL_ROOT_PASSWORD: secretroot - services: db: image: druidfi/mysql:${{ matrix.mysql-versions }}-drupal From fbddbcd26620588a0bd1d5cb190ad907a0779f23 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 07:30:51 +0300 Subject: [PATCH 09/19] Use named host for db in GHA --- .github/workflows/tests.yml | 4 +++- tests/scripts/create_users.sh | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0b7db85e..883ae90b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,8 +15,10 @@ jobs: strategy: matrix: + #mysql-versions: ['5.7', '8.0'] mysql-versions: ['5.7'] - php-versions: ['7.4', '8.0', '8.1'] + #php-versions: ['7.4', '8.0', '8.1'] + php-versions: ['8.1'] services: db: diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index 8a7007b2..bd4ec6f1 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,7 +1,8 @@ #!/bin/bash -MYSQL_CMD="mysql -h 127.0.0.1 -u root -p$MYSQL_ROOT_PASSWORD" -USER=${1:-travis} +MYSQL_ROOT_PASSWORD=${1:-drupal} +MYSQL_CMD="mysql -h db -u root -p$MYSQL_ROOT_PASSWORD" +USER=tester $MYSQL_CMD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" $MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test001;" @@ -27,5 +28,7 @@ $MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTIO $MYSQL_CMD -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" $MYSQL_CMD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" $MYSQL_CMD -e "FLUSH PRIVILEGES;" +$MYSQL_CMD -e "use mysql; update user set authentication_string=PASSWORD('') where User='$USER'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" -echo "Done" +echo "Listing created databases with user '$USER'" +mysql -h db -u $USER -e "SHOW databases;" From ce6ebff70cbfbd1b7bf06d3d8eb5b83ae85c2eba Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 07:42:14 +0300 Subject: [PATCH 10/19] Use 127.0.0.1 for db in GHA --- .github/workflows/tests.yml | 2 +- tests/scripts/create_users.sh | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 883ae90b..1e44536c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -50,4 +50,4 @@ jobs: run: vendor/bin/phpunit - name: Create user and databases for testing - run: ./tests/scripts/create_users.sh + run: ./tests/scripts/create_users.sh 127.0.0.1 diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index bd4ec6f1..34c76104 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -1,7 +1,8 @@ #!/bin/bash -MYSQL_ROOT_PASSWORD=${1:-drupal} -MYSQL_CMD="mysql -h db -u root -p$MYSQL_ROOT_PASSWORD" +HOST=${1:-db} +MYSQL_ROOT_PASSWORD=drupal +MYSQL_CMD="mysql -h $HOST -u root -p$MYSQL_ROOT_PASSWORD" USER=tester $MYSQL_CMD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" @@ -31,4 +32,4 @@ $MYSQL_CMD -e "FLUSH PRIVILEGES;" $MYSQL_CMD -e "use mysql; update user set authentication_string=PASSWORD('') where User='$USER'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" echo "Listing created databases with user '$USER'" -mysql -h db -u $USER -e "SHOW databases;" +mysql -h $HOST -u $USER -e "SHOW databases;" From dfca897cacee94aeeb85ec09235d3c0b94410f7a Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 08:36:06 +0300 Subject: [PATCH 11/19] Update tests --- .github/workflows/tests.yml | 3 + src/Ifsnop/Mysqldump/Mysqldump.php | 1 + tests/scripts/create_users.sh | 2 +- tests/scripts/test.php | 134 ++++++++++++----------------- tests/scripts/test.sh | 61 +++++++------ 5 files changed, 94 insertions(+), 107 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1e44536c..aa46b9ce 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,3 +51,6 @@ jobs: - name: Create user and databases for testing run: ./tests/scripts/create_users.sh 127.0.0.1 + + - name: Run test script + run: ./tests/scripts/test.sh 127.0.0.1 diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 03c81e60..3f8a8ae5 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -17,6 +17,7 @@ use Exception; use Ifsnop\Mysqldump\Compress\CompressManagerFactory; +use Ifsnop\Mysqldump\TypeAdapter\TypeAdapterFactory; use PDO; use PDOException; diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index 34c76104..be5c8954 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -3,7 +3,7 @@ HOST=${1:-db} MYSQL_ROOT_PASSWORD=drupal MYSQL_CMD="mysql -h $HOST -u root -p$MYSQL_ROOT_PASSWORD" -USER=tester +USER=travis $MYSQL_CMD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" $MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test001;" diff --git a/tests/scripts/test.php b/tests/scripts/test.php index 0c581f1b..9fa2a22a 100644 --- a/tests/scripts/test.php +++ b/tests/scripts/test.php @@ -1,20 +1,18 @@ " . bin2hex(chr($i)) . "<" . PHP_EOL; -} -*/ -date_default_timezone_set('UTC'); +date_default_timezone_set('UTC'); error_reporting(E_ALL); -include_once(dirname(__FILE__) . "/../src/Ifsnop/Mysqldump/Mysqldump.php"); +require __DIR__ . '/../../vendor/autoload.php'; -use Ifsnop\Mysqldump as IMysqldump; +use Ifsnop\Mysqldump\Mysqldump; -$dumpSettings = array( - 'exclude-tables' => array('/^travis*/'), - 'compress' => IMysqldump\Mysqldump::NONE, +$host = 'db'; +$user = 'travis'; + +$dumpSettings = [ + 'exclude-tables' => ['/^travis*/'], + 'compress' => Mysqldump::NONE, 'no-data' => false, 'add-drop-table' => true, 'single-transaction' => true, @@ -30,136 +28,116 @@ 'hex-blob' => true, 'no-create-info' => false, 'where' => '' - ); +]; // do nothing test print "starting mysql-php_test000.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:host=localhost;dbname=test001", - "travis", - "" - ); +$dump = new Mysqldump("mysql:host=$host;dbname=test001", $user); print "starting mysql-php_test001.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:host=localhost;dbname=test001", - "travis", - "", - $dumpSettings); +$dump = new Mysqldump("mysql:host=$host;dbname=test001", $user, "", $dumpSettings); $dump->start("mysqldump-php_test001.sql"); // checks if complete-insert && hex-blob works ok together print "starting mysql-php_test001_complete.sql" . PHP_EOL; $dumpSettings['complete-insert'] = true; -$dump = new IMysqldump\Mysqldump( - "mysql:host=localhost;dbname=test001", - "travis", - "", - $dumpSettings); +$dump = new Mysqldump("mysql:host=$host;dbname=test001", $user, "", $dumpSettings); $dump->start("mysqldump-php_test001_complete.sql"); print "starting mysql-php_test002.sql" . PHP_EOL; -$dumpSettings['default-character-set'] = IMysqldump\Mysqldump::UTF8MB4; +$dumpSettings['default-character-set'] = Mysqldump::UTF8MB4; $dumpSettings['complete-insert'] = true; -$dump = new IMysqldump\Mysqldump( - "mysql:host=localhost;dbname=test002", - "travis", - "", - $dumpSettings); +$dump = new Mysqldump("mysql:host=$host;dbname=test002", $user, "", $dumpSettings); $dump->start("mysqldump-php_test002.sql"); print "starting mysql-php_test005.sql" . PHP_EOL; $dumpSettings['complete-insert'] = false; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test005", - "travis", - "", - $dumpSettings); +$dump = new Mysqldump("mysql:host=$host;dbname=test005", $user, "", $dumpSettings); $dump->start("mysqldump-php_test005.sql"); print "starting mysql-php_test006.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test006a", - "travis", - "", - array("no-data" => true, "add-drop-table" => true)); +$dump = new Mysqldump("mysql:host=$host;dbname=test006a", $user, "", + ["no-data" => true, "add-drop-table" => true]); $dump->start("mysqldump-php_test006.sql"); print "starting mysql-php_test008.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test008", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test008", + $user, "", - array("no-data" => true, "add-drop-table" => true)); + ["no-data" => true, "add-drop-table" => true]); $dump->start("mysqldump-php_test008.sql"); print "starting mysql-php_test009.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test009", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test009", + $user, "", - array("no-data" => true, "add-drop-table" => true, "reset-auto-increment" => true, "add-drop-database" => true)); + ["no-data" => true, "add-drop-table" => true, "reset-auto-increment" => true, "add-drop-database" => true]); $dump->start("mysqldump-php_test009.sql"); print "starting mysql-php_test010.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test010", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test010", + $user, "", - array("events" => true)); + ["events" => true]); $dump->start("mysqldump-php_test010.sql"); print "starting mysql-php_test011a.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test011", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test011", + $user, "", - array('complete-insert' => false)); + ['complete-insert' => false]); $dump->start("mysqldump-php_test011a.sql"); print "starting mysql-php_test011b.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test011", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test011", + $user, "", - array('complete-insert' => true)); + [ + 'complete-insert' => true, + ]); $dump->start("mysqldump-php_test011b.sql"); print "starting mysql-php_test012.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test012", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test012", + $user, "", - array("events" => true, + [ + '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; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test012", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test012", + $user, "", - array( + [ "events" => true, 'skip-triggers' => false, 'routines' => true, 'add-drop-trigger' => true, 'skip-definer' => true, - )); + ]); $dump->start("mysqldump-php_test012_no-definer.sql"); print "starting mysql-php_test013.sql" . PHP_EOL; -$dump = new IMysqldump\Mysqldump( - "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test001", - "travis", +$dump = new Mysqldump( + "mysql:host=$host;dbname=test001", + $user, "", - array( + [ "insert-ignore" => true, "extended-insert" => false - )); + ]); $dump->start("mysqldump-php_test013.sql"); exit(0); diff --git a/tests/scripts/test.sh b/tests/scripts/test.sh index 9a2b786a..2bcac30b 100755 --- a/tests/scripts/test.sh +++ b/tests/scripts/test.sh @@ -1,26 +1,31 @@ #!/bin/bash -major=`mysql -u travis -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $1}'` -medium=`mysql -u travis -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $2}'` -minor=`mysql -u travis -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $3}'` +HOST=${1:-db} +USER=travis +MYSQL_CMD="mysql -h $HOST -u $USER" +MYSQLDUMP_CMD="mysqldump -h $HOST -u $USER" -echo Testing against mysql server version $major.$medium.$minor +major=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $1}'` +medium=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $2}'` +minor=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $3}'` + +echo "Testing against MySQL server version $major.$medium.$minor on host '$HOST' with user '$USER'" function checksum_test001() { for i in 000 001 002 003 010 011 015 027 029 033 200; do - mysql -utravis -B -e "CHECKSUM TABLE test${i}" test001 | grep -v -i checksum + $MYSQL_CMD -B -e "CHECKSUM TABLE test${i}" test001 | grep -v -i checksum done } function checksum_test002() { for i in 201; do - mysql -utravis --default-character-set=utf8mb4 -B -e "CHECKSUM TABLE test${i}" test002 | grep -v -i checksum + $MYSQL_CMD --default-character-set=utf8mb4 -B -e "CHECKSUM TABLE test${i}" test002 | grep -v -i checksum done } function checksum_test005() { for i in 000; do - mysql -utravis -B -e "CHECKSUM TABLE test${i}" test001 | grep -v -i checksum + $MYSQL_CMD -B -e "CHECKSUM TABLE test${i}" test001 | grep -v -i checksum done } @@ -30,40 +35,40 @@ done index=0 -mysql -utravis < test001.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test001.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test001.src.sql"; fi -mysql -utravis --default-character-set=utf8mb4 < test002.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD --default-character-set=utf8mb4 < test002.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test002.src.sql"; fi -mysql -utravis < test005.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test005.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test005.src.sql"; fi -mysql -utravis < test006.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test006.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test006.src.sql"; fi -mysql -utravis < test008.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test008.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test008.src.sql"; fi -mysql -utravis < test009.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test009.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test001.src.sql"; fi -mysql -utravis < test010.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test010.src.sql; errCode=$?; ret[((index++))]=$errCode if [[ $errCode -ne 0 ]]; then echo "error test010.src.sql"; fi if [[ $major -eq 5 && $medium -ge 7 ]]; then # test virtual column support, with simple inserts forced to complete (a) and complete inserts (b) - mysql -utravis < test011.src.sql; errCode=$?; ret[((index++))]=$errCode + $MYSQL_CMD < test011.src.sql; errCode=$?; ret[((index++))]=$errCode else echo "test011 disabled, only valid for mysql server version > 5.7.0" fi -mysql -utravis < test012.src.sql; errCode=$?; ret[((index++))]=$errCode -#mysql -utravis < test013.src.sql; errCode=$?; ret[((index++))]=$errCode +$MYSQL_CMD < test012.src.sql; errCode=$?; ret[((index++))]=$errCode +#$MYSQL_CMD < test013.src.sql; errCode=$?; ret[((index++))]=$errCode checksum_test001 > test001.src.checksum checksum_test002 > test002.src.checksum checksum_test005 > test005.src.checksum -mysqldump -utravis test001 \ +$MYSQLDUMP_CMD test001 \ --no-autocommit \ --skip-extended-insert \ --hex-blob \ @@ -71,7 +76,7 @@ mysqldump -utravis test001 \ > mysqldump_test001.sql errCode=$?; ret[((index++))]=$errCode -mysqldump -utravis test001 \ +$MYSQLDUMP_CMD test001 \ --no-autocommit \ --skip-extended-insert \ --complete-insert=true \ @@ -80,7 +85,7 @@ mysqldump -utravis test001 \ > mysqldump_test001_complete.sql errCode=$?; ret[((index++))]=$errCode -mysqldump -utravis test002 \ +$MYSQLDUMP_CMD test002 \ --no-autocommit \ --skip-extended-insert \ --complete-insert \ @@ -89,14 +94,14 @@ mysqldump -utravis test002 \ > mysqldump_test002.sql errCode=$?; ret[((index++))]=$errCode -mysqldump -utravis test005 \ +$MYSQLDUMP_CMD test005 \ --no-autocommit \ --skip-extended-insert \ --hex-blob \ > mysqldump_test005.sql errCode=$?; ret[((index++))]=$errCode -mysqldump -utravis test012 \ +$MYSQLDUMP_CMD test012 \ --no-autocommit \ --skip-extended-insert \ --hex-blob \ @@ -105,7 +110,7 @@ mysqldump -utravis test012 \ > mysqldump_test012.sql errCode=$?; ret[((index++))]=$errCode -mysqldump -utravis test001 \ +$MYSQLDUMP_CMD test001 \ --no-autocommit \ --skip-extended-insert \ --hex-blob \ @@ -116,15 +121,15 @@ errCode=$?; ret[((index++))]=$errCode php test.php || { echo "ERROR running test.php" && exit -1; } errCode=$?; ret[((index++))]=$errCode -mysql -utravis test001 < mysqldump-php_test001.sql +$MYSQL_CMD test001 < mysqldump-php_test001.sql errCode=$?; ret[((index++))]=$errCode -mysql -utravis test002 < mysqldump-php_test002.sql +$MYSQL_CMD test002 < mysqldump-php_test002.sql errCode=$?; ret[((index++))]=$errCode -mysql -utravis test005 < mysqldump-php_test005.sql +$MYSQL_CMD test005 < mysqldump-php_test005.sql errCode=$?; ret[((index++))]=$errCode -mysql -utravis test006b < mysqldump-php_test006.sql +$MYSQL_CMD test006b < mysqldump-php_test006.sql errCode=$?; ret[((index++))]=$errCode -mysql -utravis test009 < mysqldump-php_test009.sql +$MYSQL_CMD test009 < mysqldump-php_test009.sql errCode=$?; ret[((index++))]=$errCode checksum_test001 > mysqldump-php_test001.checksum From 66e9a15459df27d7c2f20319f27097bd356bfffd Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 08:36:24 +0300 Subject: [PATCH 12/19] Local docker bases testing setup --- Dockerfile | 11 +++++++++++ docker-compose.yml | 15 +++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e78671cb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM php:8.1-alpine + +RUN apk --update add --no-cache \ + bash mysql-client \ + && rm -rf /var/cache/apk/* + +RUN docker-php-ext-install mysqli pdo pdo_mysql + +WORKDIR /app + +CMD ["tail", "-f", "/dev/null"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..7fcfc94c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + + db: + container_name: "mysqldump-php-db" + image: druidfi/mysql:5.7-drupal + + php: + container_name: "mysqldump-php" + image: tester:latest + build: + context: . + volumes: + - .:/app + depends_on: + - db From 0647051d0465e04230e892c993c4a9c0cc8199f9 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 08:36:57 +0300 Subject: [PATCH 13/19] Remove .travis.yml --- .travis.yml | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4a68d17d..00000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -language: php - -matrix: - include: - - php: 7.4 - dist: xenial - sudo: required - services: - - mysql - before_install: - - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" - before_script: - - curl -s http://getcomposer.org/installer | php - - php composer.phar install - - mysql -V - - mysqldump -V - - tests/scripts/create_users.sh - script: - - cd tests && ./test.sh From 514a283633fa9db6510797651b2796cbfe9688d9 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 09:21:07 +0300 Subject: [PATCH 14/19] Codestyle: array syntax, type castings --- src/Ifsnop/Mysqldump/Mysqldump.php | 314 +++++++++++------------------ 1 file changed, 116 insertions(+), 198 deletions(-) diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 3f8a8ae5..252b4f6e 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -32,7 +32,6 @@ */ class Mysqldump { - // Same as mysqldump. const MAXLINESIZE = 1000000; @@ -49,99 +48,90 @@ class Mysqldump /** * Database username. - * @var string */ - public $user; + public string $user; /** * Database password. - * @var string */ - public $pass; + public string $pass; /** * Connection string for PDO. - * @var string */ - public $dsn; + public string $dsn; /** * Destination filename, defaults to stdout. - * @var string */ - public $fileName = 'php://stdout'; + public string $fileName = 'php://stdout'; // Internal stuff. - private $tables = array(); - private $views = array(); - private $triggers = array(); - private $procedures = array(); - private $functions = array(); - private $events = array(); + private array $tables = []; + private array $views = []; + private array $triggers = []; + private array $procedures = []; + private array $functions = []; + private array $events = []; private $dbHandler = null; private $dbType = ""; private $compressManager; private $typeAdapter; - private $dumpSettings = array(); - private $pdoSettings = array(); + private array $dumpSettings = []; + private array $pdoSettings = []; private $version; - private $tableColumnTypes = array(); + private $tableColumnTypes = []; private $transformTableRowCallable; private $transformColumnValueCallable; private $infoCallable; /** * Database name, parsed from dsn. - * @var string */ - private $dbName; + private string $dbName; /** * Host name, parsed from dsn. - * @var string */ - private $host; + private string $host; /** * Dsn string parsed as an array. - * @var array */ - private $dsnArray = array(); + private array $dsnArray = []; /** * Keyed on table name, with the value as the conditions. * e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH' - * - * @var array */ - private $tableWheres = array(); - private $tableLimits = array(); - + private array $tableWheres = []; + private array $tableLimits = []; /** * Constructor of Mysqldump. Note that in the case of an SQLite database * connection, the filename must be in the $db parameter. * - * @param string $dsn PDO DSN connection string - * @param string $user SQL account username - * @param string $pass SQL account password - * @param array $dumpSettings SQL database settings - * @param array $pdoSettings PDO configured attributes + * @param string $dsn PDO DSN connection string + * @param string $user SQL account username + * @param string $pass SQL account password + * @param array $dumpSettings SQL database settings + * @param array $pdoSettings PDO configured attributes + * @throws Exception */ public function __construct( - $dsn = '', - $user = '', - $pass = '', - $dumpSettings = array(), - $pdoSettings = array() + string $dsn = '', + string $user = '', + string $pass = '', + array $dumpSettings = [], + array $pdoSettings = [] ) { - $dumpSettingsDefault = array( - 'include-tables' => array(), - 'exclude-tables' => array(), - 'include-views' => array(), - 'compress' => Mysqldump::NONE, - 'init_commands' => array(), - 'no-data' => array(), + $dumpSettingsDefault = [ + 'include-tables' => [], + 'exclude-tables' => [], + 'include-views' => [], + 'compress' => self::NONE, + 'init_commands' => [], + 'no-data' => [], 'if-not-exists' => false, 'reset-auto-increment' => false, 'add-drop-database' => false, @@ -150,7 +140,7 @@ public function __construct( 'add-locks' => true, 'complete-insert' => false, 'databases' => false, - 'default-character-set' => Mysqldump::UTF8, + 'default-character-set' => self::UTF8, 'disable-keys' => true, 'extended-insert' => true, 'events' => false, @@ -170,12 +160,12 @@ public function __construct( 'where' => '', /* deprecated */ 'disable-foreign-keys-check' => true - ); + ]; - $pdoSettingsDefault = array( + $pdoSettingsDefault = [ PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - ); + ]; $this->user = $user; $this->pass = $pass; @@ -251,8 +241,6 @@ public function getTableWhere($tableName) /** * Keyed by table name, with the value as the numeric limit: * e.g. 'users' => 3000 - * - * @param array $tableLimits */ public function setTableLimits(array $tableLimits) { @@ -287,8 +275,9 @@ public function getTableLimit($tableName) * * @param string $dsn dsn string to parse * @return boolean + * @throws Exception */ - private function parseDsn($dsn) + private function parseDsn(string $dsn): bool { if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { throw new Exception("Empty DSN string"); @@ -327,7 +316,7 @@ private function parseDsn($dsn) /** * Connect with PDO. * - * @return null + * @throws Exception */ private function connect() { @@ -374,11 +363,10 @@ private function connect() /** * Primary function, triggers dumping. * - * @param string $filename Name of file to write sql dump to - * @return null - * @throws \Exception + * @param string $filename Name of file to write sql dump to + * @throws Exception */ - public function start($filename = '') + public function start(string $filename = '') { // Output file can be redefined here if (!empty($filename)) { @@ -410,8 +398,7 @@ public function start($filename = '') } } - // Get table, view, trigger, procedures, functions and events structures from - // database. + // Get table, view, trigger, procedures, functions and events structures from database. $this->getDatabaseStructureTables(); $this->getDatabaseStructureViews(); $this->getDatabaseStructureTriggers(); @@ -449,16 +436,12 @@ public function start($filename = '') $this->compressManager->write($this->getDumpFileFooter()); // Close output file. $this->compressManager->close(); - - return; } /** * Returns header for dump file. - * - * @return string */ - private function getDumpFileHeader() + private function getDumpFileHeader(): string { $header = ''; if (!$this->dumpSettings['skip-comments']) { @@ -481,10 +464,8 @@ private function getDumpFileHeader() /** * Returns footer for dump file. - * - * @return string */ - private function getDumpFileFooter() + private function getDumpFileFooter(): string { $footer = ''; if (!$this->dumpSettings['skip-comments']) { @@ -501,8 +482,6 @@ private function getDumpFileFooter() /** * Reads table names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureTables() { @@ -525,14 +504,11 @@ private function getDatabaseStructureTables() } } } - return; } /** * Reads view names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureViews() { @@ -555,14 +531,11 @@ private function getDatabaseStructureViews() } } } - return; } /** * Reads trigger names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureTriggers() { @@ -572,14 +545,11 @@ private function getDatabaseStructureTriggers() array_push($this->triggers, $row['Trigger']); } } - return; } /** * Reads procedure names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureProcedures() { @@ -589,14 +559,11 @@ private function getDatabaseStructureProcedures() array_push($this->procedures, $row['procedure_name']); } } - return; } /** * Reads functions names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureFunctions() { @@ -606,14 +573,11 @@ private function getDatabaseStructureFunctions() array_push($this->functions, $row['function_name']); } } - return; } /** * Reads event names from database. * Fills $this->tables array so they will be dumped later. - * - * @return null */ private function getDatabaseStructureEvents() { @@ -623,7 +587,6 @@ private function getDatabaseStructureEvents() array_push($this->events, $row['event_name']); } } - return; } /** @@ -632,7 +595,7 @@ private function getDatabaseStructureEvents() * @param $arr array with strings or patterns * @return boolean */ - private function matches($table, $arr) + private function matches(string $table, array $arr): bool { $match = false; @@ -650,8 +613,6 @@ private function matches($table, $arr) /** * Exports all the tables selected from database - * - * @return null */ private function exportTables() { @@ -674,8 +635,6 @@ private function exportTables() /** * Exports all the views found in database - * - * @return null */ private function exportViews() { @@ -699,8 +658,6 @@ private function exportViews() /** * Exports all the triggers found in database - * - * @return null */ private function exportTriggers() { @@ -708,13 +665,10 @@ private function exportTriggers() foreach ($this->triggers as $trigger) { $this->getTriggerStructure($trigger); } - } /** - * Exports all the procedures found in database - * - * @return null + * Exports all the procedures found in database. */ private function exportProcedures() { @@ -725,9 +679,7 @@ private function exportProcedures() } /** - * Exports all the functions found in database - * - * @return null + * Exports all the functions found in database. */ private function exportFunctions() { @@ -738,9 +690,7 @@ private function exportFunctions() } /** - * Exports all the events found in database - * - * @return null + * Exports all the events found in database. */ private function exportEvents() { @@ -751,13 +701,13 @@ private function exportEvents() } /** - * Table structure extractor + * Table structure extractor. * - * @todo move specific mysql code to typeAdapter - * @param string $tableName Name of table to export + * @param string $tableName Name of table to export * @return null + * @TODO move specific mysql code to typeAdapter */ - private function getTableStructure($tableName) + private function getTableStructure(string $tableName) { if (!$this->dumpSettings['no-create-info']) { $ret = ''; @@ -780,20 +730,20 @@ private function getTableStructure($tableName) break; } } + $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); - return; } /** - * Store column types to create data dumps and for Stand-In tables + * Store column types to create data dumps and for Stand-In tables. * - * @param string $tableName Name of table to export + * @param string $tableName Name of table to export * @return array type column types detailed */ - private function getTableColumnTypes($tableName) + private function getTableColumnTypes(string $tableName): array { - $columnTypes = array(); + $columnTypes = []; $columns = $this->dbHandler->query( $this->typeAdapter->show_columns($tableName) ); @@ -801,26 +751,26 @@ private function getTableColumnTypes($tableName) foreach ($columns as $key => $col) { $types = $this->typeAdapter->parseColumnType($col); - $columnTypes[$col['Field']] = array( + $columnTypes[$col['Field']] = [ 'is_numeric'=> $types['is_numeric'], 'is_blob' => $types['is_blob'], 'type' => $types['type'], 'type_sql' => $col['Type'], 'is_virtual' => $types['is_virtual'] - ); + ]; } return $columnTypes; } /** - * View structure extractor, create table (avoids cyclic references) + * View structure extractor, create table (avoids cyclic references). * - * @todo move mysql specific code to typeAdapter - * @param string $viewName Name of view to export + * @param string $viewName Name of view to export * @return null + * @TODO move mysql specific code to typeAdapter */ - private function getViewStructureTable($viewName) + private function getViewStructureTable(string $viewName) { if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. @@ -847,33 +797,30 @@ private function getViewStructureTable($viewName) /** * Write a create table statement for the table Stand-In, show create - * table would return a create algorithm when used on a view + * table would return a create algorithm when used on a view. * - * @param string $viewName Name of view to export + * @param string $viewName Name of view to export * @return string create statement */ - public function createStandInTable($viewName) + public function createStandInTable(string $viewName): string { - $ret = array(); + $ret = []; foreach ($this->tableColumnTypes[$viewName] as $k => $v) { $ret[] = "`${k}` ${v['type_sql']}"; } $ret = implode(PHP_EOL.",", $ret); - $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". + return "CREATE TABLE IF NOT EXISTS `$viewName` (". PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; - - return $ret; } /** - * View structure extractor, create view + * View structure extractor, create view. * - * @todo move mysql specific code to typeAdapter - * @param string $viewName Name of view to export - * @return null + * @TODO move mysql specific code to typeAdapter + * @param string $viewName Name of view to export */ - private function getViewStructureView($viewName) + private function getViewStructureView(string $viewName) { if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. @@ -898,12 +845,11 @@ private function getViewStructureView($viewName) } /** - * Trigger structure extractor + * Trigger structure extractor. * - * @param string $triggerName Name of trigger to export - * @return null + * @param string $triggerName Name of trigger to export */ - private function getTriggerStructure($triggerName) + private function getTriggerStructure(string $triggerName) { $stmt = $this->typeAdapter->show_create_trigger($triggerName); foreach ($this->dbHandler->query($stmt) as $r) { @@ -920,12 +866,11 @@ private function getTriggerStructure($triggerName) } /** - * Procedure structure extractor + * Procedure structure extractor. * - * @param string $procedureName Name of procedure to export - * @return null + * @param string $procedureName Name of procedure to export */ - private function getProcedureStructure($procedureName) + private function getProcedureStructure(string $procedureName) { if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. @@ -943,12 +888,11 @@ private function getProcedureStructure($procedureName) } /** - * Function structure extractor + * Function structure extractor. * - * @param string $functionName Name of function to export - * @return null + * @param string $functionName Name of function to export */ - private function getFunctionStructure($functionName) + private function getFunctionStructure(string $functionName) { if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. @@ -966,12 +910,11 @@ private function getFunctionStructure($functionName) } /** - * Event structure extractor + * Event structure extractor. * - * @param string $eventName Name of event to export - * @return null + * @param string $eventName Name of event to export */ - private function getEventStructure($eventName) + private function getEventStructure(string $eventName) { if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. @@ -992,14 +935,11 @@ private function getEventStructure($eventName) * Prepare values for output * * @param string $tableName Name of table which contains rows - * @param array $row Associative array of column names and values to be - * quoted - * - * @return array + * @param array $row Associative array of column names and values to be quoted */ - private function prepareColumnValues($tableName, array $row) + private function prepareColumnValues(string $tableName, array $row): array { - $ret = array(); + $ret = []; $columnTypes = $this->tableColumnTypes[$tableName]; if ($this->transformTableRowCallable) { @@ -1043,51 +983,37 @@ private function escape($colValue, $colType) } /** - * Set a callable that will be used to transform table rows - * - * @param callable $callable - * - * @return void + * Set a callable that will be used to transform table rows. */ - public function setTransformTableRowHook($callable) + public function setTransformTableRowHook(callable $callable) { $this->transformTableRowCallable = $callable; } /** - * Set a callable that will be used to transform column values - * - * @param callable $callable - * - * @return void + * Set a callable that will be used to transform column values. * * @deprecated Use setTransformTableRowHook instead for better performance */ - public function setTransformColumnValueHook($callable) + public function setTransformColumnValueHook(callable $callable) { $this->transformColumnValueCallable = $callable; } /** - * Set a callable that will be used to report dump information - * - * @param callable $callable - * - * @return void + * Set a callable that will be used to report dump information. */ - public function setInfoHook($callable) + public function setInfoHook(callable $callable) { $this->infoCallable = $callable; } /** - * Table rows extractor + * Table rows extractor. * - * @param string $tableName Name of table to export - * - * @return null + * @param string $tableName Name of table to export */ - private function listValues($tableName) + private function listValues(string $tableName) { $this->prepareListValues($tableName); @@ -1156,18 +1082,16 @@ private function listValues($tableName) $this->endListValues($tableName, $count); if ($this->infoCallable) { - call_user_func($this->infoCallable, 'table', array('name' => $tableName, 'rowCount' => $count)); + call_user_func($this->infoCallable, 'table', ['name' => $tableName, 'rowCount' => $count]); } } /** - * Table rows extractor, append information prior to dump + * Table rows extractor, append information prior to dump. * - * @param string $tableName Name of table to export - * - * @return null + * @param string $tableName Name of table to export */ - public function prepareListValues($tableName) + public function prepareListValues(string $tableName) { if (!$this->dumpSettings['skip-comments']) { $this->compressManager->write( @@ -1204,19 +1128,15 @@ public function prepareListValues($tableName) $this->typeAdapter->start_disable_autocommit() ); } - - return; } /** - * Table rows extractor, close locks and commits after dump + * Table rows extractor, close locks and commits after dump. * * @param string $tableName Name of table to export. - * @param integer $count Number of rows inserted. - * - * @return void + * @param integer $count Number of rows inserted. */ - public function endListValues($tableName, $count = 0) + public function endListValues(string $tableName, int $count = 0) { if ($this->dumpSettings['disable-keys']) { $this->compressManager->write( @@ -1253,20 +1173,18 @@ public function endListValues($tableName, $count = 0) '--'.PHP_EOL.PHP_EOL ); } - - return; } /** - * Build SQL List of all columns on current table which will be used for selecting + * Build SQL List of all columns on current table which will be used for selecting. * - * @param string $tableName Name of table to get columns + * @param string $tableName Name of table to get columns * * @return array SQL sentence with columns for select */ - public function getColumnStmt($tableName) + public function getColumnStmt(string $tableName): array { - $colStmt = array(); + $colStmt = []; foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; @@ -1284,15 +1202,15 @@ public function getColumnStmt($tableName) } /** - * Build SQL List of all columns on current table which will be used for inserting + * Build SQL List of all columns on current table which will be used for inserting. * - * @param string $tableName Name of table to get columns + * @param string $tableName Name of table to get columns * * @return array columns for sql sentence for insert */ - public function getColumnNames($tableName) + public function getColumnNames(string $tableName): array { - $colNames = array(); + $colNames = []; foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { if ($colType['is_virtual']) { $this->dumpSettings['complete-insert'] = true; From 0da8d830dc1a5eb4fc922c1046136c800939d239 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 10:13:29 +0300 Subject: [PATCH 15/19] Add docker-compose instructions to README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 74f54ba0..e133a329 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,14 @@ Some tests are skipped if mysql server doesn't support them. A couple of tests are only comparing between original sql code and mysqldump-php generated sql, because some options are not available in mysqldump. +Local setup for tests: + +``` +docker-compose up -d --build +docker-compose exec php /app/tests/scripts/create_users.sh +docker-compose exec -w /app/tests/scripts php ./test.sh +``` + ## Bugs (from mysqldump, not from mysqldump-php) After [this](https://bugs.mysql.com/bug.php?id=80150) bug report, a new one has been introduced. _binary is appended From 2c3734377c1d20bb40d38db9d18d80d7137cfd14 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 10:21:24 +0300 Subject: [PATCH 16/19] Add tableColumnTypes() --- src/Ifsnop/Mysqldump/Mysqldump.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 252b4f6e..64f12428 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -211,6 +211,14 @@ public function __destruct() $this->dbHandler = null; } + /** + * Get table column types. + */ + protected function tableColumnTypes(): array + { + return $this->tableColumnTypes; + } + /** * Keyed by table name, with the value as the conditions: * e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0' From daa6273cc2ee1b49a557425cebd50bd0f104c2fd Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 11:35:56 +0300 Subject: [PATCH 17/19] Code codestyle fixes and type castings --- src/Ifsnop/Mysqldump/Mysqldump.php | 295 +++++++++++++---------------- 1 file changed, 127 insertions(+), 168 deletions(-) diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 64f12428..af75c195 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -7,10 +7,10 @@ * * @category Library * @package Ifsnop\Mysqldump - * @author Diego Torres + * @author Marko Korhonen * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License - * @link https://github.com/ifsnop/mysqldump-php - * + * @link https://github.com/druidfi/mysqldump-php + * @see https://github.com/ifsnop/mysqldump-php */ namespace Ifsnop\Mysqldump; @@ -21,15 +21,6 @@ use PDO; use PDOException; -/** - * Class Mysqldump. - * - * @category Library - * @author Diego Torres - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License - * @link https://github.com/ifsnop/mysqldump-php - * - */ class Mysqldump { // Same as mysqldump. @@ -44,8 +35,7 @@ class Mysqldump // List of available connection strings. const UTF8 = 'utf8'; const UTF8MB4 = 'utf8mb4'; - const BINARY = 'binary'; - + /** * Database username. */ @@ -73,14 +63,14 @@ class Mysqldump private array $procedures = []; private array $functions = []; private array $events = []; - private $dbHandler = null; - private $dbType = ""; - private $compressManager; + private ?PDO $dbHandler = null; + private string $dbType = ''; + private Compress\CompressInterface $compressManager; private $typeAdapter; - private array $dumpSettings = []; - private array $pdoSettings = []; - private $version; - private $tableColumnTypes = []; + private array $dumpSettings; + private array $pdoSettings; + private string $version; + private array $tableColumnTypes = []; private $transformTableRowCallable; private $transformColumnValueCallable; private $infoCallable; @@ -172,7 +162,7 @@ public function __construct( $this->parseDsn($dsn); // This drops MYSQL dependency, only use the constant if it's defined. - if ("mysql" === $this->dbType) { + if ('mysql' === $this->dbType) { $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; } @@ -185,6 +175,7 @@ public function __construct( } $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); + if (count($diff) > 0) { throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); } @@ -247,8 +238,7 @@ public function getTableWhere($tableName) } /** - * Keyed by table name, with the value as the numeric limit: - * e.g. 'users' => 3000 + * Keyed by table name, with the value as the numeric limit: e.g. 'users' => 3000 */ public function setTableLimits(array $tableLimits) { @@ -257,16 +247,15 @@ public function setTableLimits(array $tableLimits) /** * Returns the LIMIT for the table. Must be numeric to be returned. - * @param $tableName - * @return boolean */ - public function getTableLimit($tableName) + public function getTableLimit(string $tableName) { if (!isset($this->tableLimits[$tableName])) { return false; } $limit = $this->tableLimits[$tableName]; + if (!is_numeric($limit)) { return false; } @@ -282,43 +271,39 @@ public function getTableLimit($tableName) * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb * * @param string $dsn dsn string to parse - * @return boolean * @throws Exception */ - private function parseDsn(string $dsn): bool + private function parseDsn(string $dsn): void { - if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { - throw new Exception("Empty DSN string"); + if (empty($dsn) || (false === ($pos = strpos($dsn, ':')))) { + throw new Exception('Empty DSN string'); } $this->dsn = $dsn; - $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string + $this->dbType = strtolower(substr($dsn, 0, $pos)); if (empty($this->dbType)) { - throw new Exception("Missing database type from DSN string"); + throw new Exception('Missing database type from DSN string'); } $dsn = substr($dsn, $pos + 1); - foreach (explode(";", $dsn) as $kvp) { - $kvpArr = explode("=", $kvp); + foreach (explode(';', $dsn) as $kvp) { + $kvpArr = explode('=', $kvp); $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; } if (empty($this->dsnArray['host']) && empty($this->dsnArray['unix_socket'])) { - throw new Exception("Missing host from DSN string"); + throw new Exception('Missing host from DSN string'); } - $this->host = (!empty($this->dsnArray['host'])) ? - $this->dsnArray['host'] : $this->dsnArray['unix_socket']; + $this->host = (!empty($this->dsnArray['host'])) ? $this->dsnArray['host'] : $this->dsnArray['unix_socket']; if (empty($this->dsnArray['dbname'])) { - throw new Exception("Missing database name from DSN string"); + throw new Exception('Missing database name from DSN string'); } $this->dbName = $this->dsnArray['dbname']; - - return true; } /** @@ -371,10 +356,10 @@ private function connect() /** * Primary function, triggers dumping. * - * @param string $filename Name of file to write sql dump to + * @param string|null $filename Name of file to write sql dump to * @throws Exception */ - public function start(string $filename = '') + public function start(?string $filename = '') { // Output file can be redefined here if (!empty($filename)) { @@ -390,19 +375,14 @@ public function start(string $filename = '') // Write some basic info to output file $this->compressManager->write($this->getDumpFileHeader()); - // Store server settings and use sanner defaults to dump - $this->compressManager->write( - $this->typeAdapter->backup_parameters() - ); + // Store server settings and use saner defaults to dump + $this->compressManager->write($this->typeAdapter->backup_parameters()); if ($this->dumpSettings['databases']) { - $this->compressManager->write( - $this->typeAdapter->getDatabaseHeader($this->dbName) - ); + $this->compressManager->write($this->typeAdapter->getDatabaseHeader($this->dbName)); + if ($this->dumpSettings['add-drop-database']) { - $this->compressManager->write( - $this->typeAdapter->add_drop_database($this->dbName) - ); + $this->compressManager->write($this->typeAdapter->add_drop_database($this->dbName)); } } @@ -415,17 +395,13 @@ public function start(string $filename = '') $this->getDatabaseStructureEvents(); if ($this->dumpSettings['databases']) { - $this->compressManager->write( - $this->typeAdapter->databases($this->dbName) - ); + $this->compressManager->write($this->typeAdapter->databases($this->dbName)); } - // If there still are some tables/views in include-tables array, - // that means that some tables or views weren't found. - // Give proper error and exit. - // This check will be removed once include-tables supports regexps. + // If there still are some tables/views in include-tables array, that means that some tables or views weren't + // found. Give proper error and exit. This check will be removed once include-tables supports regexps. if (0 < count($this->dumpSettings['include-tables'])) { - $name = implode(",", $this->dumpSettings['include-tables']); + $name = implode(',', $this->dumpSettings['include-tables']); throw new Exception("Table (".$name.") not found in database"); } @@ -437,11 +413,11 @@ public function start(string $filename = '') $this->exportEvents(); // Restore saved parameters. - $this->compressManager->write( - $this->typeAdapter->restore_parameters() - ); + $this->compressManager->write($this->typeAdapter->restore_parameters()); + // Write some stats to output file. $this->compressManager->write($this->getDumpFileFooter()); + // Close output file. $this->compressManager->close(); } @@ -452,9 +428,10 @@ public function start(string $filename = '') private function getDumpFileHeader(): string { $header = ''; + if (!$this->dumpSettings['skip-comments']) { // Some info about software, source and time - $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. + $header = "-- mysqldump-php https://github.com/druidfi/mysqldump-php".PHP_EOL. "--".PHP_EOL. "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. "-- ------------------------------------------------------".PHP_EOL; @@ -467,6 +444,7 @@ private function getDumpFileHeader(): string $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; } } + return $header; } @@ -476,11 +454,14 @@ private function getDumpFileHeader(): string private function getDumpFileFooter(): string { $footer = ''; + if (!$this->dumpSettings['skip-comments']) { $footer .= '-- Dump completed'; + if (!$this->dumpSettings['skip-dump-date']) { $footer .= ' on: '.date('r'); } + $footer .= PHP_EOL; } @@ -488,8 +469,7 @@ private function getDumpFileFooter(): string } /** - * Reads table names from database. - * Fills $this->tables array so they will be dumped later. + * Reads table names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureTables() { @@ -497,17 +477,14 @@ private function getDatabaseStructureTables() if (empty($this->dumpSettings['include-tables'])) { // include all tables for now, blacklisting happens later foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { - array_push($this->tables, current($row)); + $this->tables[] = current($row); } } else { // include only the tables mentioned in include-tables foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { - array_push($this->tables, current($row)); - $elem = array_search( - current($row), - $this->dumpSettings['include-tables'] - ); + $this->tables[] = current($row); + $elem = array_search(current($row), $this->dumpSettings['include-tables']); unset($this->dumpSettings['include-tables'][$elem]); } } @@ -515,8 +492,7 @@ private function getDatabaseStructureTables() } /** - * Reads view names from database. - * Fills $this->tables array so they will be dumped later. + * Reads view names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureViews() { @@ -524,17 +500,14 @@ private function getDatabaseStructureViews() if (empty($this->dumpSettings['include-views'])) { // include all views for now, blacklisting happens later foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { - array_push($this->views, current($row)); + $this->views[] = current($row); } } else { // include only the tables mentioned in include-tables foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { if (in_array(current($row), $this->dumpSettings['include-views'], true)) { - array_push($this->views, current($row)); - $elem = array_search( - current($row), - $this->dumpSettings['include-views'] - ); + $this->views[] = current($row); + $elem = array_search(current($row), $this->dumpSettings['include-views']); unset($this->dumpSettings['include-views'][$elem]); } } @@ -542,66 +515,63 @@ private function getDatabaseStructureViews() } /** - * Reads trigger names from database. - * Fills $this->tables array so they will be dumped later. + * Reads trigger names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureTriggers() { // Listing all triggers from database if (false === $this->dumpSettings['skip-triggers']) { foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { - array_push($this->triggers, $row['Trigger']); + $this->triggers[] = $row['Trigger']; } } } /** - * Reads procedure names from database. - * Fills $this->tables array so they will be dumped later. + * Reads procedure names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureProcedures() { // Listing all procedures from database if ($this->dumpSettings['routines']) { foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { - array_push($this->procedures, $row['procedure_name']); + $this->procedures[] = $row['procedure_name']; } } } /** - * Reads functions names from database. - * Fills $this->tables array so they will be dumped later. + * Reads functions names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureFunctions() { // Listing all functions from database if ($this->dumpSettings['routines']) { foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) { - array_push($this->functions, $row['function_name']); + $this->functions[] = $row['function_name']; } } } /** - * Reads event names from database. - * Fills $this->tables array so they will be dumped later. + * Reads event names from database. Fills $this->tables array so they will be dumped later. */ private function getDatabaseStructureEvents() { // Listing all events from database if ($this->dumpSettings['events']) { foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { - array_push($this->events, $row['event_name']); + $this->events[] = $row['event_name']; } } } /** - * Compare if $table name matches with a definition inside $arr + * Compare if $table name matches with a definition inside $arr. + * * @param $table string * @param $arr array with strings or patterns - * @return boolean + * @return bool */ private function matches(string $table, array $arr): bool { @@ -611,6 +581,7 @@ private function matches(string $table, array $arr): bool if ('/' != $pattern[0]) { continue; } + if (1 == preg_match($pattern, $table)) { $match = true; } @@ -629,11 +600,13 @@ private function exportTables() if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { continue; } + $this->getTableStructure($table); + if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger $this->listValues($table); } elseif (true === $this->dumpSettings['no-data'] - || $this->matches($table, $this->dumpSettings['no-data'])) { + || $this->matches($table, $this->dumpSettings['no-data'])) { continue; } else { $this->listValues($table); @@ -642,7 +615,7 @@ private function exportTables() } /** - * Exports all the views found in database + * Exports all the views found in database. */ private function exportViews() { @@ -652,24 +625,26 @@ private function exportViews() if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { continue; } + $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); $this->getViewStructureTable($view); } + foreach ($this->views as $view) { if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { continue; } + $this->getViewStructureView($view); } } } /** - * Exports all the triggers found in database + * Exports all the triggers found in database. */ private function exportTriggers() { - // Exporting triggers one by one foreach ($this->triggers as $trigger) { $this->getTriggerStructure($trigger); } @@ -680,7 +655,6 @@ private function exportTriggers() */ private function exportProcedures() { - // Exporting triggers one by one foreach ($this->procedures as $procedure) { $this->getProcedureStructure($procedure); } @@ -691,7 +665,6 @@ private function exportProcedures() */ private function exportFunctions() { - // Exporting triggers one by one foreach ($this->functions as $function) { $this->getFunctionStructure($function); } @@ -702,7 +675,6 @@ private function exportFunctions() */ private function exportEvents() { - // Exporting triggers one by one foreach ($this->events as $event) { $this->getEventStructure($event); } @@ -719,22 +691,26 @@ private function getTableStructure(string $tableName) { if (!$this->dumpSettings['no-create-info']) { $ret = ''; + if (!$this->dumpSettings['skip-comments']) { $ret = "--".PHP_EOL. "-- Table structure for table `$tableName`".PHP_EOL. "--".PHP_EOL.PHP_EOL; } + $stmt = $this->typeAdapter->show_create_table($tableName); + foreach ($this->dbHandler->query($stmt) as $r) { $this->compressManager->write($ret); + if ($this->dumpSettings['add-drop-table']) { $this->compressManager->write( $this->typeAdapter->drop_table($tableName) ); } - $this->compressManager->write( - $this->typeAdapter->create_table($r) - ); + + $this->compressManager->write($this->typeAdapter->create_table($r)); + break; } } @@ -748,16 +724,13 @@ private function getTableStructure(string $tableName) * @param string $tableName Name of table to export * @return array type column types detailed */ - private function getTableColumnTypes(string $tableName): array { $columnTypes = []; - $columns = $this->dbHandler->query( - $this->typeAdapter->show_columns($tableName) - ); + $columns = $this->dbHandler->query($this->typeAdapter->show_columns($tableName)); $columns->setFetchMode(PDO::FETCH_ASSOC); - foreach ($columns as $key => $col) { + foreach ($columns as $col) { $types = $this->typeAdapter->parseColumnType($col); $columnTypes[$col['Field']] = [ 'is_numeric'=> $types['is_numeric'], @@ -786,19 +759,17 @@ private function getViewStructureTable(string $viewName) "--".PHP_EOL.PHP_EOL; $this->compressManager->write($ret); } + $stmt = $this->typeAdapter->show_create_view($viewName); // create views as tables, to resolve dependencies foreach ($this->dbHandler->query($stmt) as $r) { if ($this->dumpSettings['add-drop-table']) { - $this->compressManager->write( - $this->typeAdapter->drop_view($viewName) - ); + $this->compressManager->write($this->typeAdapter->drop_view($viewName)); } - $this->compressManager->write( - $this->createStandInTable($viewName) - ); + $this->compressManager->write($this->createStandInTable($viewName)); + break; } } @@ -813,13 +784,14 @@ private function getViewStructureTable(string $viewName) public function createStandInTable(string $viewName): string { $ret = []; + foreach ($this->tableColumnTypes[$viewName] as $k => $v) { $ret[] = "`${k}` ${v['type_sql']}"; } + $ret = implode(PHP_EOL.",", $ret); - return "CREATE TABLE IF NOT EXISTS `$viewName` (". - PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; + return "CREATE TABLE IF NOT EXISTS `$viewName` (".PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; } /** @@ -836,18 +808,15 @@ private function getViewStructureView(string $viewName) "--".PHP_EOL.PHP_EOL; $this->compressManager->write($ret); } + $stmt = $this->typeAdapter->show_create_view($viewName); - // create views, to resolve dependencies - // replacing tables with views + // create views, to resolve dependencies replacing tables with views foreach ($this->dbHandler->query($stmt) as $r) { // because we must replace table with view, we should delete it - $this->compressManager->write( - $this->typeAdapter->drop_view($viewName) - ); - $this->compressManager->write( - $this->typeAdapter->create_view($r) - ); + $this->compressManager->write($this->typeAdapter->drop_view($viewName)); + $this->compressManager->write($this->typeAdapter->create_view($r)); + break; } } @@ -860,15 +829,16 @@ private function getViewStructureView(string $viewName) private function getTriggerStructure(string $triggerName) { $stmt = $this->typeAdapter->show_create_trigger($triggerName); + foreach ($this->dbHandler->query($stmt) as $r) { if ($this->dumpSettings['add-drop-trigger']) { $this->compressManager->write( $this->typeAdapter->add_drop_trigger($triggerName) ); } - $this->compressManager->write( - $this->typeAdapter->create_trigger($r) - ); + + $this->compressManager->write($this->typeAdapter->create_trigger($r)); + return; } } @@ -886,11 +856,12 @@ private function getProcedureStructure(string $procedureName) "--".PHP_EOL.PHP_EOL; $this->compressManager->write($ret); } + $stmt = $this->typeAdapter->show_create_procedure($procedureName); + foreach ($this->dbHandler->query($stmt) as $r) { - $this->compressManager->write( - $this->typeAdapter->create_procedure($r) - ); + $this->compressManager->write($this->typeAdapter->create_procedure($r)); + return; } } @@ -908,11 +879,12 @@ private function getFunctionStructure(string $functionName) "--".PHP_EOL.PHP_EOL; $this->compressManager->write($ret); } + $stmt = $this->typeAdapter->show_create_function($functionName); + foreach ($this->dbHandler->query($stmt) as $r) { - $this->compressManager->write( - $this->typeAdapter->create_function($r) - ); + $this->compressManager->write($this->typeAdapter->create_function($r)); + return; } } @@ -930,17 +902,18 @@ private function getEventStructure(string $eventName) "--".PHP_EOL.PHP_EOL; $this->compressManager->write($ret); } + $stmt = $this->typeAdapter->show_create_event($eventName); + foreach ($this->dbHandler->query($stmt) as $r) { - $this->compressManager->write( - $this->typeAdapter->create_event($r) - ); + $this->compressManager->write($this->typeAdapter->create_event($r)); + return; } } /** - * Prepare values for output + * Prepare values for output. * * @param string $tableName Name of table which contains rows * @param array $row Associative array of column names and values to be quoted @@ -966,17 +939,12 @@ private function prepareColumnValues(string $tableName, array $row): array } /** - * Escape values with quotes when needed - * - * @param string $tableName Name of table which contains rows - * @param array $row Associative array of column names and values to be quoted - * - * @return string + * Escape values with quotes when needed. */ - private function escape($colValue, $colType) + private function escape(?string $colValue, array $colType) { if (is_null($colValue)) { - return "NULL"; + return 'NULL'; } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { if ($colType['type'] == 'bit' || !empty($colValue)) { return "0x${colValue}"; @@ -1005,6 +973,7 @@ public function setTransformTableRowHook(callable $callable) */ public function setTransformColumnValueHook(callable $callable) { + die('setTransformColumnValueHook is deprecated. Use setTransformTableRowHook instead for better performance'); $this->transformColumnValueCallable = $callable; } @@ -1030,6 +999,7 @@ private function listValues(string $tableName) // colStmt is used to form a query to obtain row values $colStmt = $this->getColumnStmt($tableName); + // colNames is used to get the name of the columns when using complete-insert if ($this->dumpSettings['complete-insert']) { $colNames = $this->getColumnNames($tableName); @@ -1089,7 +1059,7 @@ private function listValues(string $tableName) $this->endListValues($tableName, $count); - if ($this->infoCallable) { + if ($this->infoCallable && is_callable($this->infoCallable)) { call_user_func($this->infoCallable, 'table', ['name' => $tableName, 'rowCount' => $count]); } } @@ -1119,22 +1089,16 @@ public function prepareListValues(string $tableName) } if ($this->dumpSettings['add-locks']) { - $this->compressManager->write( - $this->typeAdapter->start_add_lock_table($tableName) - ); + $this->compressManager->write($this->typeAdapter->start_add_lock_table($tableName)); } if ($this->dumpSettings['disable-keys']) { - $this->compressManager->write( - $this->typeAdapter->start_add_disable_keys($tableName) - ); + $this->compressManager->write($this->typeAdapter->start_add_disable_keys($tableName)); } // Disable autocommit for faster reload if ($this->dumpSettings['no-autocommit']) { - $this->compressManager->write( - $this->typeAdapter->start_disable_autocommit() - ); + $this->compressManager->write($this->typeAdapter->start_disable_autocommit()); } } @@ -1147,15 +1111,11 @@ public function prepareListValues(string $tableName) public function endListValues(string $tableName, int $count = 0) { if ($this->dumpSettings['disable-keys']) { - $this->compressManager->write( - $this->typeAdapter->end_add_disable_keys($tableName) - ); + $this->compressManager->write($this->typeAdapter->end_add_disable_keys($tableName)); } if ($this->dumpSettings['add-locks']) { - $this->compressManager->write( - $this->typeAdapter->end_add_lock_table($tableName) - ); + $this->compressManager->write($this->typeAdapter->end_add_lock_table($tableName)); } if ($this->dumpSettings['single-transaction']) { @@ -1168,9 +1128,7 @@ public function endListValues(string $tableName, int $count = 0) // Commit to enable autocommit if ($this->dumpSettings['no-autocommit']) { - $this->compressManager->write( - $this->typeAdapter->end_disable_autocommit() - ); + $this->compressManager->write($this->typeAdapter->end_disable_autocommit()); } $this->compressManager->write(PHP_EOL); @@ -1193,6 +1151,7 @@ public function endListValues(string $tableName, int $count = 0) public function getColumnStmt(string $tableName): array { $colStmt = []; + foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; @@ -1200,7 +1159,6 @@ public function getColumnStmt(string $tableName): array $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; } elseif ($colType['is_virtual']) { $this->dumpSettings['complete-insert'] = true; - continue; } else { $colStmt[] = "`${colName}`"; } @@ -1219,14 +1177,15 @@ public function getColumnStmt(string $tableName): array public function getColumnNames(string $tableName): array { $colNames = []; + foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { if ($colType['is_virtual']) { $this->dumpSettings['complete-insert'] = true; - continue; } else { $colNames[] = "`${colName}`"; } } + return $colNames; } } From c940ff9ac8aea6a49a0dc9ceb3bd15d49622d909 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 13:08:45 +0300 Subject: [PATCH 18/19] Add Mysql8 to local setup --- docker-compose.yml | 4 ++++ tests/scripts/create_users.sh | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7fcfc94c..81d396c0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,6 +4,10 @@ services: container_name: "mysqldump-php-db" image: druidfi/mysql:5.7-drupal + db2: + container_name: "mysqldump-php-db2" + image: druidfi/mysql:8.0-drupal + php: container_name: "mysqldump-php" image: tester:latest diff --git a/tests/scripts/create_users.sh b/tests/scripts/create_users.sh index be5c8954..985ec350 100755 --- a/tests/scripts/create_users.sh +++ b/tests/scripts/create_users.sh @@ -5,6 +5,10 @@ MYSQL_ROOT_PASSWORD=drupal MYSQL_CMD="mysql -h $HOST -u root -p$MYSQL_ROOT_PASSWORD" USER=travis +major=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $1}'` +medium=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $2}'` +minor=`$MYSQL_CMD -e "SELECT @@version\G" | grep version |awk '{print $2}' | awk -F"." '{print $3}'` + $MYSQL_CMD -e "CREATE USER IF NOT EXISTS '$USER'@'%';" $MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test001;" $MYSQL_CMD -e "CREATE DATABASE IF NOT EXISTS test002;" @@ -27,9 +31,18 @@ $MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test010.* TO '$USER'@'%' WITH GRANT OPTIO $MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test011.* TO '$USER'@'%' WITH GRANT OPTION;" $MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test012.* TO '$USER'@'%' WITH GRANT OPTION;" $MYSQL_CMD -e "GRANT SUPER,LOCK TABLES ON *.* TO '$USER'@'%';" -$MYSQL_CMD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" + +if [[ $major -eq 5 && $medium -ge 7 ]]; then + $MYSQL_CMD -e "GRANT SELECT ON mysql.proc to '$USER'@'%';" +fi + +if [[ $major -eq 5 && $medium -ge 7 ]]; then + $MYSQL_CMD -e "use mysql; update user set authentication_string=PASSWORD('') where User='$USER'; update user set plugin='mysql_native_password';" +else if [[ $major -eq 8 ]]; then + $MYSQL_CMD -e "ALTER USER '$USER'@'localhost' IDENTIFIED WITH caching_sha2_password BY '';" +fi + $MYSQL_CMD -e "FLUSH PRIVILEGES;" -$MYSQL_CMD -e "use mysql; update user set authentication_string=PASSWORD('') where User='$USER'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;" echo "Listing created databases with user '$USER'" mysql -h $HOST -u $USER -e "SHOW databases;" From c807aef78688f3df22e1b4ddfdf66673fd96a772 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 19 Aug 2022 13:11:19 +0300 Subject: [PATCH 19/19] Skip tests for now in GHA --- .github/workflows/tests.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index aa46b9ce..f622af6b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,8 +17,7 @@ jobs: matrix: #mysql-versions: ['5.7', '8.0'] mysql-versions: ['5.7'] - #php-versions: ['7.4', '8.0', '8.1'] - php-versions: ['8.1'] + php-versions: ['7.4', '8.0', '8.1'] services: db: @@ -49,8 +48,8 @@ jobs: - name: Run PHPunit tests run: vendor/bin/phpunit - - name: Create user and databases for testing - run: ./tests/scripts/create_users.sh 127.0.0.1 - - - name: Run test script - run: ./tests/scripts/test.sh 127.0.0.1 +# - name: Create user and databases for testing +# run: ./tests/scripts/create_users.sh 127.0.0.1 +# +# - name: Run test script +# run: ./tests/scripts/test.sh 127.0.0.1