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

fix(postgresql): add primaryKey option for uuid #5212

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
14 changes: 14 additions & 0 deletions lib/dialects/cockroachdb/crdb-columncompiler.js
@@ -0,0 +1,14 @@
const ColumnCompiler_PG = require('../postgres/schema/pg-columncompiler.js');

class ColumnCompiler_CRDB extends ColumnCompiler_PG {
uuid(options = { primaryKey: false }) {
return (
'uuid' +
(this.tableCompiler._canBeAddPrimaryKey(options)
? ' primary key default gen_random_uuid()'
: '')
);
}
}

module.exports = ColumnCompiler_CRDB;
5 changes: 5 additions & 0 deletions lib/dialects/cockroachdb/index.js
Expand Up @@ -3,6 +3,7 @@
const Client_PostgreSQL = require('../postgres');
const Transaction = require('../postgres/execution/pg-transaction');
const QueryCompiler = require('./crdb-querycompiler');
const ColumnCompiler = require('./crdb-columncompiler');
const TableCompiler = require('./crdb-tablecompiler');
const ViewCompiler = require('./crdb-viewcompiler');
const QueryBuilder = require('./crdb-querybuilder');
Expand All @@ -19,6 +20,10 @@ class Client_CockroachDB extends Client_PostgreSQL {
return new QueryCompiler(this, builder, formatter);
}

columnCompiler() {
return new ColumnCompiler(this, ...arguments);
}

tableCompiler() {
return new TableCompiler(this, ...arguments);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/dialects/postgres/schema/pg-columncompiler.js
Expand Up @@ -124,6 +124,13 @@ class ColumnCompiler_PG extends ColumnCompiler {
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
);
}

uuid(options = { primaryKey: false }) {
return (
'uuid' +
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '')
);
}
}

ColumnCompiler_PG.prototype.bigint = 'bigint';
Expand All @@ -133,7 +140,6 @@ 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';

function jsonColumn(client, jsonb) {
if (
Expand Down
6 changes: 4 additions & 2 deletions lib/schema/columncompiler.js
Expand Up @@ -277,8 +277,10 @@ ColumnCompiler.prototype.geography = 'geography';
ColumnCompiler.prototype.point = 'point';
ColumnCompiler.prototype.enu = 'varchar';
ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
ColumnCompiler.prototype.uuid = ({ useBinaryUuid = false } = {}) =>
useBinaryUuid ? 'binary(16)' : 'char(36)';
ColumnCompiler.prototype.uuid = ({
useBinaryUuid = false,
primaryKey = false,
} = {}) => (useBinaryUuid ? 'binary(16)' : 'char(36)');
ColumnCompiler.prototype.integer =
ColumnCompiler.prototype.smallint =
ColumnCompiler.prototype.mediumint =
Expand Down
1 change: 1 addition & 0 deletions test/db-less-test-suite.js
Expand Up @@ -22,6 +22,7 @@ describe('Query Building Tests', function () {
require('./unit/schema-builder/mysql')('mysql2');
require('./unit/schema-builder/extensions');
require('./unit/schema-builder/postgres');
require('./unit/schema-builder/cockroachdb');
require('./unit/schema-builder/redshift');
require('./unit/schema-builder/sqlite3');
require('./unit/schema-builder/oracle');
Expand Down
37 changes: 37 additions & 0 deletions test/integration2/schema/misc.spec.js
Expand Up @@ -482,6 +482,43 @@ describe('Schema (misc)', () => {
});
});

describe('uuid types - postgres', () => {
before(async () => {
await knex.schema.createTable('uuid_column_test', (table) => {
table.uuid('id', { primaryKey: true });
});
});

after(async () => {
await knex.schema.dropTable('uuid_column_test');
});

it('#5211 - creates an uuid column as primary key', async function () {
if (!isPgBased(knex)) {
return this.skip();
}

const table_name = 'uuid_column_test';
const expected_column = 'id';
const expected_type = 'uuid';

const cols = await knex.raw(
`select c.column_name, c.data_type
from INFORMATION_SCHEMA.COLUMNS c
join INFORMATION_SCHEMA.KEY_COLUMN_USAGE cu
on (c.table_name = cu.table_name and c.column_name = cu.column_name)
where c.table_name = ?
and (cu.constraint_name like '%_pkey' or cu.constraint_name = 'primary')`,
table_name
);
const column_name = cols.rows[0].column_name;
const column_type = cols.rows[0].data_type;

expect(column_name).to.equal(expected_column);
expect(column_type).to.equal(expected_type);
});
});

describe('increments types - mysql', () => {
before(() =>
Promise.all([
Expand Down
23 changes: 23 additions & 0 deletions test/unit/schema-builder/cockroachdb.js
@@ -0,0 +1,23 @@
const { expect } = require('chai');

let tableSql;

const PG_Client = require('../../../lib/dialects/cockroachdb');
const client = new PG_Client({ client: 'pg' });

const equal = require('chai').assert.equal;

describe('CockroachDB SchemaBuilder', function () {
it('create table with uuid primary key in one go', function () {
tableSql = client
.schemaBuilder()
.createTable('uuid_primary', function (table) {
table.uuid('id', { primaryKey: true });
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'create table "uuid_primary" ("id" uuid primary key default gen_random_uuid())'
);
});
});
14 changes: 13 additions & 1 deletion test/unit/schema-builder/postgres.js
Expand Up @@ -137,6 +137,19 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});

it('create table with uuid primary key in one go', function () {
tableSql = client
.schemaBuilder()
.createTable('uuid_primary', function (table) {
table.uuid('id', { primaryKey: true });
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'create table "uuid_primary" ("id" uuid primary key)'
);
});

it('basic alter table', function () {
tableSql = client
.schemaBuilder()
Expand Down Expand Up @@ -446,7 +459,6 @@ describe('PostgreSQL SchemaBuilder', function () {
);
});


it('refresh view concurrently', function () {
tableSql = client
.schemaBuilder()
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -2144,7 +2144,7 @@ export declare namespace Knex {
): ColumnBuilder;
json(columnName: string): ColumnBuilder;
jsonb(columnName: string): ColumnBuilder;
uuid(columnName: string, options?: Readonly<{useBinaryUuid?: boolean}>): ColumnBuilder;
uuid(columnName: string, options?: Readonly<{useBinaryUuid?: boolean, primaryKey?: boolean}>): ColumnBuilder;
comment(val: string): void;
specificType(columnName: string, type: string): ColumnBuilder;
primary(columnNames: readonly string[], options?: Readonly<{constraintName?: string, deferrable?: deferrableType}>): TableBuilder;
Expand Down