Skip to content

Commit 8934dbe

Browse files
committed
merge virtual columns support. Closes #107
2 parents 96864d6 + 794a4b4 commit 8934dbe

File tree

7 files changed

+158
-50
lines changed

7 files changed

+158
-50
lines changed

.travis.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
sudo: false
1+
dist: trusty
2+
sudo: required
23

34
language: php
45

@@ -8,7 +9,7 @@ php:
89
- 5.6
910
- 5.5
1011
- 5.4
11-
- 5.3
12+
# - 5.3
1213
- hhvm
1314
- nightly
1415

@@ -17,8 +18,23 @@ services:
1718

1819
before_script:
1920
- curl -s http://getcomposer.org/installer | php
20-
- php composer.phar install --dev
21+
- php composer.phar install
22+
- sudo service mysql stop || echo "mysql not stopped"
23+
- echo mysql-apt-config mysql-apt-config/select-server select mysql-5.7 | sudo debconf-set-selections
24+
- wget http://dev.mysql.com/get/mysql-apt-config_0.7.3-1_all.deb
25+
- sudo dpkg --install mysql-apt-config_0.7.3-1_all.deb
26+
- sudo apt-get update -q
27+
- sudo apt-get install -q -y --force-yes -o Dpkg::Options::=--force-confnew mysql-server
28+
- sudo mysql_upgrade
29+
- sudo service mysql stop || echo "mysql not stopped"
30+
- sudo mysqld_safe --skip-grant-tables &
31+
- sleep 4
32+
- sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"
33+
- sudo mysqladmin shutdown
34+
- sleep 1
35+
- sudo service mysql start
2136
- mysql -V
37+
- tests/create_users.sh
2238

2339
script:
2440
- php src/Ifsnop/Mysqldump/Mysqldump.php

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ MySQLDump - PHP
1717

1818
This is a php version of mysqldump cli that comes with MySQL, without dependencies, output compression and sane defaults.
1919

20-
Out of the box, MySQLDump-PHP supports backing up table structures, the data itself, views and triggers.
20+
Out of the box, MySQLDump-PHP supports backing up table structures, the data itself, views, triggers and events.
2121

2222
MySQLDump-PHP is the only library that supports:
2323
* output binary blobs as hex.
2424
* resolves view dependencies (using Stand-In tables).
25-
* output compared against original mysqldump. Linked to travis-ci testing system.
25+
* output compared against original mysqldump. Linked to travis-ci testing system (testing from php 5.3 to 7.1 & hhvm)
2626
* dumps stored procedures.
27+
* dumps events.
2728
* does extended-insert and/or complete-insert.
29+
* supports virtual columns from MySQL 5.7.
2830

2931
## Important
3032

@@ -111,32 +113,37 @@ Refer to the [wiki](https://github.com/ifsnop/mysqldump-php/wiki/full-example) f
111113
$pdoSettings = array()
112114
)
113115

