Skip to content

Commit cd1f6ca

Browse files
authored
Fix up DateTime support for seed bulkinsert(). (#838)
* Fix up DateTime support. * Add fix for Date/DateTime seeding.
1 parent 486b6cf commit cd1f6ca

File tree

6 files changed

+128
-9
lines changed

6 files changed

+128
-9
lines changed

src/Db/Adapter/AbstractAdapter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Cake\Database\Query\SelectQuery;
1818
use Cake\Database\Query\UpdateQuery;
1919
use Cake\Database\Schema\SchemaDialect;
20+
use Cake\I18n\Date;
21+
use Cake\I18n\DateTime;
2022
use Exception;
2123
use InvalidArgumentException;
2224
use Migrations\Config\Config;
@@ -723,7 +725,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void
723725
$placeholder = (string)$v;
724726
}
725727
if ($placeholder == '?') {
726-
if (is_bool($v)) {
728+
if ($v instanceof DateTime) {
729+
$vals[] = $v->toDateTimeString();
730+
} elseif ($v instanceof Date) {
731+
$vals[] = $v->toDateString();
732+
} elseif (is_bool($v)) {
727733
$vals[] = $this->castToBool($v);
728734
} else {
729735
$vals[] = $v;

src/Db/Adapter/PostgresAdapter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Migrations\Db\Adapter;
1010

1111
use Cake\Database\Connection;
12+
use Cake\I18n\Date;
13+
use Cake\I18n\DateTime;
1214
use InvalidArgumentException;
1315
use Migrations\Db\AlterInstructions;
1416
use Migrations\Db\Literal;
@@ -1526,7 +1528,11 @@ public function bulkinsert(Table $table, array $rows): void
15261528
}
15271529
$values[] = $placeholder;
15281530
if ($placeholder == '?') {
1529-
if (is_bool($v)) {
1531+
if ($v instanceof DateTime) {
1532+
$vals[] = $v->toDateTimeString();
1533+
} elseif ($v instanceof Date) {
1534+
$vals[] = $v->toDateString();
1535+
} elseif (is_bool($v)) {
15301536
$vals[] = $this->castToBool($v);
15311537
} else {
15321538
$vals[] = $v;

src/Db/Adapter/SqlserverAdapter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Migrations\Db\Adapter;
1010

1111
use BadMethodCallException;
12+
use Cake\I18n\Date;
13+
use Cake\I18n\DateTime;
1214
use InvalidArgumentException;
1315
use Migrations\Db\AlterInstructions;
1416
use Migrations\Db\Literal;
@@ -1361,7 +1363,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void
13611363
$placeholder = (string)$v;
13621364
}
13631365
if ($placeholder == '?') {
1364-
if (is_bool($v)) {
1366+
if ($v instanceof DateTime) {
1367+
$vals[] = $v->toDateTimeString();
1368+
} elseif ($v instanceof Date) {
1369+
$vals[] = $v->toDateString();
1370+
} elseif (is_bool($v)) {
13651371
$vals[] = $this->castToBool($v);
13661372
} else {
13671373
$vals[] = $v;

tests/TestCase/Command/SeedCommandTest.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Cake\Event\EventManager;
1212
use Cake\TestSuite\TestCase;
1313
use InvalidArgumentException;
14+
use Phinx\Config\FeatureFlags;
15+
use ReflectionClass;
1416
use ReflectionProperty;
1517

1618
class SeedCommandTest extends TestCase
@@ -38,6 +40,13 @@ public function tearDown(): void
3840
$connection->execute('DROP TABLE IF EXISTS numbers');
3941
$connection->execute('DROP TABLE IF EXISTS letters');
4042
$connection->execute('DROP TABLE IF EXISTS stores');
43+
44+
if (class_exists(FeatureFlags::class)) {
45+
$reflection = new ReflectionClass(FeatureFlags::class);
46+
if ($reflection->hasProperty('addTimestampsUseDateTime')) {
47+
FeatureFlags::$addTimestampsUseDateTime = false;
48+
}
49+
}
4150
}
4251

4352
protected function resetOutput(): void
@@ -141,7 +150,7 @@ public function testSeederBaseSeed(): void
141150
$this->assertEquals(2, $query->fetchColumn(0));
142151
}
143152

144-
public function testSeederImplictAll(): void
153+
public function testSeederImplicitAll(): void
145154
{
146155
$this->createTables();
147156
$this->exec('migrations seed -c test');
@@ -192,4 +201,68 @@ public function testSeederSourceNotFound(): void
192201

193202
$this->exec('migrations seed -c test --source NotThere --seed LettersSeed');
194203
}
204+
205+
public function testSeederWithTimestampFields(): void
206+
{
207+
if (class_exists(FeatureFlags::class)) {
208+
$reflection = new ReflectionClass(FeatureFlags::class);
209+
if ($reflection->hasProperty('addTimestampsUseDateTime')) {
210+
FeatureFlags::$addTimestampsUseDateTime = false;
211+
}
212+
}
213+
214+
$this->createTables();
215+
$this->exec('migrations seed -c test --seed StoresSeed');
216+
217+
$this->assertExitSuccess();
218+
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
219+
$this->assertOutputContains('All Done');
220+
221+
/** @var \Cake\Database\Connection $connection */
222+
$connection = ConnectionManager::get('test');
223+
$result = $connection->selectQuery()
224+
->select(['*'])
225+
->from('stores')
226+
->orderBy('id DESC')
227+
->limit(1)
228+
->execute()->fetchAll('assoc');
229+
230+
$this->assertNotEmpty($result[0]);
231+
$store = $result[0];
232+
$this->assertEquals('foo_with_date', $store['name']);
233+
$this->assertNotEmpty($store['created']);
234+
$this->assertNotEmpty($store['modified']);
235+
}
236+
237+
public function testSeederWithDateTimeFields(): void
238+
{
239+
$this->skipIf(!class_exists(FeatureFlags::class));
240+
241+
$reflection = new ReflectionClass(FeatureFlags::class);
242+
$this->skipIf(!$reflection->hasProperty('addTimestampsUseDateTime'));
243+
244+
FeatureFlags::$addTimestampsUseDateTime = true;
245+
246+
$this->createTables();
247+
$this->exec('migrations seed -c test --seed StoresSeed');
248+
249+
$this->assertExitSuccess();
250+
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
251+
$this->assertOutputContains('All Done');
252+
253+
/** @var \Cake\Database\Connection $connection */
254+
$connection = ConnectionManager::get('test');
255+
$result = $connection->selectQuery()
256+
->select(['*'])
257+
->from('stores')
258+
->orderBy('id DESC')
259+
->limit(1)
260+
->execute()->fetchAll('assoc');
261+
262+
$this->assertNotEmpty($result[0]);
263+
$store = $result[0];
264+
$this->assertEquals('foo_with_date', $store['name']);
265+
$this->assertNotEmpty($store['created']);
266+
$this->assertNotEmpty($store['modified']);
267+
}
195268
}

tests/test_app/config/Seeds/NumbersSeed.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ class NumbersSeed extends AbstractSeed
99
{
1010
/**
1111
* Run Method.
12-
*
13-
* Write your database seeder using this method.
14-
*
15-
* More information on writing seeders is available here:
16-
* https://book.cakephp.org/phinx/0/en/seeding.html
1712
*/
1813
public function run(): void
1914
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Cake\I18n\Date;
4+
use Cake\I18n\DateTime;
5+
use Migrations\AbstractSeed;
6+
7+
/**
8+
* NumbersSeed seed.
9+
*/
10+
class StoresSeed extends AbstractSeed
11+
{
12+
/**
13+
* Run Method.
14+
*/
15+
public function run(): void
16+
{
17+
$data = [
18+
[
19+
'name' => 'foo',
20+
'created' => new Date(),
21+
'modified' => new Date(),
22+
],
23+
[
24+
'name' => 'foo_with_date',
25+
'created' => new DateTime(),
26+
'modified' => new DateTime(),
27+
],
28+
];
29+
30+
$table = $this->table('stores');
31+
$table->insert($data)->save();
32+
}
33+
}

0 commit comments

Comments
 (0)