From c931214098f3c6afe563a37e52f112c41b30eda1 Mon Sep 17 00:00:00 2001 From: Vilkas67 <67541650+Vilkas67@users.noreply.github.com> Date: Mon, 5 Jul 2021 12:00:54 +0300 Subject: [PATCH 1/7] Found Lodash and .camelCase method --- lib/schema/tablebuilder.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js index a561b0a125..20270cfa16 100644 --- a/lib/schema/tablebuilder.js +++ b/lib/schema/tablebuilder.js @@ -50,15 +50,29 @@ class TableBuilder { } // The "timestamps" call is really just sets the `created_at` and `updated_at` columns. - timestamps() { - const method = arguments[0] === true ? 'timestamp' : 'datetime'; - const createdAt = this[method]('created_at'); - const updatedAt = this[method]('updated_at'); - if (arguments[1] === true) { - const now = this.client.raw('CURRENT_TIMESTAMP'); - createdAt.notNullable().defaultTo(now); - updatedAt.notNullable().defaultTo(now); + + timestamps(param1, param2, useCamelCase) { + const camel = useCamelCase === true; + const method = param1 === true ? 'timestamp' : 'datetime'; + if (camel) { + const createdAt = this[method].camelCase('created_at'); + const updatedAt = this[method].camelCase('updated_at'); + if (param2 === true) { + const now = this.client.raw('CURRENT_TIMESTAMP'); + createdAt.notNullable().defaultTo(now); + updatedAt.notNullable().defaultTo(now); + } } + { + const createdAt = this[method]('created_at'); + const updatedAt = this[method]('updated_at'); + if (param2 === true) { + const now = this.client.raw('CURRENT_TIMESTAMP'); + createdAt.notNullable().defaultTo(now); + updatedAt.notNullable().defaultTo(now); + } + } + return; } From 7de9f2da0e9f0f60dac619efcd16cedcf5bfad60 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 4 Nov 2021 21:41:04 +0100 Subject: [PATCH 2/7] rebase + typings + rework + tests --- lib/schema/tablebuilder.js | 46 +++++++++++++-------------- test/integration/logger.js | 7 +++- test/integration2/schema/misc.spec.js | 42 ++++++++++++++++++++++++ test/unit/schema-builder/postgres.js | 17 ++++++++++ types/index.d.ts | 10 ++++-- 5 files changed, 95 insertions(+), 27 deletions(-) diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js index 20270cfa16..4115dd2b03 100644 --- a/lib/schema/tablebuilder.js +++ b/lib/schema/tablebuilder.js @@ -10,7 +10,7 @@ const each = require('lodash/each'); const extend = require('lodash/extend'); const toArray = require('lodash/toArray'); const helpers = require('../util/helpers'); -const { isString, isFunction } = require('../util/is'); +const { isString, isFunction, isObject } = require('../util/is'); class TableBuilder { constructor(client, method, tableName, tableNameLike, fn) { @@ -51,29 +51,24 @@ class TableBuilder { // The "timestamps" call is really just sets the `created_at` and `updated_at` columns. - timestamps(param1, param2, useCamelCase) { - const camel = useCamelCase === true; - const method = param1 === true ? 'timestamp' : 'datetime'; - if (camel) { - const createdAt = this[method].camelCase('created_at'); - const updatedAt = this[method].camelCase('updated_at'); - if (param2 === true) { - const now = this.client.raw('CURRENT_TIMESTAMP'); - createdAt.notNullable().defaultTo(now); - updatedAt.notNullable().defaultTo(now); - } - } - { - const createdAt = this[method]('created_at'); - const updatedAt = this[method]('updated_at'); - if (param2 === true) { - const now = this.client.raw('CURRENT_TIMESTAMP'); - createdAt.notNullable().defaultTo(now); - updatedAt.notNullable().defaultTo(now); - } + timestamps() { + let useTimestamps, defaultToNow, useCamelCase; + if (isObject(arguments[0])) { + ({ useTimestamps, defaultToNow, useCamelCase } = arguments[0]); + } else { + useTimestamps = arguments[0]; + defaultToNow = arguments[1]; + useCamelCase = arguments[2]; } + const method = useTimestamps === true ? 'timestamp' : 'datetime'; + const createdAt = this[method](useCamelCase ? 'createdAt' : 'created_at'); + const updatedAt = this[method](useCamelCase ? 'updatedAt' : 'updated_at'); - return; + if (defaultToNow === true) { + const now = this.client.raw('CURRENT_TIMESTAMP'); + createdAt.notNullable().defaultTo(now); + updatedAt.notNullable().defaultTo(now); + } } // Set the comment value for a table, they're only allowed to be called @@ -304,7 +299,12 @@ const AlterMethods = { }, dropTimestamps() { - return this.dropColumns(['created_at', 'updated_at']); + // arguments[0] = useCamelCase + return this.dropColumns( + arguments[0] === true + ? ['createdAt', 'updatedAt'] + : ['created_at', 'updated_at'] + ); }, setNullable(column) { diff --git a/test/integration/logger.js b/test/integration/logger.js index 58ef497c6e..af068b0a90 100644 --- a/test/integration/logger.js +++ b/test/integration/logger.js @@ -98,7 +98,12 @@ module.exports = function (knex) { return _.reduce( val, function (memo, val, key) { - if (_.includes(['created_at', 'updated_at'], key)) { + if ( + _.includes( + ['created_at', 'updated_at', 'createdAt', 'updatedAt'], + key + ) + ) { memo[key] = TEST_TIMESTAMP; } else if (_.includes(['dummy_id'], key)) { memo[key] = TEST_ID; diff --git a/test/integration2/schema/misc.spec.js b/test/integration2/schema/misc.spec.js index 6a871b2cbe..d9ede2a585 100644 --- a/test/integration2/schema/misc.spec.js +++ b/test/integration2/schema/misc.spec.js @@ -671,6 +671,48 @@ describe('Schema (misc)', () => { ]); })); + it('create table with timestamps options', async () => { + await knex.schema.dropTableIfExists('test_table_one'); + await knex.schema + .createTable('test_table_one', (table) => { + if (isMysql(knex)) table.engine('InnoDB'); + table.bigIncrements('id'); + table.timestamps({ + useTimestamps: false, + defaultToNow: true, + useCamelCase: true, + }); + }) + .testSql((tester) => { + tester('mysql', [ + 'create table `test_table_one` (`id` bigint unsigned not null auto_increment primary key, `createdAt` datetime, `updatedAt` datetime) default character set utf8 engine = InnoDB', + ]); + tester( + ['pg', 'cockroachdb'], + [ + 'create table "test_table_one" ("id" bigserial primary key, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', + ] + ); + tester('pg-redshift', [ + 'create table "test_table_one" ("id" bigint identity(1,1) primary key not null, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', + ]); + tester('sqlite3', [ + 'create table `test_table_one` (`id` integer not null primary key autoincrement, `createdAt` datetime not null default CURRENT_TIMESTAMP, `updatedAt` datetime not null default CURRENT_TIMESTAMP)', + ]); + tester('oracledb', [ + `create table "test_table_one" + ( + "id" number(20, 0) not null primary key, + "created_at" timestamp with local time zone not null default CURRENT_TIMESTAMP, + "updated_at" timestamp with local time zone not null default CURRENT_TIMESTAMP + )`, + ]); + tester('mssql', [ + 'CREATE TABLE [test_table_one] ([id] bigint identity(1,1) not null primary key, [createdAt] datetime2 not null default CURRENT_TIMESTAMP, [updatedAt] datetime2) not null default CURRENT_TIMESTAMP', + ]); + }); + }); + it('is possible to set the db engine with the table.engine', () => knex.schema .createTable('test_table_two', (table) => { diff --git a/test/unit/schema-builder/postgres.js b/test/unit/schema-builder/postgres.js index 2caee2ac6f..6f7f7e154f 100644 --- a/test/unit/schema-builder/postgres.js +++ b/test/unit/schema-builder/postgres.js @@ -1619,6 +1619,23 @@ describe('PostgreSQL SchemaBuilder', function () { ); }); + it('adding timestamps with options object', () => { + tableSql = client + .schemaBuilder() + .table('users', (table) => { + table.timestamps({ + useTimestamps: true, + defaultToNow: true, + useCamelCase: true, + }); + }) + .toSQL(); + equal(1, tableSql.length); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "createdAt" timestamptz not null default CURRENT_TIMESTAMP, add column "updatedAt" timestamptz not null default CURRENT_TIMESTAMP' + ); + }); + it('adding binary', function () { tableSql = client .schemaBuilder() diff --git a/types/index.d.ts b/types/index.d.ts index 9c77a5f9dd..db26bfb3fe 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2023,8 +2023,12 @@ export declare namespace Knex { /** @deprecated */ timestamp(columnName: string, withoutTz?: boolean, precision?: number): ColumnBuilder; timestamps( - useTimestampType?: boolean, - makeDefaultNow?: boolean + useTimestamps?: boolean, + defaultToNow?: boolean, + useCamelCase?: boolean + ): ColumnBuilder; + timestamps( + options?: Readonly<{useTimestamps?: boolean, defaultToNow?: boolean, useCamelCase?: boolean}> ): void; geometry(columnName: string): ColumnBuilder; geography(columnName: string): ColumnBuilder; @@ -2072,7 +2076,7 @@ export declare namespace Knex { dropUnique(columnNames: readonly (string | Raw)[], indexName?: string): TableBuilder; dropPrimary(constraintName?: string): TableBuilder; dropIndex(columnNames: string | readonly (string | Raw)[], indexName?: string): TableBuilder; - dropTimestamps(): TableBuilder; + dropTimestamps(useCamelCase?: boolean): TableBuilder; queryContext(context: any): TableBuilder; } From bb7725929082b98e4b31cd840e40a38c163b5021 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 4 Nov 2021 22:07:25 +0100 Subject: [PATCH 3/7] change drop --- test/integration2/schema/misc.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration2/schema/misc.spec.js b/test/integration2/schema/misc.spec.js index d9ede2a585..4c059fc0f2 100644 --- a/test/integration2/schema/misc.spec.js +++ b/test/integration2/schema/misc.spec.js @@ -672,9 +672,8 @@ describe('Schema (misc)', () => { })); it('create table with timestamps options', async () => { - await knex.schema.dropTableIfExists('test_table_one'); await knex.schema - .createTable('test_table_one', (table) => { + .createTable('test_table_timestamp', (table) => { if (isMysql(knex)) table.engine('InnoDB'); table.bigIncrements('id'); table.timestamps({ @@ -711,6 +710,7 @@ describe('Schema (misc)', () => { 'CREATE TABLE [test_table_one] ([id] bigint identity(1,1) not null primary key, [createdAt] datetime2 not null default CURRENT_TIMESTAMP, [updatedAt] datetime2) not null default CURRENT_TIMESTAMP', ]); }); + await knex.schema.dropTableIfExists('test_table_timestamp'); }); it('is possible to set the db engine with the table.engine', () => From 2637ed5da241a7239289f4454bec1859009763da Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 4 Nov 2021 22:13:52 +0100 Subject: [PATCH 4/7] fix test --- test/integration2/schema/misc.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration2/schema/misc.spec.js b/test/integration2/schema/misc.spec.js index 4c059fc0f2..57636ecc0b 100644 --- a/test/integration2/schema/misc.spec.js +++ b/test/integration2/schema/misc.spec.js @@ -696,7 +696,7 @@ describe('Schema (misc)', () => { 'create table "test_table_one" ("id" bigint identity(1,1) primary key not null, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', ]); tester('sqlite3', [ - 'create table `test_table_one` (`id` integer not null primary key autoincrement, `createdAt` datetime not null default CURRENT_TIMESTAMP, `updatedAt` datetime not null default CURRENT_TIMESTAMP)', + 'create table `test_table_timestamp` (`id` integer not null primary key autoincrement, `createdAt` datetime not null default CURRENT_TIMESTAMP, `updatedAt` datetime not null default CURRENT_TIMESTAMP)', ]); tester('oracledb', [ `create table "test_table_one" @@ -707,7 +707,7 @@ describe('Schema (misc)', () => { )`, ]); tester('mssql', [ - 'CREATE TABLE [test_table_one] ([id] bigint identity(1,1) not null primary key, [createdAt] datetime2 not null default CURRENT_TIMESTAMP, [updatedAt] datetime2) not null default CURRENT_TIMESTAMP', + 'CREATE TABLE [test_table_timestamp] ([id] bigint identity(1,1) not null primary key, [createdAt] datetime2 not null CONSTRAINT [test_table_timestamp_createdat_default] DEFAULT CURRENT_TIMESTAMP, [updatedAt] datetime2 not null CONSTRAINT [test_table_timestamp_updatedat_default] DEFAULT CURRENT_TIMESTAMP)', ]); }); await knex.schema.dropTableIfExists('test_table_timestamp'); From 83274b1b11d5c5891527f70ceafa0496c232a14e Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 4 Nov 2021 22:18:15 +0100 Subject: [PATCH 5/7] fix tablename --- test/integration2/schema/misc.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration2/schema/misc.spec.js b/test/integration2/schema/misc.spec.js index 57636ecc0b..eb6676f379 100644 --- a/test/integration2/schema/misc.spec.js +++ b/test/integration2/schema/misc.spec.js @@ -684,22 +684,22 @@ describe('Schema (misc)', () => { }) .testSql((tester) => { tester('mysql', [ - 'create table `test_table_one` (`id` bigint unsigned not null auto_increment primary key, `createdAt` datetime, `updatedAt` datetime) default character set utf8 engine = InnoDB', + 'create table `test_table_timestamp` (`id` bigint unsigned not null auto_increment primary key, `createdAt` datetime not null default CURRENT_TIMESTAMP, `updatedAt` datetime not null default CURRENT_TIMESTAMP) default character set utf8 engine = InnoDB', ]); tester( ['pg', 'cockroachdb'], [ - 'create table "test_table_one" ("id" bigserial primary key, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', + 'create table "test_table_timestamp" ("id" bigserial primary key, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', ] ); tester('pg-redshift', [ - 'create table "test_table_one" ("id" bigint identity(1,1) primary key not null, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', + 'create table "test_table_timestamp" ("id" bigint identity(1,1) primary key not null, "createdAt" timestamptz not null default CURRENT_TIMESTAMP, "updatedAt" timestamptz not null default CURRENT_TIMESTAMP)', ]); tester('sqlite3', [ 'create table `test_table_timestamp` (`id` integer not null primary key autoincrement, `createdAt` datetime not null default CURRENT_TIMESTAMP, `updatedAt` datetime not null default CURRENT_TIMESTAMP)', ]); tester('oracledb', [ - `create table "test_table_one" + `create table "test_table_timestamp" ( "id" number(20, 0) not null primary key, "created_at" timestamp with local time zone not null default CURRENT_TIMESTAMP, From d403676a5ef80302c171f58c8db54377533100d7 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Tue, 9 Nov 2021 21:20:48 +0100 Subject: [PATCH 6/7] add parameters --- lib/schema/tablebuilder.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js index 4115dd2b03..1c04dba284 100644 --- a/lib/schema/tablebuilder.js +++ b/lib/schema/tablebuilder.js @@ -51,14 +51,9 @@ class TableBuilder { // The "timestamps" call is really just sets the `created_at` and `updated_at` columns. - timestamps() { - let useTimestamps, defaultToNow, useCamelCase; + timestamps(useTimestamps, defaultToNow, useCamelCase) { if (isObject(arguments[0])) { ({ useTimestamps, defaultToNow, useCamelCase } = arguments[0]); - } else { - useTimestamps = arguments[0]; - defaultToNow = arguments[1]; - useCamelCase = arguments[2]; } const method = useTimestamps === true ? 'timestamp' : 'datetime'; const createdAt = this[method](useCamelCase ? 'createdAt' : 'created_at'); From bde624a9f471f9636094adecc3d57a7fa0fb8b6a Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 9 Dec 2021 14:47:59 +0100 Subject: [PATCH 7/7] change parameter --- lib/schema/tablebuilder.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js index 1c04dba284..b77f9eced3 100644 --- a/lib/schema/tablebuilder.js +++ b/lib/schema/tablebuilder.js @@ -52,8 +52,8 @@ class TableBuilder { // The "timestamps" call is really just sets the `created_at` and `updated_at` columns. timestamps(useTimestamps, defaultToNow, useCamelCase) { - if (isObject(arguments[0])) { - ({ useTimestamps, defaultToNow, useCamelCase } = arguments[0]); + if (isObject(useTimestamps)) { + ({ useTimestamps, defaultToNow, useCamelCase } = useTimestamps); } const method = useTimestamps === true ? 'timestamp' : 'datetime'; const createdAt = this[method](useCamelCase ? 'createdAt' : 'created_at');