Skip to content

Commit

Permalink
Fix native enum with specified schema (#3307) (#3400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saka7 authored and kibertoad committed Aug 24, 2019
1 parent 658e612 commit 1ef1a4e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/dialects/postgres/schema/columncompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ Object.assign(ColumnCompiler_PG.prototype, {
: allowed.join("', '");

if (options.useNative) {
let enumName = '';
const schemaName = this.tableCompiler.schemaNameRaw;

if (schemaName) {
enumName += `"${schemaName}".`;
}

enumName += `"${options.enumName}"`;

if (!options.existingType) {
this.tableCompiler.unshiftQuery(
`create type "${options.enumName}" as enum ('${values}')`
`create type ${enumName} as enum ('${values}')`
);
}

return `"${options.enumName}"`;
return enumName;
}
return `text check (${this.formatter.wrap(this.args[0])} in ('${values}'))`;
},
Expand Down
26 changes: 25 additions & 1 deletion test/integration/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,31 @@ module.exports = function(knex) {
afterEach(function() {
return knex.schema
.dropTableIfExists('native_enum_test')
.raw('DROP TYPE "foo_type"');
.raw('DROP TYPE IF EXISTS "foo_type"')
.raw('DROP SCHEMA IF EXISTS "test" CASCADE');
});

it('uses native type with schema', function() {
return knex.schema.createSchemaIfNotExists('test').then(() => {
return knex.schema
.withSchema('test')
.createTable('native_enum_test', function(table) {
table
.enum('foo_column', ['a', 'b', 'c'], {
useNative: true,
enumName: 'foo_type',
schema: true,
})
.notNull();
table.uuid('id').notNull();
})
.testSql(function(tester) {
tester('pg', [
"create type \"test\".\"foo_type\" as enum ('a', 'b', 'c')",
'create table "test"."native_enum_test" ("foo_column" "test"."foo_type" not null, "id" uuid not null)',
]);
});
});
});

it('uses native type when useNative is specified', function() {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/schema/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,32 @@ describe('PostgreSQL SchemaBuilder', function() {
);
});

it('adding enum with useNative and withSchema', function() {
const schema = 'test';
const enumName = 'foo_type';

tableSql = client
.schemaBuilder()
.withSchema(schema)
.table('users', function(table) {
table
.enu('foo', ['bar', 'baz'], {
useNative: true,
schema: true,
enumName,
})
.notNullable();
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal(
`create type "${schema}"."${enumName}" as enum ('bar', 'baz')`
);
expect(tableSql[1].sql).to.equal(
`alter table "${schema}"."users" add column "foo" "${schema}"."${enumName}" not null`
);
});

it('adding enum with useNative and existingType', function() {
tableSql = client
.schemaBuilder()
Expand Down

0 comments on commit 1ef1a4e

Please sign in to comment.