Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero precision in timestamp/datetime #4784 #4786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch!

}

// 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