Skip to content

Commit 088cbb3

Browse files
authored
Feature/truncate test (#38)
Added tests now include php 7.2 and MariaDb 10.3 Added truncate table test (#37) Added MariaDb events ids to const Added filtering dummy events generated by MariaDB Added missing throws in BasicTest
1 parent b565499 commit 088cbb3

File tree

5 files changed

+121
-12
lines changed

5 files changed

+121
-12
lines changed

.travis.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ php:
66
- 5.6
77
- 7.0
88
- 7.1
9+
- 7.2
910
env:
1011
- DB=mysql57
1112
- DB=mysql56
13+
- DB=mariadb
1214

1315
cache:
1416
apt: true
@@ -20,25 +22,23 @@ sudo: required
2022

2123
before_script:
2224
- "sudo /etc/init.d/mysql stop || true"
23-
- "sudo apt-get remove mysql* -y"
25+
- "sudo apt-get remove mysql* -y --purge"
2426
- "if [ $DB = 'mysql57' ]; then echo deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7 | sudo tee /etc/apt/sources.list.d/mysql.list; sudo apt-get update; sudo apt-get install mysql-server -y --allow-unauthenticated; fi"
2527
- "if [ $DB = 'mysql56' ]; then echo deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.6 | sudo tee /etc/apt/sources.list.d/mysql.list; sudo apt-get update; sudo apt-get install mysql-server -y --allow-unauthenticated; fi"
26-
- "sudo mysql_upgrade"
28+
- "if [ $DB = 'mariadb' ]; then sudo rm -rf /var/lib/mysql /etc/mysql; echo deb http://ftp.hosteurope.de/mirror/mariadb.org/repo/10.3/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/mysql.list; sudo apt-get update; sudo apt-get install mariadb-server-10.3 -y --allow-unauthenticated; fi"
29+
- "sudo mysql_upgrade --force"
2730

2831
# Config
2932
- "echo '[mysqld]' | sudo tee /etc/mysql/conf.d/replication.cnf"
3033
- "echo 'log-bin=mysql-bin' | sudo tee -a /etc/mysql/conf.d/replication.cnf"
3134
- "echo 'server-id=1' | sudo tee -a /etc/mysql/conf.d/replication.cnf"
3235
- "echo 'binlog-format = row' | sudo tee -a /etc/mysql/conf.d/replication.cnf"
36+
- "cat /etc/mysql/conf.d/replication.cnf"
3337

34-
# Enable GTID
35-
- "echo '[mysqld]' | sudo tee /etc/mysql/conf.d/gtid.cnf"
36-
- "echo 'gtid_mode=ON' | sudo tee -a /etc/mysql/conf.d/gtid.cnf"
37-
- "echo 'enforce_gtid_consistency' | sudo tee -a /etc/mysql/conf.d/gtid.cnf"
38-
- "echo 'binlog_format=ROW' | sudo tee -a /etc/mysql/conf.d/gtid.cnf"
39-
- "echo 'log_slave_updates' | sudo tee -a /etc/mysql/conf.d/gtid.cnf"
38+
# Enable GTID (only for mysql 5.*)
39+
- "if [ $DB != 'mariadb' ]; then echo '[mysqld]' | sudo tee /etc/mysql/conf.d/gtid.cnf; echo 'gtid_mode=ON' | sudo tee -a /etc/mysql/conf.d/gtid.cnf; echo 'enforce_gtid_consistency' | sudo tee -a /etc/mysql/conf.d/gtid.cnf; echo 'binlog_format=ROW' | sudo tee -a /etc/mysql/conf.d/gtid.cnf; echo 'log_slave_updates' | sudo tee -a /etc/mysql/conf.d/gtid.cnf; cat /etc/mysql/conf.d/gtid.cnf; fi"
4040

41-
# Start mysql (avoid errors to have logs)
41+
# Start mysql (avoid errors to have logs)/var/lib/mysql
4242
- "sudo /etc/init.d/mysql restart || true"
4343
- "sudo tail -1000 /var/log/syslog"
4444

@@ -49,6 +49,7 @@ before_script:
4949

5050
- if [ $DB = 'mysql56' ]; then echo "USE mysql;\nUPDATE user SET password=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root; fi
5151
- if [ $DB = 'mysql57' ]; then echo "USE mysql;\nUPDATE user SET authentication_string=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root; fi
52+
- if [ $DB = 'mariadb' ]; then echo "USE mysql;\nUPDATE user SET password=PASSWORD('root') WHERE User='root';\nFLUSH PRIVILEGES;\n" | mysql -u root; fi
5253

5354
install:
5455
travis_retry composer install --no-interaction --prefer-source;

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
# Release Notes
44

5+
## v5.0.1 (2018-05-29)
6+
- Added tests now include php 7.2 and MariaDb 10.3
7+
- Added truncate table test (#37)
8+
- Added MariaDb events ids to const
9+
- Added filtering dummy events generated by MariaDB
10+
- Added missing throws in BasicTest
11+
512
## v5.0.0 (2018-04-27)
613
- Removed unused classes from code and merged some classes to one class
714
- Added ability in MySQLReplicationFactory to provide implementations interfaces in constructor. This will give ability to replace default classes to your own

src/MySQLReplication/Definitions/ConstEventType.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,16 @@ class ConstEventType
6161
const DELETE_ROWS_EVENT_V2 = 32;
6262

6363
// mariadb
64+
// https://github.com/MariaDB/server/blob/10.4/sql/log_event.h
65+
const MARIA_BINLOG_CHECKPOINT_EVENT = 161;
6466
const MARIA_GTID_EVENT = 162;
65-
}
67+
const MARIA_GTID_LIST_EVENT = 163;
68+
const MARIA_START_ENCRYPTION_EVENT = 164;
69+
const MARIA_QUERY_COMPRESSED_EVENT = 165;
70+
const MARIA_WRITE_ROWS_COMPRESSED_EVENT_V1 = 166;
71+
const MARIA_UPDATE_ROWS_COMPRESSED_EVENT_V1 = 167;
72+
const MARIA_DELETE_ROWS_COMPRESSED_EVENT_V1 = 168;
73+
const MARIA_WRITE_ROWS_COMPRESSED_EVENT = 169;
74+
const MARIA_UPDATE_ROWS_COMPRESSED_EVENT = 170;
75+
const MARIA_DELETE_ROWS_COMPRESSED_EVENT = 171;
76+
}

src/MySQLReplication/Event/Event.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use MySQLReplication\BinaryDataReader\BinaryDataReader;
66
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
77
use MySQLReplication\BinLog\BinLogException;
8+
use MySQLReplication\BinLog\BinLogServerInfo;
89
use MySQLReplication\BinLog\BinLogSocketConnect;
910
use MySQLReplication\Config\Config;
1011
use MySQLReplication\Definitions\ConstEventType;
1112
use MySQLReplication\Event\DTO\EventDTO;
1213
use MySQLReplication\Event\DTO\FormatDescriptionEventDTO;
1314
use MySQLReplication\Event\DTO\HeartbeatDTO;
15+
use MySQLReplication\Event\DTO\QueryDTO;
1416
use MySQLReplication\Event\RowEvent\RowEventFactory;
1517
use MySQLReplication\Exception\MySQLReplicationException;
1618
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException;
@@ -25,6 +27,8 @@
2527
*/
2628
class Event
2729
{
30+
const MARIADB_DUMMY_QUERY = '# Dum';
31+
2832
/**
2933
* @var BinLogSocketConnect
3034
*/
@@ -111,7 +115,9 @@ public function consume()
111115
} else if (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) {
112116
$eventDTO = (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO();
113117
} else if (ConstEventType::QUERY_EVENT === $eventInfo->getType()) {
114-
$eventDTO = (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO();
118+
$eventDTO = $this->filterDummyMariaDbEvents(
119+
(new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO()
120+
);
115121
} else if (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) {
116122
$eventDTO = (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO();
117123
} else if (ConstEventType::FORMAT_DESCRIPTION_EVENT === $eventInfo->getType()) {
@@ -141,6 +147,19 @@ private function createEventInfo(BinaryDataReader $binaryDataReader)
141147
);
142148
}
143149

150+
/**
151+
* @param QueryDTO $queryDTO
152+
* @return QueryDTO|null
153+
*/
154+
private function filterDummyMariaDbEvents(QueryDTO $queryDTO)
155+
{
156+
if (BinLogServerInfo::isMariaDb() && false !== strpos($queryDTO->getQuery(), self::MARIADB_DUMMY_QUERY)) {
157+
return null;
158+
}
159+
160+
return $queryDTO;
161+
}
162+
144163
/**
145164
* @param EventDTO $eventDTO
146165
*/

tests/Integration/BasicTest.php

+72-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace MySQLReplication\Tests\Integration;
44

5+
use Doctrine\DBAL\DBALException;
6+
use MySQLReplication\BinLog\BinLogException;
7+
use MySQLReplication\Config\ConfigException;
58
use MySQLReplication\Definitions\ConstEventType;
69
use MySQLReplication\Event\DTO\DeleteRowsDTO;
710
use MySQLReplication\Event\DTO\QueryDTO;
811
use MySQLReplication\Event\DTO\TableMapDTO;
912
use MySQLReplication\Event\DTO\UpdateRowsDTO;
1013
use MySQLReplication\Event\DTO\WriteRowsDTO;
1114
use MySQLReplication\Event\DTO\XidDTO;
15+
use MySQLReplication\Exception\MySQLReplicationException;
16+
use MySQLReplication\Gtid\GtidException;
17+
use MySQLReplication\Socket\SocketException;
18+
use Psr\SimpleCache\InvalidArgumentException;
1219

1320
/**
1421
* Class BasicTest
@@ -18,6 +25,9 @@ class BasicTest extends BaseTest
1825
{
1926
/**
2027
* @test
28+
* @throws DBALException
29+
* @throws InvalidArgumentException
30+
* @throws MySQLReplicationException
2131
*/
2232
public function shouldGetDeleteEvent()
2333
{
@@ -41,6 +51,9 @@ public function shouldGetDeleteEvent()
4151

4252
/**
4353
* @test
54+
* @throws DBALException
55+
* @throws InvalidArgumentException
56+
* @throws MySQLReplicationException
4457
*/
4558
public function shouldGetUpdateEvent()
4659
{
@@ -66,14 +79,16 @@ public function shouldGetUpdateEvent()
6679

6780
/**
6881
* @test
82+
* @throws DBALException
83+
* @throws InvalidArgumentException
84+
* @throws MySQLReplicationException
6985
*/
7086
public function shouldGetWriteEventDropTable()
7187
{
7288
$this->connection->exec($createExpected = 'CREATE TABLE `test` (id INTEGER(11))');
7389
$this->connection->exec('INSERT INTO `test` VALUES (1)');
7490
$this->connection->exec($dropExpected = 'DROP TABLE `test`');
7591

76-
7792
/** @var QueryDTO $event */
7893
$event = $this->getEvent();
7994
self::assertInstanceOf(QueryDTO::class, $event);
@@ -103,6 +118,9 @@ public function shouldGetWriteEventDropTable()
103118

104119
/**
105120
* @test
121+
* @throws DBALException
122+
* @throws InvalidArgumentException
123+
* @throws MySQLReplicationException
106124
*/
107125
public function shouldGetQueryEventCreateTable()
108126
{
@@ -118,6 +136,13 @@ public function shouldGetQueryEventCreateTable()
118136

119137
/**
120138
* @test
139+
* @throws BinLogException
140+
* @throws ConfigException
141+
* @throws DBALException
142+
* @throws GtidException
143+
* @throws InvalidArgumentException
144+
* @throws MySQLReplicationException
145+
* @throws SocketException
121146
*/
122147
public function shouldDropColumn()
123148
{
@@ -146,6 +171,13 @@ public function shouldDropColumn()
146171

147172
/**
148173
* @test
174+
* @throws BinLogException
175+
* @throws ConfigException
176+
* @throws DBALException
177+
* @throws GtidException
178+
* @throws InvalidArgumentException
179+
* @throws MySQLReplicationException
180+
* @throws SocketException
149181
*/
150182
public function shouldFilterEvents()
151183
{
@@ -168,6 +200,13 @@ public function shouldFilterEvents()
168200

169201
/**
170202
* @test
203+
* @throws DBALException
204+
* @throws BinLogException
205+
* @throws ConfigException
206+
* @throws MySQLReplicationException
207+
* @throws GtidException
208+
* @throws SocketException
209+
* @throws InvalidArgumentException
171210
*/
172211
public function shouldFilterTables()
173212
{
@@ -197,4 +236,36 @@ public function shouldFilterTables()
197236
self::assertEquals($expectedTable, $event->getTableMap()->getTable());
198237
self::assertEquals($expectedValue, $event->getValues()[0]['data']);
199238
}
239+
240+
/**
241+
* @test
242+
* @throws DBALException
243+
* @throws InvalidArgumentException
244+
* @throws MySQLReplicationException
245+
*/
246+
public function shouldTruncateTable()
247+
{
248+
$this->disconnect();
249+
250+
$this->configBuilder->withEventsOnly(
251+
[ConstEventType::QUERY_EVENT]
252+
);
253+
254+
$this->connect();
255+
256+
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
257+
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
258+
259+
$this->connection->exec('CREATE TABLE test_truncate_column (id INTEGER(11), data VARCHAR(50))');
260+
$this->connection->exec('INSERT INTO test_truncate_column VALUES (1, \'A value\')');
261+
$this->connection->exec('TRUNCATE TABLE test_truncate_column');
262+
263+
/** @var QueryDTO $event */
264+
$event = $this->getEvent();
265+
self::assertSame('CREATE TABLE test_truncate_column (id INTEGER(11), data VARCHAR(50))', $event->getQuery());
266+
$event = $this->getEvent();
267+
self::assertSame('BEGIN', $event->getQuery());
268+
$event = $this->getEvent();
269+
self::assertSame('TRUNCATE TABLE test_truncate_column', $event->getQuery());
270+
}
200271
}

0 commit comments

Comments
 (0)