Skip to content

Commit 0fcd2e2

Browse files
authored
Fix error running migrations with type timestamptimezone (#979)
Add test capturing the generation of snapshots using `timestamptimezone` This is a cakephp/database abstract type, which is fine with the built-in backend. * Fix running migrations with timestamptimezone type. We generate migrations with this type which is a cakephp/database type. It should be allowed in platforms that support it. * Make behavior generic as all backends support this type.
1 parent e6da2fe commit 0fcd2e2

File tree

5 files changed

+476
-0
lines changed

5 files changed

+476
-0
lines changed

src/Db/Adapter/AbstractAdapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,10 @@ public function getColumnTypes(): array
10071007
'decimal',
10081008
'double',
10091009
'datetime',
1010+
'datetimefractional',
10101011
'timestamp',
1012+
'timestampfractional',
1013+
'timestamptimezone',
10111014
'time',
10121015
'date',
10131016
'blob',

tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,35 @@ public function testSnapshotGenerateOnly()
139139
$this->assertFalse(file_exists($this->migrationPath . 'schema-dump-test.lock'), 'Lock file should not be created with --generate-only');
140140
}
141141

142+
public function testSnapshotPostgresTimestampTzColumn(): void
143+
{
144+
$this->skipIf(env('DB') !== 'pgsql');
145+
146+
/** @var \Cake\Database\Connection $connection */
147+
$connection = ConnectionManager::get('test');
148+
$connection->execute(
149+
'CREATE TABLE IF NOT EXISTS postgres_timestamp_tz (id SERIAL PRIMARY KEY, created TIMESTAMPTZ NOT NULL)',
150+
);
151+
152+
$scenario = 'PostgresTimestampTz';
153+
154+
$bakeName = $this->getBakeName("TestSnapshot{$scenario}");
155+
$this->exec("bake migration_snapshot {$bakeName} -c test");
156+
157+
$connection->execute('DROP TABLE postgres_timestamp_tz');
158+
159+
$generatedMigration = glob($this->migrationPath . "*_TestSnapshot{$scenario}*.php");
160+
$this->generatedFiles = $generatedMigration;
161+
$this->generatedFiles[] = $this->migrationPath . 'schema-dump-test.lock';
162+
163+
$generatedMigration = basename($generatedMigration[0]);
164+
$fileName = pathinfo($generatedMigration, PATHINFO_FILENAME);
165+
$this->assertOutputContains('Marking the migration ' . $fileName . ' as migrated...');
166+
$this->assertOutputContains('Creating a dump of the new database state...');
167+
$this->assertNotEmpty($this->generatedFiles);
168+
$this->assertCorrectSnapshot($bakeName, file_get_contents($this->generatedFiles[0]));
169+
}
170+
142171
/**
143172
* Test baking a snapshot with the phinx auto-id feature disabled
144173
*

tests/TestCase/Migration/ManagerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,23 @@ public function testPostgresFullMigration(): void
28292829
$this->assertFalse($adapter->hasTable('users'));
28302830
}
28312831

2832+
public function testPostgresTimestamptimezone(): void
2833+
{
2834+
if ($this->getDriverType() !== 'postgres') {
2835+
$this->markTestSkipped('Test requires postgres');
2836+
}
2837+
$adapter = $this->prepareEnvironment([
2838+
'migrations' => ROOT . '/config/PostgresTimestamptimezone',
2839+
]);
2840+
$adapter->connect();
2841+
// migrate to the latest version
2842+
$this->manager->migrate();
2843+
2844+
$this->assertTrue($adapter->hasTable('timestamp_articles'));
2845+
2846+
$this->manager->rollback('all');
2847+
}
2848+
28322849
public function testMigrationWithDropColumnAndForeignKeyAndIndex(): void
28332850
{
28342851
if ($this->getDriverType() !== 'mysql') {

0 commit comments

Comments
 (0)