Add ability to manually define schema for enu with useNative (#3307) (#3413)

This commit is contained in:
Taras Ozarko 2019-08-26 00:07:30 +03:00 committed by Igor Savin
parent 1ef1a4ef07
commit d6426d75e1
2 changed files with 53 additions and 1 deletions

View File

@ -36,7 +36,7 @@ Object.assign(ColumnCompiler_PG.prototype, {
if (options.useNative) {
let enumName = '';
const schemaName = this.tableCompiler.schemaNameRaw;
const schemaName = options.schemaName || this.tableCompiler.schemaNameRaw;
if (schemaName) {
enumName += `"${schemaName}".`;

View File

@ -964,6 +964,58 @@ describe('PostgreSQL SchemaBuilder', function() {
);
});
it('adding enum with useNative, from manually defined schema and withSchema', function() {
const tableSchema = 'table_schema';
const tableName = 'table_name';
const typeSchema = 'type_schema';
const typeName = 'type_name';
const columnName = 'column_name';
tableSql = client
.schemaBuilder()
.withSchema(tableSchema)
.table(tableName, function(table) {
table.enu(columnName, ['foo', 'bar', 'baz'], {
useNative: true,
schemaName: typeSchema,
enumName: typeName,
});
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal(
`create type "${typeSchema}"."${typeName}" as enum ('foo', 'bar', 'baz')`
);
expect(tableSql[1].sql).to.equal(
`alter table "${tableSchema}"."${tableName}" add column "${columnName}" "${typeSchema}"."${typeName}"`
);
});
it('adding enum with useNative and existingType, from manually defined schema and withSchema', function() {
const tableSchema = 'table_schema';
const tableName = 'table_name';
const typeSchema = 'type_schema';
const typeName = 'type_name';
const columnName = 'column_name';
tableSql = client
.schemaBuilder()
.withSchema(tableSchema)
.table(tableName, function(table) {
table.enu(columnName, null, {
useNative: true,
schemaName: typeSchema,
enumName: typeName,
existingType: true,
});
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
`alter table "${tableSchema}"."${tableName}" add column "${columnName}" "${typeSchema}"."${typeName}"`
);
});
it('adding date', function() {
tableSql = client
.schemaBuilder()