From 5815f28711cb1336d50259d7fc03449a70516f2f Mon Sep 17 00:00:00 2001 From: Agnes Lin Date: Fri, 13 Dec 2019 12:49:19 -0500 Subject: [PATCH] test: add test for string auto-generation --- lib/migration.js | 44 +++++++++++++++---------------- test/postgresql.migration.test.js | 43 +++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/lib/migration.js b/lib/migration.js index 07596e0f..23a19ff2 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -299,21 +299,20 @@ function mixinMigration(PostgreSQL) { let result = self.columnDataType(model, propName); // checks if dataType is set to uuid let postgreType = modelDef.properties[propName]; - if(postgreType.postgresql && postgreType.postgresql.dataType){ + if (postgreType.postgresql && postgreType.postgresql.dataType) { postgreType = postgreType.postgresql.dataType.toUpperCase(); } if (prop.generated) { if (result === 'INTEGER') { return 'SERIAL'; - } - else if (postgreType === 'UUID') { + } else if (postgreType === 'UUID') { result = result + ' NOT NULL' + ' DEFAULT uuid_generate_v4()'; return result; } else { console.log(chalk.red('>>> WARNING: ') + - `auto-generattion is not supported for type "${chalk.yellow(prop.type)}". Please add your own function to the table "${chalk.yellow(model)}".` - ); + `auto-generattion is not supported for type "${chalk.yellow(prop.type)}". \ + Please add your own function to the table "${chalk.yellow(model)}".`); } } @@ -336,28 +335,29 @@ function mixinMigration(PostgreSQL) { 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' + 'CREATE SCHEMA ' + self.escapeName(self.schema(model)), - function(err) { - if (err && err.code !== '42P06') { - return cb && cb(err); - } - self.execute('CREATE TABLE ' + name + ' (\n ' + - self.propertiesSQL(model) + '\n)', - function(err, info) { - if (err) { - return cb(err, info); + function(err) { + if (err && err.code !== '42P06') { + return cb && cb(err); } - self.addIndexes(model, undefined, function(err) { + self.execute('CREATE TABLE ' + name + ' (\n ' + + self.propertiesSQL(model) + '\n)', + function(err, info) { if (err) { - return cb(err); + return cb(err, info); } - const fkSQL = self.getForeignKeySQL(model, - self.getModelDefinition(model).settings.foreignKeys); - self.addForeignKeys(model, fkSQL, function(err, result) { - cb(err); + self.addIndexes(model, undefined, function(err) { + if (err) { + return cb(err); + } + const fkSQL = self.getForeignKeySQL(model, + self.getModelDefinition(model).settings.foreignKeys); + self.addForeignKeys(model, fkSQL, function(err, result) { + cb(err); + }); }); }); - }); - }); + }, + ); }; PostgreSQL.prototype.buildIndex = function(model, property) { diff --git a/test/postgresql.migration.test.js b/test/postgresql.migration.test.js index 4c432cd1..0d38217d 100644 --- a/test/postgresql.migration.test.js +++ b/test/postgresql.migration.test.js @@ -14,7 +14,7 @@ describe('migrations', function() { before(setup); it('should run migration', function(done) { - db.automigrate('UserDataWithIndexes', done); + db.automigrate(['UserDataWithIndexes', 'OrderData'], done); }); it('UserDataWithIndexes should have correct indexes', function(done) { @@ -73,6 +73,26 @@ describe('migrations', function() { done(); }); }); + + it('OrderData should have correct id type uuid and default function', function(done) { + checkDefault('OrderData', function(err, cols) { + assert.deepEqual(cols, { + ordercode: + {column_name: 'ordercode', + column_default: 'uuid_generate_v4()', + data_type: 'uuid'}, + ordername: + {column_name: 'ordername', + column_default: null, + data_type: 'text'}, + id: + {column_name: 'id', + column_default: 'nextval(\'orderdata_id_seq\'::regclass)', + data_type: 'integer'}, + }); + done(); + }); + }); }); function setup(done) { @@ -118,6 +138,13 @@ function setup(done) { }, }, }); + const OrderData = db.define('OrderData', { + ordercode: {type: 'String', required: true, generated: true, useDefaultIdType: false, + postgresql: { + dataType: 'uuid', + }}, + ordername: {type: 'String'}, + }); done(); } @@ -161,3 +188,17 @@ function table(model) { function query(sql, cb) { db.adapter.query(sql, cb); } +function checkDefault(model, cb) { + query('SELECT column_name, column_default, data_type FROM information_schema.columns \ + WHERE(table_schema, table_name) = (\'public\', \'orderdata\');', + function(err, data) { + const cols = {}; + if (!err) { + data.forEach(function(index) { + cols[index.column_name] = index; + delete index.name; + }); + } + cb(err, cols); + }); +}