From 724901c522d0336344d3dfd59fa24a4050ddc9e3 Mon Sep 17 00:00:00 2001 From: Walter Huf Date: Thu, 29 Jan 2026 14:09:46 +0100 Subject: [PATCH 1/2] Add tests for Datetime2 parsing --- tests/Integration/TypesTest.php | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Integration/TypesTest.php b/tests/Integration/TypesTest.php index 42a83ce..eb229ea 100644 --- a/tests/Integration/TypesTest.php +++ b/tests/Integration/TypesTest.php @@ -381,6 +381,46 @@ public function testShouldReturnNullOnZeroDateDateTime(): void self::assertNull($event->values[0]['test']); } + public function testShouldBeDateTime2(): void + { + $create_query = 'CREATE TABLE test (test DATETIME(6));'; + $insert_query = 'INSERT INTO test VALUES("1984-12-03 12:33:07.023450")'; + + $event = $this->createAndInsertValue($create_query, $insert_query); + + self::assertEquals('1984-12-03 12:33:07.023450', $event->values[0]['test']); + } + + public function testShouldBeZeroDateTime2(): void + { + $create_query = 'CREATE TABLE test (id INTEGER, test DATETIME(6) NOT NULL DEFAULT 0);'; + $insert_query = 'INSERT INTO test (id) VALUES(1)'; + + $event = $this->createAndInsertValue($create_query, $insert_query); + + self::assertNull($event->values[0]['test']); + } + + public function testShouldBeBrokenDateTime2(): void + { + $create_query = 'CREATE TABLE test (test DATETIME(6) NOT NULL);'; + $insert_query = 'INSERT INTO test VALUES("2013-00-00 00:00:00")'; + + $event = $this->createAndInsertValue($create_query, $insert_query); + + self::assertNull($event->values[0]['test']); + } + + public function testShouldReturnNullOnZeroDateDateTime2(): void + { + $create_query = 'CREATE TABLE test (test DATETIME(6) NOT NULL);'; + $insert_query = 'INSERT INTO test VALUES("0000-00-00 00:00:00")'; + + $event = $this->createAndInsertValue($create_query, $insert_query); + + self::assertNull($event->values[0]['test']); + } + public function testShouldBeYear(): void { $create_query = 'CREATE TABLE test (test YEAR(4), test2 YEAR, test3 YEAR)'; From 2b81d0e9fa89f5320d3a8eadf83b07966b6b0700 Mon Sep 17 00:00:00 2001 From: Walter Huf Date: Thu, 29 Jan 2026 14:14:22 +0100 Subject: [PATCH 2/2] Fix the microseconds parsing for Datetime2 --- src/MySQLReplication/Event/RowEvent/RowEvent.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MySQLReplication/Event/RowEvent/RowEvent.php b/src/MySQLReplication/Event/RowEvent/RowEvent.php index d5cbf77..9717f17 100644 --- a/src/MySQLReplication/Event/RowEvent/RowEvent.php +++ b/src/MySQLReplication/Event/RowEvent/RowEvent.php @@ -707,7 +707,11 @@ protected function getDatetime2(ColumnDTO $columnDTO): ?string $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second ); if ($formattedDate) { - return $formattedDate . $fsp; + if ($fsp > 0) { + return vsprintf('%s.%06u', [$formattedDate, $fsp]); + } else { + return $formattedDate; + } } return null;