Skip to content

fix(postgres): handle timestamptz correctly - #11916

Closed
Minishlink wants to merge 3 commits into
typeorm:masterfrom
Minishlink:fix/timestamptz-parsing
Closed

Minishlink wants to merge 3 commits into
typeorm:masterfrom
Minishlink:fix/timestamptz-parsing

Conversation

@Minishlink

@Minishlink Minishlink commented Jan 12, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up of this PR #11774

I won't handle tests this time since there is a lot of confusion around them about this subject. I'll let maintainers merge this as-is or update the relevant tests

This fixes getting Date objects from cached entities instead of strings for columns marked as timestamptz (like it was the case for timestamp with time zone).

@Minishlink
Minishlink marked this pull request as ready for review January 12, 2026 11:00
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incomplete Coverage

The PR adds timestamptz handling in two locations but may be missing other places where timestamp types are checked. Search the codebase for other conditional checks involving timestamp with time zone or timestamp without time zone to ensure timestamptz is handled consistently throughout the driver.

columnMetadata.type === "timestamptz" ||
columnMetadata.type === "timestamp with time zone" ||

@Minishlink Minishlink changed the title fix/timestamptz parsing fix(postgres): treat 'timestamptz' same as 'timestamp with time zone' Jan 12, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 12, 2026 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to e23ab52

CategorySuggestion                                                                                                                                    Impact
Possible issue
Preserve timezone for timestamptz columns

To prevent potential data loss, handle timestamptz and timestamp with time zone
types separately to ensure timezone information is preserved when persisting
values.

