Repository: knex/knex
Pull request #4786 (2021-10-30 20:49:36)

Zero precision in timestamp/datetime #4784
- Fixes https://github.com/knex/knex/issues/4784
- Remove duplicate (actually timestamp == datetime implementation in PG) and so correct datetime precision = 0 too.

kibertoad: @OlivierCavadenti If you think we need to release it as a hotfix, we should also backport it to 0.95 branch.
OlivierCavadenti: > @OlivierCavadenti If you think we need to release it as a hotfix, we should also backport it to 0.95 branch.

yes we can. do you have any conventation (naming, ...) to make the backport ?
kibertoad: @OlivierCavadenti I don't think we have one. Also feel free to merge approved PRs at your convenience!

Patch:
diff --git a/lib/dialects/postgres/schema/pg-columncompiler.js b/lib/dialects/postgres/schema/pg-columncompiler.js
index cc49f883..5fb7fa25 100644
--- a/lib/dialects/postgres/schema/pg-columncompiler.js
+++ b/lib/dialects/postgres/schema/pg-columncompiler.js
@@ -72,22 +72,16 @@ class ColumnCompiler_PG extends ColumnCompiler {
       useTz = !withoutTz;
     }
     useTz = typeof useTz === 'boolean' ? useTz : true;
-    precision = precision ? '(' + precision + ')' : '';
+    precision =
+      precision !== undefined && precision !== null
+        ? '(' + precision + ')'
+        : '';
 
     return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`;
   }
 
   timestamp(withoutTz = false, precision) {
-    let useTz;
-    if (isObject(withoutTz)) {
-      ({ useTz, precision } = withoutTz);
-    } else {
-      useTz = !withoutTz;
-    }
-    useTz = typeof useTz === 'boolean' ? useTz : true;
-    precision = precision ? '(' + precision + ')' : '';
-
-    return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`;
+    return this.datetime(withoutTz, precision);
   }
 
   // Modifiers:


Test patch:
diff --git a/test/unit/schema-builder/postgres.js b/test/unit/schema-builder/postgres.js
index 48ca1996..2caee2ac 100644
--- a/test/unit/schema-builder/postgres.js
+++ b/test/unit/schema-builder/postgres.js
@@ -1528,6 +1528,19 @@ describe('PostgreSQL SchemaBuilder', function () {
     );
   });
 
+  it('adding timestamp with options object but precision 0 - #4784', () => {
+    tableSql = client
+      .schemaBuilder()
+      .table('users', (table) => {
+        table.timestamp('foo', { useTz: false, precision: 0 });
+      })
+      .toSQL();
+    equal(1, tableSql.length);
+    expect(tableSql[0].sql).to.equal(
+      'alter table "users" add column "foo" timestamp(0)'
+    );
+  });
+
   it('adding datetime with options object', () => {
     tableSql = client
       .schemaBuilder()
@@ -1567,6 +1580,19 @@ describe('PostgreSQL SchemaBuilder', function () {
     );
   });
 
+  it('adding datetime with options object but precision 0 - #4784', () => {
+    tableSql = client
+      .schemaBuilder()
+      .table('users', (table) => {
+        table.datetime('foo', { useTz: false, precision: 0 });
+      })
+      .toSQL();
+    equal(1, tableSql.length);
+    expect(tableSql[0].sql).to.equal(
+      'alter table "users" add column "foo" timestamp(0)'
+    );
+  });
+
   it('adding timestamps', () => {
     tableSql = client
       .schemaBuilder()