From d6426d75e1fa269ca4697aac82e661077bc91f29 Mon Sep 17 00:00:00 2001 From: Taras Ozarko Date: Mon, 26 Aug 2019 00:07:30 +0300 Subject: [PATCH] Add ability to manually define schema for enu with useNative (#3307) (#3413) --- .../postgres/schema/columncompiler.js | 2 +- test/unit/schema/postgres.js | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/dialects/postgres/schema/columncompiler.js b/lib/dialects/postgres/schema/columncompiler.js index b30f3fb30..9fdd7b3d9 100644 --- a/lib/dialects/postgres/schema/columncompiler.js +++ b/lib/dialects/postgres/schema/columncompiler.js @@ -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}".`; diff --git a/test/unit/schema/postgres.js b/test/unit/schema/postgres.js index 99b117302..8b1a55ab4 100644 --- a/test/unit/schema/postgres.js +++ b/test/unit/schema/postgres.js @@ -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()