Skip to content

Commit 3879c9c

Browse files
committed
Add fix for Date/DateTime seeding.
1 parent eb229d5 commit 3879c9c

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
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/Phinx/SeedTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Migrations\Test\CommandTester;
2222
use Migrations\Test\TestCase\DriverConnectionTrait;
2323
use PDO;
24-
use Phinx\Config\FeatureFlags;
2524
use Phinx\Db\Adapter\WrapperInterface;
2625
use Symfony\Component\Console\Input\ArrayInput;
2726
use Symfony\Component\Console\Output\StreamOutput;
@@ -91,8 +90,6 @@ public function tearDown(): void
9190
$this->connection->execute('DROP TABLE IF EXISTS numbers');
9291
$this->connection->execute('DROP TABLE IF EXISTS letters');
9392
$this->connection->execute('DROP TABLE IF EXISTS stores');
94-
95-
FeatureFlags::$addTimestampsUseDateTime = false;
9693
}
9794

9895
/**

tests/TestCase/Command/SeedCommandTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cake\TestSuite\TestCase;
1313
use InvalidArgumentException;
1414
use Phinx\Config\FeatureFlags;
15+
use ReflectionClass;
1516
use ReflectionProperty;
1617

1718
class SeedCommandTest extends TestCase
@@ -39,6 +40,13 @@ public function tearDown(): void
3940
$connection->execute('DROP TABLE IF EXISTS numbers');
4041
$connection->execute('DROP TABLE IF EXISTS letters');
4142
$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+
}
4250
}
4351

4452
protected function resetOutput(): void
@@ -194,9 +202,14 @@ public function testSeederSourceNotFound(): void
194202
$this->exec('migrations seed -c test --source NotThere --seed LettersSeed');
195203
}
196204

197-
public function testSeederWithDateTimeFields(): void
205+
public function testSeederWithTimestampFields(): void
198206
{
199-
FeatureFlags::$addTimestampsUseDateTime = true;
207+
if (class_exists(FeatureFlags::class)) {
208+
$reflection = new ReflectionClass(FeatureFlags::class);
209+
if ($reflection->hasProperty('addTimestampsUseDateTime')) {
210+
FeatureFlags::$addTimestampsUseDateTime = false;
211+
}
212+
}
200213

201214
$this->createTables();
202215
$this->exec('migrations seed -c test --seed StoresSeed');
@@ -221,9 +234,14 @@ public function testSeederWithDateTimeFields(): void
221234
$this->assertNotEmpty($store['modified']);
222235
}
223236

224-
public function testSeederWithTimestampFields(): void
237+
public function testSeederWithDateTimeFields(): void
225238
{
226-
FeatureFlags::$addTimestampsUseDateTime = false;
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;
227245

228246
$this->createTables();
229247
$this->exec('migrations seed -c test --seed StoresSeed');

0 commit comments

Comments
 (0)