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 1 commit
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
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
41 changes: 41 additions & 0 deletions test/integration2/schema/misc.spec.js
Expand Up @@ -482,6 +482,47 @@ describe('Schema (misc)', () => {
});
});

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

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

const table_name = 'uuid_columns_test';
const expected_column = 'id';

return knex
.raw(
'SELECT c.oid FROM pg_catalog.pg_class c WHERE c.relname = ?',
[table_name]
)
.then((res) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use instead await notation

const column_oid = res.rows[0].oid;

return knex
.raw(
`select c.column_name
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we select and test tolumn type too ?

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`,
table_name
)
.then((res) => {
const column_name = res.rows[0].column_name;

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

describe('increments types - mysql', () => {
before(() =>
Promise.all([
Expand Down
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