Skip to content

Commit

Permalink
Zero precision in timestamp/datetime #4784 (#4786)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Oct 30, 2021
1 parent d31dc64 commit a431485
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
16 changes: 5 additions & 11 deletions lib/dialects/postgres/schema/pg-columncompiler.js
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions test/unit/schema-builder/postgres.js
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a431485

Please sign in to comment.