From eb229d564deb495ad8a9854e624df88ed2ee7035 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 8 Apr 2025 02:24:02 +0200 Subject: [PATCH 1/2] Fix up DateTime support. --- tests/TestCase/Command/Phinx/SeedTest.php | 3 ++ tests/TestCase/Command/SeedCommandTest.php | 57 ++++++++++++++++++++- tests/test_app/config/Seeds/NumbersSeed.php | 5 -- tests/test_app/config/Seeds/StoresSeed.php | 33 ++++++++++++ 4 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 tests/test_app/config/Seeds/StoresSeed.php diff --git a/tests/TestCase/Command/Phinx/SeedTest.php b/tests/TestCase/Command/Phinx/SeedTest.php index f30d3b83..384bd067 100644 --- a/tests/TestCase/Command/Phinx/SeedTest.php +++ b/tests/TestCase/Command/Phinx/SeedTest.php @@ -21,6 +21,7 @@ use Migrations\Test\CommandTester; use Migrations\Test\TestCase\DriverConnectionTrait; use PDO; +use Phinx\Config\FeatureFlags; use Phinx\Db\Adapter\WrapperInterface; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\StreamOutput; @@ -90,6 +91,8 @@ public function tearDown(): void $this->connection->execute('DROP TABLE IF EXISTS numbers'); $this->connection->execute('DROP TABLE IF EXISTS letters'); $this->connection->execute('DROP TABLE IF EXISTS stores'); + + FeatureFlags::$addTimestampsUseDateTime = false; } /** diff --git a/tests/TestCase/Command/SeedCommandTest.php b/tests/TestCase/Command/SeedCommandTest.php index 8bac092e..1c084135 100644 --- a/tests/TestCase/Command/SeedCommandTest.php +++ b/tests/TestCase/Command/SeedCommandTest.php @@ -11,6 +11,7 @@ use Cake\Event\EventManager; use Cake\TestSuite\TestCase; use InvalidArgumentException; +use Phinx\Config\FeatureFlags; use ReflectionProperty; class SeedCommandTest extends TestCase @@ -141,7 +142,7 @@ public function testSeederBaseSeed(): void $this->assertEquals(2, $query->fetchColumn(0)); } - public function testSeederImplictAll(): void + public function testSeederImplicitAll(): void { $this->createTables(); $this->exec('migrations seed -c test'); @@ -192,4 +193,58 @@ public function testSeederSourceNotFound(): void $this->exec('migrations seed -c test --source NotThere --seed LettersSeed'); } + + public function testSeederWithDateTimeFields(): void + { + FeatureFlags::$addTimestampsUseDateTime = true; + + $this->createTables(); + $this->exec('migrations seed -c test --seed StoresSeed'); + + $this->assertExitSuccess(); + $this->assertOutputContains('StoresSeed: seeding'); + $this->assertOutputContains('All Done'); + + /** @var \Cake\Database\Connection $connection */ + $connection = ConnectionManager::get('test'); + $result = $connection->selectQuery() + ->select(['*']) + ->from('stores') + ->orderBy('id DESC') + ->limit(1) + ->execute()->fetchAll('assoc'); + + $this->assertNotEmpty($result[0]); + $store = $result[0]; + $this->assertEquals('foo_with_date', $store['name']); + $this->assertNotEmpty($store['created']); + $this->assertNotEmpty($store['modified']); + } + + public function testSeederWithTimestampFields(): void + { + FeatureFlags::$addTimestampsUseDateTime = false; + + $this->createTables(); + $this->exec('migrations seed -c test --seed StoresSeed'); + + $this->assertExitSuccess(); + $this->assertOutputContains('StoresSeed: seeding'); + $this->assertOutputContains('All Done'); + + /** @var \Cake\Database\Connection $connection */ + $connection = ConnectionManager::get('test'); + $result = $connection->selectQuery() + ->select(['*']) + ->from('stores') + ->orderBy('id DESC') + ->limit(1) + ->execute()->fetchAll('assoc'); + + $this->assertNotEmpty($result[0]); + $store = $result[0]; + $this->assertEquals('foo_with_date', $store['name']); + $this->assertNotEmpty($store['created']); + $this->assertNotEmpty($store['modified']); + } } diff --git a/tests/test_app/config/Seeds/NumbersSeed.php b/tests/test_app/config/Seeds/NumbersSeed.php index 5fb9d229..faf17b58 100644 --- a/tests/test_app/config/Seeds/NumbersSeed.php +++ b/tests/test_app/config/Seeds/NumbersSeed.php @@ -9,11 +9,6 @@ class NumbersSeed extends AbstractSeed { /** * Run Method. - * - * Write your database seeder using this method. - * - * More information on writing seeders is available here: - * https://book.cakephp.org/phinx/0/en/seeding.html */ public function run(): void { diff --git a/tests/test_app/config/Seeds/StoresSeed.php b/tests/test_app/config/Seeds/StoresSeed.php new file mode 100644 index 00000000..7be59b1b --- /dev/null +++ b/tests/test_app/config/Seeds/StoresSeed.php @@ -0,0 +1,33 @@ + 'foo', + 'created' => new Date(), + 'modified' => new Date(), + ], + [ + 'name' => 'foo_with_date', + 'created' => new DateTime(), + 'modified' => new DateTime(), + ], + ]; + + $table = $this->table('stores'); + $table->insert($data)->save(); + } +} From 3879c9c54d8a5aeb5d67bc2dc97a28f892c33fd1 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 11 Apr 2025 18:33:08 +0200 Subject: [PATCH 2/2] Add fix for Date/DateTime seeding. --- src/Db/Adapter/AbstractAdapter.php | 8 ++++++- src/Db/Adapter/PostgresAdapter.php | 8 ++++++- src/Db/Adapter/SqlserverAdapter.php | 8 ++++++- tests/TestCase/Command/Phinx/SeedTest.php | 3 --- tests/TestCase/Command/SeedCommandTest.php | 26 ++++++++++++++++++---- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Db/Adapter/AbstractAdapter.php b/src/Db/Adapter/AbstractAdapter.php index a5ca2844..1b5ab8cd 100644 --- a/src/Db/Adapter/AbstractAdapter.php +++ b/src/Db/Adapter/AbstractAdapter.php @@ -17,6 +17,8 @@ use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; use Cake\Database\Schema\SchemaDialect; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use Exception; use InvalidArgumentException; use Migrations\Config\Config; @@ -723,7 +725,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void $placeholder = (string)$v; } if ($placeholder == '?') { - if (is_bool($v)) { + if ($v instanceof DateTime) { + $vals[] = $v->toDateTimeString(); + } elseif ($v instanceof Date) { + $vals[] = $v->toDateString(); + } elseif (is_bool($v)) { $vals[] = $this->castToBool($v); } else { $vals[] = $v; diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index e642a0a6..94039a33 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -9,6 +9,8 @@ namespace Migrations\Db\Adapter; use Cake\Database\Connection; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Literal; @@ -1526,7 +1528,11 @@ public function bulkinsert(Table $table, array $rows): void } $values[] = $placeholder; if ($placeholder == '?') { - if (is_bool($v)) { + if ($v instanceof DateTime) { + $vals[] = $v->toDateTimeString(); + } elseif ($v instanceof Date) { + $vals[] = $v->toDateString(); + } elseif (is_bool($v)) { $vals[] = $this->castToBool($v); } else { $vals[] = $v; diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index 0053a3bf..a7acc93c 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -9,6 +9,8 @@ namespace Migrations\Db\Adapter; use BadMethodCallException; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Literal; @@ -1361,7 +1363,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void $placeholder = (string)$v; } if ($placeholder == '?') { - if (is_bool($v)) { + if ($v instanceof DateTime) { + $vals[] = $v->toDateTimeString(); + } elseif ($v instanceof Date) { + $vals[] = $v->toDateString(); + } elseif (is_bool($v)) { $vals[] = $this->castToBool($v); } else { $vals[] = $v; diff --git a/tests/TestCase/Command/Phinx/SeedTest.php b/tests/TestCase/Command/Phinx/SeedTest.php index 384bd067..f30d3b83 100644 --- a/tests/TestCase/Command/Phinx/SeedTest.php +++ b/tests/TestCase/Command/Phinx/SeedTest.php @@ -21,7 +21,6 @@ use Migrations\Test\CommandTester; use Migrations\Test\TestCase\DriverConnectionTrait; use PDO; -use Phinx\Config\FeatureFlags; use Phinx\Db\Adapter\WrapperInterface; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\StreamOutput; @@ -91,8 +90,6 @@ public function tearDown(): void $this->connection->execute('DROP TABLE IF EXISTS numbers'); $this->connection->execute('DROP TABLE IF EXISTS letters'); $this->connection->execute('DROP TABLE IF EXISTS stores'); - - FeatureFlags::$addTimestampsUseDateTime = false; } /** diff --git a/tests/TestCase/Command/SeedCommandTest.php b/tests/TestCase/Command/SeedCommandTest.php index 1c084135..af966e1d 100644 --- a/tests/TestCase/Command/SeedCommandTest.php +++ b/tests/TestCase/Command/SeedCommandTest.php @@ -12,6 +12,7 @@ use Cake\TestSuite\TestCase; use InvalidArgumentException; use Phinx\Config\FeatureFlags; +use ReflectionClass; use ReflectionProperty; class SeedCommandTest extends TestCase @@ -39,6 +40,13 @@ public function tearDown(): void $connection->execute('DROP TABLE IF EXISTS numbers'); $connection->execute('DROP TABLE IF EXISTS letters'); $connection->execute('DROP TABLE IF EXISTS stores'); + + if (class_exists(FeatureFlags::class)) { + $reflection = new ReflectionClass(FeatureFlags::class); + if ($reflection->hasProperty('addTimestampsUseDateTime')) { + FeatureFlags::$addTimestampsUseDateTime = false; + } + } } protected function resetOutput(): void @@ -194,9 +202,14 @@ public function testSeederSourceNotFound(): void $this->exec('migrations seed -c test --source NotThere --seed LettersSeed'); } - public function testSeederWithDateTimeFields(): void + public function testSeederWithTimestampFields(): void { - FeatureFlags::$addTimestampsUseDateTime = true; + if (class_exists(FeatureFlags::class)) { + $reflection = new ReflectionClass(FeatureFlags::class); + if ($reflection->hasProperty('addTimestampsUseDateTime')) { + FeatureFlags::$addTimestampsUseDateTime = false; + } + } $this->createTables(); $this->exec('migrations seed -c test --seed StoresSeed'); @@ -221,9 +234,14 @@ public function testSeederWithDateTimeFields(): void $this->assertNotEmpty($store['modified']); } - public function testSeederWithTimestampFields(): void + public function testSeederWithDateTimeFields(): void { - FeatureFlags::$addTimestampsUseDateTime = false; + $this->skipIf(!class_exists(FeatureFlags::class)); + + $reflection = new ReflectionClass(FeatureFlags::class); + $this->skipIf(!$reflection->hasProperty('addTimestampsUseDateTime')); + + FeatureFlags::$addTimestampsUseDateTime = true; $this->createTables(); $this->exec('migrations seed -c test --seed StoresSeed');