-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(postgres): parse timestamp dates less than year 1000 (#5071)
### Bug Parsing a timestamp column in postgresql with a date less than year 1000 will create an invalid Date when using the `forceUtcTimezone` set to `true`. ### Reproduction Created test for reproduction, run without the fix to `PostgresSqlConnection.ts`. ### Fix Use the date parsing function from `postgres-date` that is used by default in `pg` for parsing dates but add the `Z` to force the utc timezone. `new Date('0022-01-01 09:17:58.000Z')` when parsing the timestamp column str creates an invalid date in js. --------- Co-authored-by: Martin Adámek <banan23@gmail.com>
- Loading branch information
1 parent
6667656
commit 63eb5c5
Showing
4 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/features/schema-generator/timestamp.postgres.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
|
||
@Entity() | ||
class TimestampTest { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ columnType: 'timestamp' }) | ||
createdAtTimestamp!: Date; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [TimestampTest], | ||
dbName: '5071', | ||
forceUtcTimezone: true, | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
afterAll(async () => await orm.close(true)); | ||
|
||
test('postgres timestamp is correctly parsed', async () => { | ||
const createdAt = new Date('0022-01-01T00:00:00Z'); | ||
const something = orm.em.create(TimestampTest, { id: 1, createdAtTimestamp: createdAt }); | ||
await orm.em.persistAndFlush(something); | ||
|
||
const res = await orm.em.fork().find(TimestampTest, something.id); | ||
|
||
expect(isNaN(res[0]!.createdAtTimestamp.getTime())).toBe(false); | ||
expect(res[0]!.createdAtTimestamp.getTime()).toEqual(createdAt.getTime()); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.