114-
$dumpSettingsDefault = array(
116+
$dumpSettingsDefault = array(
115117
'include-tables' => array(),
116118
'exclude-tables' => array(),
117-
'compress' => 'None',
118-
'no-data' => false,
119+
'compress' => Mysqldump::NONE,
120+
'init_commands' => array(),
121+
'no-data' => array(),
119122
'reset-auto-increment' => false,
123+
'add-drop-database' => false,
120124
'add-drop-table' => false,
121-
'single-transaction' => true,
122-
'lock-tables' => false,
125+
'add-drop-trigger' => true,
123126
'add-locks' => true,
124-
'extended-insert' => true,
125127
'complete-insert' => false,
128+
'databases' => false,
129+
'default-character-set' => Mysqldump::UTF8,
126130
'disable-keys' => true,
127-
'where' => '',
131+
'extended-insert' => true,
132+
'events' => false,
133+
'hex-blob' => true, /* faster than escaped content */
134+
'net_buffer_length' => self::MAXLINESIZE,
135+
'no-autocommit' => true,
128136
'no-create-info' => false,
129-
'skip-triggers' => false,
130-
'add-drop-trigger' => true,
137+
'lock-tables' => true,
131138
'routines' => false,
132-
'hex-blob' => true,
133-
'databases' => false,
134-
'add-drop-database' => false,
139+
'single-transaction' => true,
140+
'skip-triggers' => false,
135141
'skip-tz-utc' => false,
136-
'no-autocommit' => true,
137-
'default-character-set' => 'utf8',
138142
'skip-comments' => false,
139143
'skip-dump-date' => false,
144+
'where' => '',
145+
/* deprecated */
146+
'disable-foreign-keys-check' => true
140147
);
141148

142149
$pdoSettingsDefaults = array(
@@ -256,7 +263,7 @@ it is identical tests are OK.
256263

257264
## TODO
258265

259-
...
266+
Write more tests.
260267

261268
## Contributing
262269

@@ -269,11 +276,13 @@ This project is open-sourced software licensed under the [GPL license](http://ww
269276

270277
## Credits
271278

279+
After more than 8 years, there is barely anything left from the original source code, but:
280+
272281
Originally based on James Elliott's script from 2009.
273282
http://code.google.com/p/db-mysqldump/
274283

275284
Adapted and extended by Michael J. Calkins.
276285
https://github.com/clouddueling
277286

278-
Currently maintained and developed by Diego Torres.
287+
Currently maintained, developed and improved by Diego Torres.
279288
https://github.com/ifsnop

src/Ifsnop/Mysqldump/Mysqldump.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ private function getTableColumnTypes($tableName) {
658658
'is_numeric'=> $types['is_numeric'],
659659
'is_blob' => $types['is_blob'],
660660
'type' => $types['type'],
661-
'type_sql' => $col['Type']
661+
'type_sql' => $col['Type'],
662+
'is_virtual' => $types['is_virtual']
662663
);
663664
}
664665

@@ -861,7 +862,7 @@ private function listValues($tableName)
861862
$lineSize = 0;
862863

863864
$colStmt = $this->getColumnStmt($tableName);
864-
$stmt = "SELECT $colStmt FROM `$tableName`";
865+
$stmt = "SELECT " . implode(",", $colStmt) . " FROM `$tableName`";
865866

866867
if ($this->dumpSettings['where']) {
867868
$stmt .= " WHERE {$this->dumpSettings['where']}";
@@ -875,9 +876,9 @@ private function listValues($tableName)
875876

876877
if ($this->dumpSettings['complete-insert']) {
877878
$lineSize += $this->compressManager->write(
878-
"INSERT INTO `$tableName` (`" .
879-
implode("`, `", array_keys($this->tableColumnTypes[$tableName])) .
880-
"`) VALUES (" . implode(",", $vals) . ")"
879+
"INSERT INTO `$tableName` (" .
880+
implode(", ", $colStmt) .
881+
") VALUES (" . implode(",", $vals) . ")"
881882
);
882883
} else {
883884
$lineSize += $this->compressManager->write(
@@ -1007,11 +1008,13 @@ function getColumnStmt($tableName)
10071008
$colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`";
10081009
} else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) {
10091010
$colStmt[] = "HEX(`${colName}`) AS `${colName}`";
1011+
} else if ($colType['is_virtual']) {
1012+
$this->dumpSettings['complete-insert'] = true;
1013+
continue;
10101014
} else {
10111015
$colStmt[] = "`${colName}`";
10121016
}
10131017
}
1014-
$colStmt = implode($colStmt, ",");
10151018

10161019
return $colStmt;
10171020
}
@@ -1857,6 +1860,8 @@ public function parseColumnType($colType)
18571860
}
18581861
$colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']);
18591862
$colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']);
1863+
// for virtual 'Extra' -> "STORED GENERATED"
1864+
$colInfo['is_virtual'] = strpos($colType['Extra'], "STORED GENERATED") === false ? false : true;
18601865

18611866
return $colInfo;
18621867
}

tests/create_users.sh

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#!/bin/bash
22

3-
mysql -e "CREATE USER 'travis'@'%';"
4-
mysql -e "CREATE DATABASE test001;"
5-
mysql -e "CREATE DATABASE test002;"
6-
mysql -e "CREATE DATABASE test005;"
7-
mysql -e "CREATE DATABASE test006a;"
8-
mysql -e "CREATE DATABASE test006b;"
9-
mysql -e "CREATE DATABASE test008;"
10-
mysql -e "CREATE DATABASE test009;"
11-
mysql -e "CREATE DATABASE test010;"
12-
mysql -e "GRANT ALL PRIVILEGES ON test001.* TO 'travis'@'%' WITH GRANT OPTION;"
13-
mysql -e "GRANT ALL PRIVILEGES ON test002.* TO 'travis'@'%' WITH GRANT OPTION;"
14-
mysql -e "GRANT ALL PRIVILEGES ON test005.* TO 'travis'@'%' WITH GRANT OPTION;"
15-
mysql -e "GRANT ALL PRIVILEGES ON test006a.* TO 'travis'@'%' WITH GRANT OPTION;"
16-
mysql -e "GRANT ALL PRIVILEGES ON test006b.* TO 'travis'@'%' WITH GRANT OPTION;"
17-
mysql -e "GRANT ALL PRIVILEGES ON test008.* TO 'travis'@'%' WITH GRANT OPTION;"
18-
mysql -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT OPTION;"
19-
mysql -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;"
20-
mysql -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';"
21-
mysql -e "GRANT SELECT ON mysql.proc to 'travis'@'%';"
22-
mysql -e "FLUSH PRIVILEGES;"
3+
mysql -u root -e "CREATE USER 'travis'@'%';"
4+
mysql -u root -e "CREATE DATABASE test001;"
5+
mysql -u root -e "CREATE DATABASE test002;"
6+
mysql -u root -e "CREATE DATABASE test005;"
7+
mysql -u root -e "CREATE DATABASE test006a;"
8+
mysql -u root -e "CREATE DATABASE test006b;"
9+
mysql -u root -e "CREATE DATABASE test008;"
10+
mysql -u root -e "CREATE DATABASE test009;"
11+
mysql -u root -e "CREATE DATABASE test010;"
12+
mysql -u root -e "CREATE DATABASE test011;"
13+
mysql -u root -e "GRANT ALL PRIVILEGES ON test001.* TO 'travis'@'%' WITH GRANT OPTION;"
14+
mysql -u root -e "GRANT ALL PRIVILEGES ON test002.* TO 'travis'@'%' WITH GRANT OPTION;"
15+
mysql -u root -e "GRANT ALL PRIVILEGES ON test005.* TO 'travis'@'%' WITH GRANT OPTION;"
16+
mysql -u root -e "GRANT ALL PRIVILEGES ON test006a.* TO 'travis'@'%' WITH GRANT OPTION;"
17+
mysql -u root -e "GRANT ALL PRIVILEGES ON test006b.* TO 'travis'@'%' WITH GRANT OPTION;"
18+
mysql -u root -e "GRANT ALL PRIVILEGES ON test008.* TO 'travis'@'%' WITH GRANT OPTION;"
19+
mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT OPTION;"
20+
mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;"
21+
mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO 'travis'@'%' WITH GRANT OPTION;"
22+
mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';"
23+
mysql -u root -e "GRANT SELECT ON mysql.proc to 'travis'@'%';"
24+
mysql -u root -e "FLUSH PRIVILEGES;"

tests/test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,18 @@
8383
array("events" => true));
8484
$dump->start("mysqldump-php_test010.sql");
8585

86+
$dump = new IMysqldump\Mysqldump(
87+
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test011",
88+
"travis",
89+
"",
90+
array('complete-insert' => false));
91+
$dump->start("mysqldump-php_test011a.sql");
92+
93+
$dump = new IMysqldump\Mysqldump(
94+
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test011",
95+
"travis",
96+
"",
97+
array('complete-insert' => true));
98+
$dump->start("mysqldump-php_test011b.sql");
99+
86100
exit;

tests/test.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ for i in 000; do
1818
done
1919
}
2020

21-
for i in $(seq 0 30) ; do
21+
for i in $(seq 0 35) ; do
2222
ret[$i]=0
2323
done
2424

@@ -31,6 +31,7 @@ mysql -utravis < test006.src.sql; ret[((index++))]=$?
3131
mysql -utravis < test008.src.sql; ret[((index++))]=$?
3232
mysql -utravis < test009.src.sql; ret[((index++))]=$?
3333
mysql -utravis < test010.src.sql; ret[((index++))]=$?
34+
mysql -utravis < test011.src.sql; ret[((index++))]=$?
3435

3536
checksum_test001 > test001.src.checksum
3637
checksum_test002 > test002.src.checksum
@@ -82,6 +83,7 @@ cat test002.src.sql | grep ^INSERT > test002.filtered.sql
8283
cat test005.src.sql | grep ^INSERT > test005.filtered.sql
8384
cat test008.src.sql | grep FOREIGN > test008.filtered.sql
8485
cat test010.src.sql | grep CREATE | grep EVENT > test010.filtered.sql
86+
cat test011.src.sql | grep INSERT > test011.filtered.sql
8587
cat mysqldump_test001.sql | grep ^INSERT > mysqldump_test001.filtered.sql
8688
cat mysqldump_test002.sql | grep ^INSERT > mysqldump_test002.filtered.sql
8789
cat mysqldump_test005.sql | grep ^INSERT > mysqldump_test005.filtered.sql
@@ -90,6 +92,8 @@ cat mysqldump-php_test002.sql | grep ^INSERT > mysqldump-php_test002.filtered.sq
9092
cat mysqldump-php_test005.sql | grep ^INSERT > mysqldump-php_test005.filtered.sql
9193
cat mysqldump-php_test008.sql | grep FOREIGN > mysqldump-php_test008.filtered.sql
9294
cat mysqldump-php_test010.sql | grep CREATE | grep EVENT > mysqldump-php_test010.filtered.sql
95+
cat mysqldump-php_test011a.sql | grep INSERT > mysqldump-php_test011a.filtered.sql
96+
cat mysqldump-php_test011b.sql | grep INSERT > mysqldump-php_test011b.filtered.sql
9397

9498
diff test001.filtered.sql mysqldump_test001.filtered.sql
9599
ret[((index++))]=$?
@@ -114,22 +118,28 @@ ret[((index++))]=$?
114118
diff test008.filtered.sql mysqldump-php_test008.filtered.sql
115119
ret[((index++))]=$?
116120

117-
#test 24 - reset-auto-increment
121+
#test reset-auto-increment
118122
test009=`cat mysqldump-php_test009.sql | grep -i ENGINE | grep AUTO_INCREMENT`
119123
if [[ -z $test009 ]]; then ret[((index++))]=0; else ret[((index++))]=1; fi
120124

121125
# test backup events
122126
diff test010.filtered.sql mysqldump-php_test010.filtered.sql
123127
ret[((index++))]=$?
124128

129+
# test virtual column support, with simple inserts forced to complete (a) and complete inserts (b)
130+
diff test011.filtered.sql mysqldump-php_test011a.filtered.sql
131+
ret[((index++))]=$?
132+
diff test011.filtered.sql mysqldump-php_test011b.filtered.sql
133+
ret[((index++))]=$?
134+
125135
rm *.checksum 2> /dev/null
126136
rm *.filtered.sql 2> /dev/null
127137
rm mysqldump* 2> /dev/null
128138

129139
echo "Done $index tests"
130140

131141
retvalue=0
132-
for i in $(seq 0 30) ; do
142+
for i in $(seq 0 35) ; do
133143
if [[ ${ret[$i]} -ne 0 ]]; then
134144
echo "test $i returned ${ret[$i]}"
135145
retvalue=${ret[$i]}

tests/test011.src.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
DROP DATABASE IF EXISTS `test011`;
2+
CREATE DATABASE `test011`;
3+
USE `test011`;
4+
5+
-- MySQL dump 10.13 Distrib 5.7.15, for Linux (x86_64)
6+
--
7+
-- Host: localhost Database: test
8+
-- ------------------------------------------------------
9+
-- Server version 5.7.15
10+
11+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
12+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
13+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
14+
/*!40101 SET NAMES utf8 */;
15+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
16+
/*!40103 SET TIME_ZONE='+00:00' */;
17+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
18+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
19+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
20+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
21+
22+
--
23+
-- Table structure for table `test011`
24+
--
25+
26+
DROP TABLE IF EXISTS `test011`;
27+
/*!40101 SET @saved_cs_client = @@character_set_client */;
28+
/*!40101 SET character_set_client = utf8 */;
29+
CREATE TABLE `test011` (
30+
`id` int(11) NOT NULL,
31+
`hash` char(32) CHARACTER SET ascii COLLATE ascii_bin GENERATED ALWAYS AS (md5(`id`)) STORED NOT NULL
32+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
33+
/*!40101 SET character_set_client = @saved_cs_client */;
34+
35+
--
36+
-- Dumping data for table `test011`
37+
--
38+
39+
LOCK TABLES `test011` WRITE;
40+
/*!40000 ALTER TABLE `test011` DISABLE KEYS */;
41+
INSERT INTO `test011` (`id`) VALUES (159413),(294775);
42+
/*!40000 ALTER TABLE `test011` ENABLE KEYS */;
43+
UNLOCK TABLES;
44+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
45+
46+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
47+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
48+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
49+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
50+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
51+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
52+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

0 commit comments

Comments
 (0)