src/driver/postgres/PostgresDriver.ts [744-751]

 if (
     columnMetadata.type === "datetime" ||
     columnMetadata.type === Date ||
     columnMetadata.type === "timestamp" ||
-    columnMetadata.type === "timestamptz" ||
-    columnMetadata.type === "timestamp with time zone" ||
     columnMetadata.type === "timestamp without time zone"
 ) {
     return DateUtils.mixedDateToDate(value)
+} else if (
+    columnMetadata.type === "timestamptz" ||
+    columnMetadata.type === "timestamp with time zone"
+) {
+    return DateUtils.mixedDateToDate(value, { keepTimezone: true })

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies a potential data integrity issue where timezone information for timestamptz columns might be lost, as the PR treats timezone-aware and timezone-unaware types identically.

Medium
  • More

Previous suggestions

Suggestions up to commit e540aba
CategorySuggestion                                                                                                                                    Impact
General
Preserve timezone information for timestamptz

For timestamptz and timestamp with time zone types, modify the
DateUtils.mixedDateToDate call to include an option that preserves timezone
information, ensuring correct data persistence.

src/driver/postgres/PostgresDriver.ts [743-751]

 } else if (
     columnMetadata.type === "datetime" ||
     columnMetadata.type === Date ||
     columnMetadata.type === "timestamp" ||
     columnMetadata.type === "timestamptz" ||
     columnMetadata.type === "timestamp with time zone" ||
     columnMetadata.type === "timestamp without time zone"
 ) {
-    return DateUtils.mixedDateToDate(value)
+    return DateUtils.mixedDateToDate(value, {
+        utc: columnMetadata.type === "timestamptz" || columnMetadata.type === "timestamp with time zone"
+    })
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies a potential data handling bug where treating the timezone-aware timestamptz type identically to non-timezone-aware types could lead to loss of timezone information.

Medium
Suggestions up to commit e4c10f6
CategorySuggestion                                                                                                                                    Impact
General
Normalize timestamptz type alias centrally

Centralize the handling of the timestamptz type alias by normalizing it to
timestamp with time zone in a single normalizeType method, rather than adding
redundant checks in multiple places.

src/driver/postgres/PostgresDriver.ts [744-749]

 columnMetadata.type === "datetime" ||
 columnMetadata.type === Date ||
 columnMetadata.type === "timestamp" ||
-columnMetadata.type === "timestamptz" ||
 columnMetadata.type === "timestamp with time zone" ||
 columnMetadata.type === "timestamp without time zone"
Suggestion importance[1-10]: 7

__

Why: The suggestion provides a good refactoring opportunity by proposing to centralize type alias handling, which would reduce code duplication and improve maintainability, instead of adding the same check in multiple methods.

Medium

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 12, 2026 •

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

(Review updated until commit e540aba)

🐞 Bugs (0) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider

Great, No Issues Found!

Qodo reviewed your code and found no material issues that require review

Grey Divider


Previous review results

Results up to commit e4c10f6


🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider
Action Required
1. Missing timetz type handling 🐞 Bug
Description
• The PR adds timestamptz checks to handle the PostgreSQL alias for 'timestamp with time zone',
  but fails to add the parallel timetz checks for 'time with time zone'
• Both timestamptz and timetz are valid PostgreSQL type aliases that users can specify in column
  definitions (e.g., @Column({ type: 'timetz' }))
• When a column is defined with type: 'timetz', the preparePersistentValue and
  prepareHydratedValue methods will not properly convert time values, causing incorrect data
  persistence and retrieval
• This creates an inconsistency where timestamptz is now fully supported but timetz remains
  partially supported (works in schema but not in data conversion)

correctness

Code

src/driver/postgres/PostgresDriver.ts[747]

+            columnMetadata.type === "timestamptz" ||
Evidence
The evidence demonstrates that: (1) timetz is a valid, supported PostgreSQL type alias listed in
supportedDataTypes and handled by normalizeType, (2) the preparePersistentValue method only checks
for 'time' without checking for 'timetz' or 'time with time zone', (3) the prepareHydratedValue
method has the same gap, (4) columnMetadata.type contains the original type string as specified by
the user, not the normalized form, (5) test files show real-world usage of timestamptz, confirming
users do use these aliases, (6) the PR adds timestamptz but not timetz, creating an inconsistency.

src/driver/postgres/PostgresDriver.ts[151-151]
src/driver/postgres/PostgresDriver.ts[1104-1105]
src/driver/postgres/PostgresDriver.ts[741-742]
src/driver/postgres/PostgresDriver.ts[844-845]
src/metadata/ColumnMetadata.ts[376-376]
src/driver/cockroachdb/CockroachDriver.ts[391-392]

Agent Prompt
## Issue Description
The PR adds support for the `timestamptz` PostgreSQL type alias but fails to add the parallel support for `timetz` (time with time zone alias). Both aliases should be handled consistently in the type conversion methods.

## Issue Context
- PostgreSQL supports type aliases: `timestamptz` for 'timestamp with time zone' and `timetz` for 'time with time zone'
- The driver already recognizes `timetz` in supportedDataTypes and normalizeType method
- Users can define columns with `@Column({ type: 'timetz' })` but these columns won't be properly converted during persistence/hydration
- The PR correctly adds `timestamptz` checks but misses the parallel `timetz` checks

## Fix Focus Areas

### 1. preparePersistentValue method - Add timetz handling
- src/driver/postgres/PostgresDriver.ts[741-751]

Modify the time type check to include 'timetz' and 'time with time zone':
```typescript
} else if (
   columnMetadata.type === "time" ||
   columnMetadata.type === "timetz" ||
   columnMetadata.type === "time with time zone" ||
   columnMetadata.type === "time without time zone"
) {
   return DateUtils.mixedDateToTimeString(value)
}
```

### 2. prepareHydratedValue method - Add timetz handling
- src/driver/postgres/PostgresDriver.ts[844-845]

Modify the time type check to include 'timetz' and 'time with time zone':
```typescript
} else if (
   columnMetadata.type === "time" ||
   columnMetadata.type === "timetz" ||
   columnMetadata.type === "time with time zone" ||
   columnMetadata.type === "time without time zone"
) {
   value = DateUtils.mixedTimeToString(value)
}
```

### 3. Consider fixing CockroachDB driver as well
- src/driver/cockroachdb/CockroachDriver.ts[391-392]
- src/driver/cockroachdb/CockroachDriver.ts[453-454]

The CockroachDB driver has the same issue and should be fixed consistently.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@gioboa gioboa changed the title fix(postgres): treat 'timestamptz' same as 'timestamp with time zone' fix(postgres): handle timestamptz correctly Jan 14, 2026
@gioboa gioboa changed the title fix(postgres): handle timestamptz correctly fix(postgres): handle timestamptz correctly Jan 14, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 14, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@11916
    

commit: e23ab52

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit e540aba

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests on this PR please?

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 80.881% (-0.001%) from 80.882%
when pulling e540aba on Minishlink:fix/timestamptz-parsing
into 13ab7c3 on typeorm:master.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Missing tests for timestamptz fix 📘 Rule violation ✓ Correctness
Description
• The PR fixes timestamptz handling for cached entities but does not add or update tests in
  test/functional.
• The PR description explicitly states 'I won't handle tests this time' which violates the
  compliance requirement.
• Existing tests in test/functional/database-schema/column-types/postgres already test timestamptz
  but don't cover the caching scenario that this fix addresses.
Code

src/driver/postgres/PostgresDriver.ts[747]

+            columnMetadata.type === "timestamptz" ||
Evidence
The compliance checklist requires that issue fixes add or update tests in test/functional with an
issue reference. The PR description explicitly states the author won't handle tests, and no test
files are included in the diff. The code changes in preparePersistentValue and prepareHydratedValue
functions add timestamptz handling, but there are no corresponding test updates to verify this fix
works correctly, especially for the cached entity scenario mentioned in the PR description.

Rule 3: Prefer functional tests over per-issue tests
src/driver/postgres/PostgresDriver.ts[747-747]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The PR adds `timestamptz` handling to preparePersistentValue and prepareHydratedValue functions but lacks tests to verify the fix works correctly, especially for cached entities.

## Issue Context
The PR description mentions this fixes getting Date objects from cached entities instead of strings for columns marked as `timestamptz`. The compliance checklist requires functional tests for issue fixes.

## Fix Focus Areas
- test/functional/database-schema/column-types/postgres/column-types-postgres.test.ts[1-400]
- test/functional/query-builder/cache/query-builder-cache.test.ts[1-100]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



ⓘ The new review experience is currently in Beta. Learn more

Qodo Logo

@alumni alumni self-assigned this Jan 25, 2026
@alumni

alumni commented Jan 30, 2026

Copy link
Copy Markdown
Collaborator

We'll merge #11774 since it has tests

@alumni alumni closed this Jan 30, 2026
@Minishlink
Minishlink deleted the fix/timestamptz-parsing branch January 30, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants