From a49990b6c02401d6d5a9215712edd5f17ed3bed1 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Sat, 30 Oct 2021 22:41:37 +0200 Subject: [PATCH] Zero precision in timestamp/datetime #4784 --- .../postgres/schema/pg-columncompiler.js | 13 ++-------- test/unit/schema-builder/postgres.js | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/dialects/postgres/schema/pg-columncompiler.js b/lib/dialects/postgres/schema/pg-columncompiler.js index cc49f883cd..d453d67a6d 100644 --- a/lib/dialects/postgres/schema/pg-columncompiler.js +++ b/lib/dialects/postgres/schema/pg-columncompiler.js @@ -72,22 +72,13 @@ class ColumnCompiler_PG extends ColumnCompiler { useTz = !withoutTz; } useTz = typeof useTz === 'boolean' ? useTz : true; - precision = precision ? '(' + precision + ')' : ''; + precision = precision != undefined ? '(' + 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: diff --git a/test/unit/schema-builder/postgres.js b/test/unit/schema-builder/postgres.js index 48ca199630..c3726438d9 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.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 timestamps', () => { tableSql = client .schemaBuilder()