diff --git a/lib/dialects/mssql/schema/mssql-columncompiler.js b/lib/dialects/mssql/schema/mssql-columncompiler.js index 87941d0f92..568407c688 100644 --- a/lib/dialects/mssql/schema/mssql-columncompiler.js +++ b/lib/dialects/mssql/schema/mssql-columncompiler.js @@ -143,7 +143,9 @@ ColumnCompiler_MSSQL.prototype.json = 'nvarchar(max)'; // TODO: mssql supports check constraints as of SQL Server 2008 // so make enu here more like postgres ColumnCompiler_MSSQL.prototype.enu = 'nvarchar(100)'; -ColumnCompiler_MSSQL.prototype.uuid = 'uniqueidentifier'; +ColumnCompiler_MSSQL.prototype.uuid = ({ useBinaryUuid = false } = {}) => + useBinaryUuid ? 'binary(16)' : 'uniqueidentifier'; + ColumnCompiler_MSSQL.prototype.datetime = 'datetime2'; ColumnCompiler_MSSQL.prototype.bool = 'bit'; diff --git a/lib/dialects/postgres/schema/pg-columncompiler.js b/lib/dialects/postgres/schema/pg-columncompiler.js index 5fb7fa2580..facb5d654f 100644 --- a/lib/dialects/postgres/schema/pg-columncompiler.js +++ b/lib/dialects/postgres/schema/pg-columncompiler.js @@ -113,7 +113,8 @@ ColumnCompiler_PG.prototype.double = 'double precision'; ColumnCompiler_PG.prototype.floating = 'real'; ColumnCompiler_PG.prototype.smallint = 'smallint'; ColumnCompiler_PG.prototype.tinyint = 'smallint'; -ColumnCompiler_PG.prototype.uuid = 'uuid'; +ColumnCompiler_PG.prototype.uuid = ({ useBinaryUuid = false } = {}) => + useBinaryUuid ? 'binary(16)' : 'uuid'; function jsonColumn(client, jsonb) { if ( diff --git a/lib/dialects/redshift/schema/redshift-columncompiler.js b/lib/dialects/redshift/schema/redshift-columncompiler.js index cd3f4059db..405ce4b824 100644 --- a/lib/dialects/redshift/schema/redshift-columncompiler.js +++ b/lib/dialects/redshift/schema/redshift-columncompiler.js @@ -2,6 +2,7 @@ // ------- const ColumnCompiler_PG = require('../../postgres/schema/pg-columncompiler'); +const ColumnCompiler = require('../../../schema/columncompiler'); class ColumnCompiler_Redshift extends ColumnCompiler_PG { constructor() { @@ -54,7 +55,7 @@ ColumnCompiler_Redshift.prototype.mediumblob = 'varchar(16777218)'; ColumnCompiler_Redshift.prototype.set = 'text'; ColumnCompiler_Redshift.prototype.text = 'varchar(max)'; ColumnCompiler_Redshift.prototype.tinyblob = 'varchar(256)'; -ColumnCompiler_Redshift.prototype.uuid = 'char(36)'; +ColumnCompiler_Redshift.prototype.uuid = ColumnCompiler.prototype.uuid; ColumnCompiler_Redshift.prototype.varbinary = 'varchar(max)'; ColumnCompiler_Redshift.prototype.bigint = 'bigint'; ColumnCompiler_Redshift.prototype.bool = 'boolean'; diff --git a/lib/schema/columncompiler.js b/lib/schema/columncompiler.js index bf1a9319ab..56a35adf27 100644 --- a/lib/schema/columncompiler.js +++ b/lib/schema/columncompiler.js @@ -147,7 +147,8 @@ ColumnCompiler.prototype.geography = 'geography'; ColumnCompiler.prototype.point = 'point'; ColumnCompiler.prototype.enu = 'varchar'; ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text'; -ColumnCompiler.prototype.uuid = 'char(36)'; +ColumnCompiler.prototype.uuid = ({ useBinaryUuid = false } = {}) => + useBinaryUuid ? 'binary(16)' : 'char(36)'; ColumnCompiler.prototype.increments = ({ primaryKey = true } = {}) => 'integer not null' + (primaryKey ? ' primary key' : '') + ' autoincrement'; ColumnCompiler.prototype.bigincrements = ({ primaryKey = true } = {}) => diff --git a/test/unit/schema-builder/mssql.js b/test/unit/schema-builder/mssql.js index 91649f22b2..709ff44114 100644 --- a/test/unit/schema-builder/mssql.js +++ b/test/unit/schema-builder/mssql.js @@ -1167,6 +1167,32 @@ describe('MSSQL SchemaBuilder', function () { ); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'ALTER TABLE [users] ADD [foo] uniqueidentifier' + ); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + equal(1, tableSql.length); + equal(tableSql[0].sql, 'ALTER TABLE [users] ADD [foo] binary(16)'); + }); + it('is possible to set raw statements in defaultTo, #146', function () { tableSql = client .schemaBuilder() diff --git a/test/unit/schema-builder/mysql.js b/test/unit/schema-builder/mysql.js index 453914aa73..6304f971e4 100644 --- a/test/unit/schema-builder/mysql.js +++ b/test/unit/schema-builder/mysql.js @@ -1115,6 +1115,34 @@ module.exports = function (dialect) { ); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table `users` add `foo` char(36)' + ); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table `users` add `foo` binary(16)' + ); + }); + it('test set comment', function () { tableSql = client .schemaBuilder() diff --git a/test/unit/schema-builder/oracledb.js b/test/unit/schema-builder/oracledb.js index dc07b5f85f..f0299be6be 100644 --- a/test/unit/schema-builder/oracledb.js +++ b/test/unit/schema-builder/oracledb.js @@ -1011,6 +1011,32 @@ describe('OracleDb SchemaBuilder', function () { ); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal('alter table "users" add "foo" char(36)'); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add "foo" binary(16)' + ); + }); + it('test set comment', function () { tableSql = client .schemaBuilder() diff --git a/test/unit/schema-builder/postgres.js b/test/unit/schema-builder/postgres.js index 8aff022c2e..de5b733406 100644 --- a/test/unit/schema-builder/postgres.js +++ b/test/unit/schema-builder/postgres.js @@ -1671,6 +1671,34 @@ describe('PostgreSQL SchemaBuilder', function () { ); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" uuid' + ); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" binary(16)' + ); + }); + it('set comment', function () { tableSql = client .schemaBuilder() diff --git a/test/unit/schema-builder/redshift.js b/test/unit/schema-builder/redshift.js index 3d3b92dfd4..028c7b30dc 100644 --- a/test/unit/schema-builder/redshift.js +++ b/test/unit/schema-builder/redshift.js @@ -850,6 +850,34 @@ describe('Redshift SchemaBuilder', function () { ); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" char(36)' + ); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" binary(16)' + ); + }); + it('allows adding default json objects when the column is json', function () { tableSql = client .schemaBuilder() diff --git a/test/unit/schema-builder/sqlite3.js b/test/unit/schema-builder/sqlite3.js index 0cebd7b123..1333c39064 100644 --- a/test/unit/schema-builder/sqlite3.js +++ b/test/unit/schema-builder/sqlite3.js @@ -894,6 +894,34 @@ describe('SQLite SchemaBuilder', function () { equal(tableSql[0].sql, 'alter table `users` add column `foo` blob'); }); + it('adding uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo'); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table `users` add column `foo` char(36)' + ); + }); + + it('adding binary uuid', function () { + tableSql = client + .schemaBuilder() + .table('users', function (table) { + table.uuid('foo', { useBinaryUuid: true }); + }) + .toSQL(); + + expect(tableSql.length).to.equal(1); + expect(tableSql[0].sql).to.equal( + 'alter table `users` add column `foo` binary(16)' + ); + }); + it('allows for on delete cascade with foreign keys, #166', function () { tableSql = client .schemaBuilder() diff --git a/types/index.d.ts b/types/index.d.ts index fbd82073bb..0517318c8a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2049,7 +2049,7 @@ export declare namespace Knex { ): ColumnBuilder; json(columnName: string): ColumnBuilder; jsonb(columnName: string): ColumnBuilder; - uuid(columnName: string): ColumnBuilder; + uuid(columnName: string, options?: Readonly<{useBinaryUuid?: boolean}>): ColumnBuilder; comment(val: string): void; specificType(columnName: string, type: string): ColumnBuilder; primary(columnNames: readonly string[], options?: Readonly<{constraintName?: string, deferrable?: deferrableType}>): TableBuilder;