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/SeedCommandTest.php b/tests/TestCase/Command/SeedCommandTest.php index 8bac092e..af966e1d 100644 --- a/tests/TestCase/Command/SeedCommandTest.php +++ b/tests/TestCase/Command/SeedCommandTest.php @@ -11,6 +11,8 @@ use Cake\Event\EventManager; use Cake\TestSuite\TestCase; use InvalidArgumentException; +use Phinx\Config\FeatureFlags; +use ReflectionClass; use ReflectionProperty; class SeedCommandTest extends TestCase @@ -38,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 @@ -141,7 +150,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 +201,68 @@ public function testSeederSourceNotFound(): void $this->exec('migrations seed -c test --source NotThere --seed LettersSeed'); } + + public function testSeederWithTimestampFields(): void + { + 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'); + + $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 testSeederWithDateTimeFields(): void + { + $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'); + + $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(); + } +}