oguimbal / pg-mem

An in memory postgres DB instance for your unit tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot cast type timestamptz to timestamp

luixo opened this issue Β· comments

Describe the bug

The casting of timestamps doesn't work

    cannot cast type timestamptz to timestamp

    🐜 This seems to be an execution error, which means that your request syntax seems okay,
        but the resulting statement cannot be executed β†’ Probably not a pg-mem error.

    *️⃣ Failed SQL statement: /* doesn't matter */ where sessions.timestamp > now();

    πŸ‘‰ You can file an issue at https://github.com/oguimbal/pg-mem along with a way to reproduce this error (if you can), and  the stacktrace:

To Reproduce

CREATE TABLE "user" ("id" SERIAL NOT NULL, "timestamp" timestamp NOT NULL, PRIMARY KEY ("id"));
INSERT INTO "user"("timestamp") VALUES (now()) RETURNING "id";
SELECT FROM "user" WHERE "timestamp" > now()

pg-mem version

2.6.13

Patch to fix (probably, a bad one)

Worked for me, but should be thoroughly tested with different type combinations.

diff --git a/node_modules/pg-mem/index.js b/node_modules/pg-mem/index.js
index 2c95ad5..82cfd53 100644
--- a/node_modules/pg-mem/index.js
+++ b/node_modules/pg-mem/index.js
@@ -7440,6 +7440,7 @@ class TimestampType extends datatype_base_1.TypeBase {
         switch (to.primary) {
             case interfaces_private_1.DataType.timestamp:
                 return this.primary === interfaces_private_1.DataType.timestamp
+                    || this.primary === interfaces_private_1.DataType.timestamptz
                     || this.primary === interfaces_private_1.DataType.date;
             case interfaces_private_1.DataType.timestamptz:
                 return this.primary !== interfaces_private_1.DataType.time;
@@ -7515,6 +7516,23 @@ class TimestampType extends datatype_base_1.TypeBase {
     doLt(a, b) {
         return (0, moment_1.default)(a).diff((0, moment_1.default)(b)) < 0;
     }
+    doPrefer(type) {
+        const preciseTypes = [
+            interfaces_private_1.DataType.timestamp,
+            interfaces_private_1.DataType.timestamptz,
+            interfaces_private_1.DataType.date
+        ]
+        if (preciseTypes.includes(type.primary)) {
+            return type;
+        }
+        if (preciseTypes.includes(this.primary)) {
+            return this;
+        }
+        if (type.primary === interfaces_private_1.DataType.time) {
+            return type;
+        }
+        return this;
+    }
 }
 exports.TimestampType = TimestampType;
 
commented

Seconded. Any attention for this? It's a quick fix. @oguimbal