Fixing posgres datetime and timestamp column created with wrong format (#4578)

This commit is contained in:
zeotuan 2021-07-21 08:23:20 +10:00 committed by GitHub
parent c335fda657
commit 55eadcf7ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 6 deletions

View File

@ -71,10 +71,10 @@ class ColumnCompiler_PG extends ColumnCompiler {
} else {
useTz = !withoutTz;
}
useTz = typeof useTz === 'boolean' ? useTz : true;
precision = precision ? '(' + precision + ')' : '';
return `${useTz ? 'timestamptz' : 'timestamp'}${
precision ? '(' + precision + ')' : ''
}`;
return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`;
}
timestamp(withoutTz = false, precision) {
@ -84,10 +84,10 @@ class ColumnCompiler_PG extends ColumnCompiler {
} else {
useTz = !withoutTz;
}
useTz = typeof useTz === 'boolean' ? useTz : true;
precision = precision ? '(' + precision + ')' : '';
return `${useTz ? 'timestamptz' : 'timestamp'}${
precision ? '(' + precision + ')' : ''
}`;
return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`;
}
// Modifiers:

View File

@ -1155,6 +1155,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding default datetime', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo');
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz'
);
});
it('adding timestamp with timezone', () => {
tableSql = client
.schemaBuilder()
@ -1168,6 +1181,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding datetime with timezone', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo', false);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz'
);
});
it('adding timestamp without timezone', () => {
tableSql = client
.schemaBuilder()
@ -1181,6 +1207,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding datetime without timezone', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo', true);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamp'
);
});
it('adding timestamp with precision', () => {
tableSql = client
.schemaBuilder()
@ -1194,6 +1233,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding datetime with precision', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo', undefined, 3);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz(3)'
);
});
it('adding timestamp with options object', () => {
tableSql = client
.schemaBuilder()
@ -1207,6 +1259,32 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding timestamp with options object but no timestamp', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.timestamp('foo', { precision: 3 });
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz(3)'
);
});
it('adding timestamp with empty options object', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.timestamp('foo', {});
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz'
);
});
it('adding datetime with options object', () => {
tableSql = client
.schemaBuilder()
@ -1220,6 +1298,32 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});
it('adding datetime with options object but no timestamp', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo', { precision: 3 });
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz(3)'
);
});
it('adding datetime with empty options object', () => {
tableSql = client
.schemaBuilder()
.table('users', (table) => {
table.datetime('foo', {});
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'alter table "users" add column "foo" timestamptz'
);
});
it('adding timestamps', () => {
tableSql = client
.schemaBuilder()