From 58e8476fc6620a4938c839946da53e6a0ae991f0 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Fri, 11 Apr 2025 18:39:17 +0200 Subject: [PATCH 01/10] Fix Date/Datetime seeding --- src/Phinx/Db/Adapter/PdoAdapter.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 4b025805a..77ef4b982 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -15,6 +15,8 @@ use Cake\Database\Query\InsertQuery; use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use PDO; use PDOException; @@ -430,6 +432,10 @@ public function bulkinsert(Table $table, array $rows): void foreach ($row as $v) { if ($v instanceof Literal) { continue; + } elseif ($v instanceof DateTime) { + $vals[] = $v->toDateTimeString(); + } elseif ($v instanceof Date) { + $vals[] = $v->toDateString(); } elseif (is_bool($v)) { $vals[] = $this->castToBool($v); } else { From a6c40a337c5699d73b49e20b2791eed0315b355c Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 15 Apr 2025 23:31:18 +0000 Subject: [PATCH 02/10] Update composer.json --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 1b4ccbe0c..405255b8f 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,7 @@ "ext-json": "*", "ext-pdo": "*", "cakephp/cakephp-codesniffer": "^5.0", + "cakephp/i18n": "^5.0", "phpunit/phpunit": "^9.5.19", "symfony/yaml": "^3.4|^4.0|^5.0|^6.0|^7.0" }, From b5ecfb7226f8c4c02f41f6637d51dd9ba7b7d7ab Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Wed, 16 Apr 2025 02:02:20 +0200 Subject: [PATCH 03/10] Add test case (#2351) --- tests/Phinx/Console/Command/SeedRunTest.php | 14 +++++++------- tests/Phinx/Migration/_files/seeds/UserSeeder.php | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/Phinx/Console/Command/SeedRunTest.php b/tests/Phinx/Console/Command/SeedRunTest.php index 17ede581a..0c6f7be5a 100644 --- a/tests/Phinx/Console/Command/SeedRunTest.php +++ b/tests/Phinx/Console/Command/SeedRunTest.php @@ -63,7 +63,7 @@ public function testExecute() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$this->config, $this->input, $this->output]) ->getMock(); @@ -102,7 +102,7 @@ public function testExecuteWithDsn() ]); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$config, $this->input, $this->output]) ->getMock(); @@ -127,7 +127,7 @@ public function testExecuteWithEnvironmentOption() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$this->config, $this->input, $this->output]) ->getMock(); @@ -153,7 +153,7 @@ public function testExecuteWithInvalidEnvironmentOption() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$this->config, $this->input, $this->output]) ->getMock(); @@ -180,7 +180,7 @@ public function testDatabaseNameSpecified() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$this->config, $this->input, $this->output]) ->getMock(); @@ -205,7 +205,7 @@ public function testExecuteMultipleSeeders() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$this->config, $this->input, $this->output]) ->getMock(); @@ -256,7 +256,7 @@ public function testSeedRunMemorySqlite() $command = $application->find('seed:run'); // mock the manager class - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ + /** @var \Phinx\Migration\Manager|\PHPUnit\Framework\MockObject\MockObject $managerStub */ $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') ->setConstructorArgs([$config, $this->input, $this->output]) ->getMock(); diff --git a/tests/Phinx/Migration/_files/seeds/UserSeeder.php b/tests/Phinx/Migration/_files/seeds/UserSeeder.php index db9dfdffa..f99bc2bb2 100644 --- a/tests/Phinx/Migration/_files/seeds/UserSeeder.php +++ b/tests/Phinx/Migration/_files/seeds/UserSeeder.php @@ -1,5 +1,7 @@ 'foo', - 'created' => date('Y-m-d H:i:s'), + 'created' => new Date(), ], [ 'name' => 'bar', - 'created' => date('Y-m-d H:i:s'), + 'created' => new DateTime(), ], ]; From c9aca9993d264cce563f94cf273a6ebad9dd6da3 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 09:58:58 -0600 Subject: [PATCH 04/10] rework tests Signed-off-by: Matthew Peveler --- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 26 +++++++++++++++++++ .../Phinx/Db/Adapter/PostgresAdapterTest.php | 26 +++++++++++++++++++ tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 26 +++++++++++++++++++ .../Phinx/Db/Adapter/SqlServerAdapterTest.php | 26 +++++++++++++++++++ .../Migration/_files/seeds/UserSeeder.php | 4 +-- 5 files changed, 106 insertions(+), 2 deletions(-) diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 95fba2e77..c7924ff84 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -4,6 +4,8 @@ namespace Test\Phinx\Db\Adapter; use Cake\Database\Query; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use PDO; use PDOException; @@ -2215,6 +2217,30 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column2']); } + public function testBulkInsertDates() + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + } + public function testInsertData() { $data = [ diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index ec4019f89..2f10eece4 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -4,6 +4,8 @@ namespace Test\Phinx\Db\Adapter; use Cake\Database\Query; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use PDO; use Phinx\Db\Adapter\AbstractAdapter; @@ -2447,6 +2449,30 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column2']); } + public function testBulkInsertDates() + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + } + public function testInsertData() { $table = new Table('table1', [], $this->adapter); diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index ebe738af2..cbf6b7d45 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -5,6 +5,8 @@ use BadMethodCallException; use Cake\Database\Query; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use Exception; use InvalidArgumentException; use PDO; @@ -1901,6 +1903,30 @@ public function testBulkInsertDataEnum() $this->assertEquals('c', $rows[0]['column3']); } + public function testBulkInsertDates() + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + } + public function testNullWithoutDefaultValue() { $this->markTestSkipped('Skipping for now. See Github Issue #265.'); diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 8a5a96c7f..5f33376b2 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -5,6 +5,8 @@ use BadMethodCallException; use Cake\Database\Query; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use PDO; use Phinx\Db\Adapter\SqlServerAdapter; @@ -1345,6 +1347,30 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00.000', $rows[2]['column2']); } + public function testBulkInsertDates() + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + } + public function testInsertData() { $table = new Table('table1', [], $this->adapter); diff --git a/tests/Phinx/Migration/_files/seeds/UserSeeder.php b/tests/Phinx/Migration/_files/seeds/UserSeeder.php index f99bc2bb2..60db1e7c8 100644 --- a/tests/Phinx/Migration/_files/seeds/UserSeeder.php +++ b/tests/Phinx/Migration/_files/seeds/UserSeeder.php @@ -11,11 +11,11 @@ public function run(): void $data = [ [ 'name' => 'foo', - 'created' => new Date(), + 'created' => date('Y-m-d H:i:s'), ], [ 'name' => 'bar', - 'created' => new DateTime(), + 'created' => date('Y-m-d H:i:s'), ], ]; From b9d78092e6a65d98aacc6156cf9b8c657dfce74e Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 10:05:10 -0600 Subject: [PATCH 05/10] fix sqlite test --- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index cbf6b7d45..516f2b6a8 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1923,7 +1923,7 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); + $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $rows[0]['created']); $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); } From 3927ee42176d1ae943499f93575244cf4caa57ea Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 10:34:00 -0600 Subject: [PATCH 06/10] use more precise testing --- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 4 ++-- tests/Phinx/Db/Adapter/PostgresAdapterTest.php | 4 ++-- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 4 ++-- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index c7924ff84..49c8e3e77 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -2237,8 +2237,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 2f10eece4..af90f4e26 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -2469,8 +2469,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index 516f2b6a8..a72160619 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1923,8 +1923,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $rows[0]['created']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + $this->assertMatchesRegularExpression($data[0]['created']->toDateString(), $rows[0]['created']); + $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testNullWithoutDefaultValue() diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 5f33376b2..193538e9d 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -1367,8 +1367,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[0]['created']); - $this->assertMatchesRegularExpression('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $rows[1]['created']); + $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() From 742dce6c9fe011349e4dffc9aec07bdffaeb3f2c Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 10:39:13 -0600 Subject: [PATCH 07/10] use assertEquals Signed-off-by: Matthew Peveler --- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 6 +++--- tests/Phinx/Db/Adapter/PostgresAdapterTest.php | 6 +++--- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 6 +++--- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 49c8e3e77..36eb88a02 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -2217,7 +2217,7 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column2']); } - public function testBulkInsertDates() + public function testBulkInsertDates(): void { $data = [ [ @@ -2237,8 +2237,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); - $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index af90f4e26..7c686b235 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -2449,7 +2449,7 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column2']); } - public function testBulkInsertDates() + public function testBulkInsertDates(): void { $data = [ [ @@ -2469,8 +2469,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); - $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index a72160619..cbec9d959 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1903,7 +1903,7 @@ public function testBulkInsertDataEnum() $this->assertEquals('c', $rows[0]['column3']); } - public function testBulkInsertDates() + public function testBulkInsertDates(): void { $data = [ [ @@ -1923,8 +1923,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression($data[0]['created']->toDateString(), $rows[0]['created']); - $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals($data[0]['created']->toDateString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testNullWithoutDefaultValue() diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 193538e9d..22cd33955 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -1347,7 +1347,7 @@ public function testBulkInsertLiteral() $this->assertEquals('2025-01-01 00:00:00.000', $rows[2]['column2']); } - public function testBulkInsertDates() + public function testBulkInsertDates(): void { $data = [ [ @@ -1367,8 +1367,8 @@ public function testBulkInsertDates() $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertMatchesRegularExpression($data[0]['created']->toDateTimeString(), $rows[0]['created']); - $this->assertMatchesRegularExpression($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); } public function testInsertData() From 89096154797910cc2910cc75ae13e613ef9fbd00 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 10:49:31 -0600 Subject: [PATCH 08/10] Include milliseconds in sqlserver test Signed-off-by: Matthew Peveler --- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 22cd33955..943f218e5 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -1367,8 +1367,8 @@ public function testBulkInsertDates(): void $rows = $this->adapter->fetchAll('SELECT * FROM table1'); $this->assertEquals('foo', $rows[0]['name']); $this->assertEquals('bar', $rows[1]['name']); - $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); - $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals($data[0]['created']->format('Y-m-d H:i:s.000'), $rows[0]['created']); + $this->assertEquals($data[1]['created']->format('Y-m-d H:i:s.000'), $rows[1]['created']); } public function testInsertData() From 8675429fba7c33e25aedcffc6ca931eed66531b7 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 11:10:16 -0600 Subject: [PATCH 09/10] add support for regular inserts --- src/Phinx/Db/Adapter/PdoAdapter.php | 55 +++++++++++-------- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 28 ++++++++++ .../Phinx/Db/Adapter/PostgresAdapterTest.php | 28 ++++++++++ tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 28 ++++++++++ .../Phinx/Db/Adapter/SqlServerAdapterTest.php | 28 ++++++++++ 5 files changed, 143 insertions(+), 24 deletions(-) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 77ef4b982..61ccbabcf 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -321,6 +321,32 @@ public function fetchAll(string $sql): array return $this->query($sql)->fetchAll(); } + /** + * Get the parameters array for prepared insert statement + * + * @param array $row Row to be inserted into DB + * @return array + */ + protected function getInsertParameters(array $row): array + { + $values = []; + foreach ($row as $value) { + if ($value instanceof Literal) { + continue; + } elseif ($value instanceof DateTime) { + $values[] = $value->toDateTimeString(); + } elseif ($value instanceof Date) { + $values[] = $value->toDateString(); + } elseif (is_bool($value)) { + $values[] = $this->castToBool($value); + } else { + $values[] = $value; + } + } + + return $values; + } + /** * @inheritDoc */ @@ -344,21 +370,14 @@ public function insert(Table $table, array $row): void $this->output->writeln($sql); } else { $sql .= '('; - $vals = []; $values = []; foreach ($row as $value) { $values[] = $value instanceof Literal ? (string)$value : '?'; - if (!($value instanceof Literal)) { - if (is_bool($value)) { - $vals[] = $this->castToBool($value); - } else { - $vals[] = $value; - } - } } + $params = $this->getInsertParameters($row); $sql .= implode(', ', $values) . ')'; $stmt = $this->getConnection()->prepare($sql); - $stmt->execute($vals); + $stmt->execute($params); } } @@ -426,25 +445,13 @@ public function bulkinsert(Table $table, array $rows): void } $sql .= implode(',', $queries); $stmt = $this->getConnection()->prepare($sql); - $vals = []; + $params = []; foreach ($rows as $row) { - foreach ($row as $v) { - if ($v instanceof Literal) { - continue; - } elseif ($v instanceof DateTime) { - $vals[] = $v->toDateTimeString(); - } elseif ($v instanceof Date) { - $vals[] = $v->toDateString(); - } elseif (is_bool($v)) { - $vals[] = $this->castToBool($v); - } else { - $vals[] = $v; - } - } + $params = array_merge($params, $this->getInsertParameters($row)); } - $stmt->execute($vals); + $stmt->execute($params); } } diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 36eb88a02..24b2d24e8 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -2313,6 +2313,34 @@ public function testInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column3']); } + public function testInsertDates(): void + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + 'column3' => 'foo', + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->addColumn('column3', 'string', ['null' => true, 'default' => null]) + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals('foo', $rows[0]['column3']); + $this->assertNull($rows[1]['column3']); + } + public function testDumpCreateTable() { $inputDefinition = new InputDefinition([new InputOption('dry-run')]); diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 7c686b235..46714f992 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -2558,6 +2558,34 @@ public function testInsertLiteral() $this->assertEquals('2025-01-01 00:00:00', $rows[2]['column3']); } + public function testInsertDates(): void + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + 'column3' => 'foo', + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->addColumn('column3', 'string', ['null' => true, 'default' => null]) + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertEquals($data[0]['created']->toDateTimeString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals('foo', $rows[0]['column3']); + $this->assertNull($rows[1]['column3']); + } + public function testInsertDataWithSchema() { $this->adapter->createSchema('schema1'); diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index cbec9d959..622583a8e 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -1927,6 +1927,34 @@ public function testBulkInsertDates(): void $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); } + public function testInsertDates(): void + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + 'column3' => 'foo', + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->addColumn('column3', 'string', ['null' => true, 'default' => null]) + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertEquals($data[0]['created']->toDateString(), $rows[0]['created']); + $this->assertEquals($data[1]['created']->toDateTimeString(), $rows[1]['created']); + $this->assertEquals('foo', $rows[0]['column3']); + $this->assertNull($rows[1]['column3']); + } + public function testNullWithoutDefaultValue() { $this->markTestSkipped('Skipping for now. See Github Issue #265.'); diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 943f218e5..e442e1aaa 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -1440,6 +1440,34 @@ public function testInsertLiteral() $this->assertEquals('2025-01-01 00:00:00.000', $rows[2]['column3']); } + public function testInsertDates(): void + { + $data = [ + [ + 'name' => 'foo', + 'created' => new Date(), + 'column3' => 'foo', + ], + [ + 'name' => 'bar', + 'created' => new DateTime(), + ], + ]; + $table = new Table('table1', [], $this->adapter); + $table->addColumn('name', 'string') + ->addColumn('created', 'datetime') + ->addColumn('column3', 'string', ['null' => true, 'default' => null]) + ->insert($data) + ->save(); + $rows = $this->adapter->fetchAll('SELECT * FROM table1'); + $this->assertEquals('foo', $rows[0]['name']); + $this->assertEquals('bar', $rows[1]['name']); + $this->assertEquals($data[0]['created']->format('Y-m-d H:i:s.000'), $rows[0]['created']); + $this->assertEquals($data[1]['created']->format('Y-m-d H:i:s.000'), $rows[1]['created']); + $this->assertEquals('foo', $rows[0]['column3']); + $this->assertNull($rows[1]['column3']); + } + public function testTruncateTable() { $table = new Table('table1', [], $this->adapter); From ea1467708457c13dae1798f2e0c2c9dc518ad7a1 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 17 Apr 2025 11:23:28 -0600 Subject: [PATCH 10/10] Rename variable Signed-off-by: Matthew Peveler --- src/Phinx/Db/Adapter/PdoAdapter.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 61ccbabcf..8c7f8e40d 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -329,22 +329,22 @@ public function fetchAll(string $sql): array */ protected function getInsertParameters(array $row): array { - $values = []; + $params = []; foreach ($row as $value) { if ($value instanceof Literal) { continue; } elseif ($value instanceof DateTime) { - $values[] = $value->toDateTimeString(); + $params[] = $value->toDateTimeString(); } elseif ($value instanceof Date) { - $values[] = $value->toDateString(); + $params[] = $value->toDateString(); } elseif (is_bool($value)) { - $values[] = $this->castToBool($value); + $params[] = $this->castToBool($value); } else { - $values[] = $value; + $params[] = $value; } } - return $values; + return $params; } /**