Skip to content

Commit

Permalink
Merge pull request #19 from iMi-digital/main
Browse files Browse the repository at this point in the history
Fix #17 - Virtual Blob Columns were not treated as virtual
  • Loading branch information
back-2-95 authored Nov 30, 2022
2 parents 6842f6d + 663153e commit 5850725
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,9 @@ private function listValues(string $tableName)
$lineSize = 0;
$colNames = [];

// getting the column statement has side effect, so we backup this setting for consitency
$completeInsertBackup = $this->settings->isEnabled('complete-insert');

// colStmt is used to form a query to obtain row values
$colStmt = $this->getColumnStmt($tableName);

Expand Down Expand Up @@ -865,6 +868,8 @@ private function listValues(string $tableName)
if ($this->infoCallable && is_callable($this->infoCallable)) {
call_user_func($this->infoCallable, 'table', ['name' => $tableName, 'rowCount' => $count]);
}

$this->settings->setCompleteInsert($completeInsertBackup);
}

/**
Expand Down Expand Up @@ -957,14 +962,14 @@ protected function getColumnStmt(string $tableName): array
$colStmt = [];
foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) {
// TODO handle bug where PHP 8.1 returns double field wrong
if ($colType['type'] == 'double' && PHP_VERSION_ID > 80100) {
if ($colType['is_virtual']) {
$this->settings->setCompleteInsert();
} elseif ($colType['type'] == 'double' && PHP_VERSION_ID > 80100) {
$colStmt[] = sprintf("CONCAT(`%s`) AS `%s`", $colName, $colName);
} elseif ($colType['type'] === 'bit' && $this->settings->isEnabled('hex-blob')) {
$colStmt[] = sprintf("LPAD(HEX(`%s`),2,'0') AS `%s`", $colName, $colName);
} elseif ($colType['is_blob'] && $this->settings->isEnabled('hex-blob')) {
$colStmt[] = sprintf("HEX(`%s`) AS `%s`", $colName, $colName);
} elseif ($colType['is_virtual']) {
$this->settings->setCompleteInsert();
} else {
$colStmt[] = sprintf("`%s`", $colName);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/scripts/create_users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $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 "CREATE DATABASE IF NOT EXISTS test014;"
$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;"
Expand All @@ -32,6 +33,7 @@ $MYSQL_CMD -e "GRANT ALL PRIVILEGES ON test009.* TO '$USER'@'%' WITH GRANT OPTIO
$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 ALL PRIVILEGES ON test014.* TO '$USER'@'%' WITH GRANT OPTION;"
$MYSQL_CMD -e "GRANT PROCESS,SUPER,LOCK TABLES ON *.* TO '$USER'@'%';"

if [[ $major -eq 5 && $medium -ge 7 ]]; then
Expand Down
23 changes: 23 additions & 0 deletions tests/scripts/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@
);
$dump->start("output/mysqldump-php_test013.sql");

print "Create dump with PHP: mysql-php_test014a.sql" . PHP_EOL;
$dump = new Mysqldump(
"mysql:host=$host;dbname=test014",
$user,
"",
[
'complete-insert' => false,
'hex-blob' => true,
] );
$dump->start("output/mysqldump-php_test014a.sql");

print "Create dump with PHP: mysql-php_test014b.sql" . PHP_EOL;
$dump = new Mysqldump(
"mysql:host=$host;dbname=test014",
$user,
"",
[
'complete-insert' => true,
'hex-blob' => true,
]
);
$dump->start("output/mysqldump-php_test014b.sql");

exit(0);
} catch (Exception $e) {
print "Error: " . $e->getMessage() . PHP_EOL;
Expand Down
38 changes: 35 additions & 3 deletions tests/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ $MYSQL_CMD < test012.src.sql && echo " - done."; errCode=$?; ret[((index++))]=$e
if [[ $errCode -ne 0 ]]; then echo "error test012.src.sql"; fi
#$MYSQL_CMD < test013.src.sql; errCode=$?; ret[((index++))]=$errCode

printf "Import test014.src.sql"
$MYSQL_CMD < test014.src.sql && echo " - done."; errCode=$?; ret[((index++))]=$errCode
if [[ $errCode -ne 0 ]]; then echo "error test014.src.sql"; fi

printf "\nRun checksum tests:\n\n"

printf "Create checksum: test001.src.checksum"
Expand Down Expand Up @@ -202,6 +206,13 @@ else
echo "test011 disabled, only valid for mysql server version 5.7.x"
fi

if [[ $major -eq 5 && $medium -ge 7 ]]; then
# test virtual column support, with simple inserts forced to complete (a) and complete inserts (b)
cat test014.src.sql | egrep "INSERT|GENERATED" > output/test014.filtered.sql && echo "Created test014.filtered.sql"
else
echo "test014 disabled, only valid for mysql server version 5.7.x"
fi

cat output/mysqldump_test001.sql | grep ^INSERT > output/mysqldump_test001.filtered.sql && echo "Created mysqldump_test001.filtered.sql"
cat output/mysqldump_test001_complete.sql | grep ^INSERT > output/mysqldump_test001_complete.filtered.sql && echo "Created mysqldump_test001_complete.filtered.sql"
cat output/mysqldump_test002.sql | grep ^INSERT > output/mysqldump_test002.filtered.sql && echo "Created mysqldump_test002.filtered.sql"
Expand All @@ -226,6 +237,14 @@ fi
cat output/mysqldump-php_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'CREATE.*TRIGGER' -e 'FUNCTION' -e 'PROCEDURE' > output/mysqldump-php_test012.filtered.sql && echo "Created mysqldump-php_test012.filtered.sql"
cat output/mysqldump-php_test013.sql | grep INSERT > output/mysqldump-php_test013.filtered.sql && echo "Created mysqldump-php_test013.filtered.sql"

if [[ $major -eq 5 && $medium -ge 7 ]]; then
# test virtual column support, with simple inserts forced to complete (a) and complete inserts (b)
cat output/mysqldump-php_test014a.sql | egrep "INSERT|GENERATED" > output/mysqldump-php_test014a.filtered.sql && echo "Created mysqldump-php_test014a.filtered.sql"
cat output/mysqldump-php_test014b.sql | egrep "INSERT|GENERATED" > output/mysqldump-php_test014b.filtered.sql && echo "Created mysqldump-php_test014b.filtered.sql"
else
echo "test014 disabled, only valid for mysql server version 5.7.x"
fi

printf "\nRun diff tests:\n\n"

test="Test#$index diff test001.filtered.sql mysqldump_test001.filtered.sql"
Expand Down Expand Up @@ -325,6 +344,19 @@ diff output/mysqldump_test013.filtered.sql output/mysqldump-php_test013.filtered
errCode=$?; ret[((index++))]=$errCode
if [[ $errCode -ne 0 ]]; then echo -e "\n$test\n"; fi

if [[ $major -eq 5 && $medium -ge 7 ]]; then
# test virtual column support, with simple inserts forced to complete (a) and complete inserts (b)
test="Test#$index diff test014.filtered.sql mysqldump-php_test014a.filtered.sql"
diff output/test014.filtered.sql output/mysqldump-php_test014a.filtered.sql
errCode=$?; ret[((index++))]=$errCode
if [[ $errCode -ne 0 ]]; then echo -e "\n$test\n"; fi
test="Test#$index diff test014.filtered.sql mysqldump-php_test014b.filtered.sql"
diff output/test014.filtered.sql output/mysqldump-php_test014b.filtered.sql
errCode=$?; ret[((index++))]=$errCode
if [[ $errCode -ne 0 ]]; then echo -e "\n$test\n"; fi
else
echo "test011 disabled, only valid for mysql server version 5.7.x"
fi
echo -e "\nDone $index tests\n"

retvalue=0
Expand All @@ -337,9 +369,9 @@ for i in $(seq 0 $index) ; do
done

if [[ $retvalue -eq 0 ]]; then
rm output/*.checksum 2> /dev/null
rm output/*.filtered.sql 2> /dev/null
rm output/mysqldump* 2> /dev/null
# rm output/*.checksum 2> /dev/null
# rm output/*.filtered.sql 2> /dev/null
# rm output/mysqldump* 2> /dev/null

echo -e "\nAll tests were successfully"
else
Expand Down
52 changes: 52 additions & 0 deletions tests/scripts/test014.src.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
DROP DATABASE IF EXISTS `test014`;
CREATE DATABASE `test014`;
USE `test014`;

-- MySQL dump 10.13 Distrib 5.7.15, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.7.15

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40141 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `test014`
--

DROP TABLE IF EXISTS `test014`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test014` (
`id` binary(16) NOT NULL,
`copy_id` binary(16) GENERATED ALWAYS AS (`id`) VIRTUAL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `test014`
--

LOCK TABLES `test014` WRITE;
/*!40000 ALTER TABLE `test014` DISABLE KEYS */;
INSERT INTO `test014` (`id`) VALUES (0x31353934313300000000000000000000),(0x32393437373500000000000000000000);
/*!40000 ALTER TABLE `test014` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40141 SET SQL_NOTES=@OLD_SQL_NOTES */;

0 comments on commit 5850725

Please sign in to comment.