From f130c153dda38fb798d1157baf843a844feac2f0 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Sat, 30 Oct 2021 22:41:37 +0200 Subject: [PATCH 1/2] 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..95b3eb4d2c 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..2caee2ac6f 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() From 5c42cc82788cb9c1df4f4d6e7f9442fb6824c4de Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Sat, 30 Oct 2021 22:52:44 +0200 Subject: [PATCH 2/2] check null too --- lib/dialects/postgres/schema/pg-columncompiler.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/dialects/postgres/schema/pg-columncompiler.js b/lib/dialects/postgres/schema/pg-columncompiler.js index 95b3eb4d2c..5fb7fa2580 100644 --- a/lib/dialects/postgres/schema/pg-columncompiler.js +++ b/lib/dialects/postgres/schema/pg-columncompiler.js @@ -72,7 +72,10 @@ class ColumnCompiler_PG extends ColumnCompiler { useTz = !withoutTz; } useTz = typeof useTz === 'boolean' ? useTz : true; - precision = precision !== undefined ? '(' + precision + ')' : ''; + precision = + precision !== undefined && precision !== null + ? '(' + precision + ')' + : ''; return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`; }