Skip to content

Commit

Permalink
lib: Fix bug when migrating a table with no foreign keys to having some
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Apr 14, 2017
1 parent 941c301 commit fa8960b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/TableDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Operation = require('./Operation');

const cloneDeep = require('lodash/cloneDeep');
const diffKeys = require('./utils/diffKeys');
const isEmpty = require('lodash/isEmpty');
const isEqual = require('lodash/isEqual');

const KEY_TYPES = {
Expand Down Expand Up @@ -753,7 +752,7 @@ function generateForegnKeysSchema(createDefinitions) {
};
}

return isEmpty(foreignKeys) ? null : foreignKeys;
return foreignKeys;
}

function arraysEqual(a, b) {
Expand Down
51 changes: 51 additions & 0 deletions test/integration/addForeignKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const MySQLPlus = require('../../lib/MySQLPlus');

const config = require('../config');

const ColTypes = MySQLPlus.ColTypes;

describe('when migrating a table with no foreign keys to having some foreign keys', function() {

const pool = MySQLPlus.createPool(config);
const pool2 = MySQLPlus.createPool(config);

pool.defineTable('add_foreign_keys_foreign', {
columns: {
id: ColTypes.int().notNull().primaryKey(),
},
});
pool.defineTable('add_foreign_keys_main', {
columns: {
id: ColTypes.int().notNull().index(),
},
});
pool2.defineTable('add_foreign_keys_main', {
columns: {
id: ColTypes.int().notNull().index(),
},
foreignKeys: {
id: 'add_foreign_keys_foreign.id',
},
});

before(done => {
pool.sync(err => {
if (err) {
throw err;
}

pool.end(done);
});
});

after(done => {
pool2.end(done);
});

it('should not error', done => {
pool2.sync(done);
});

});

0 comments on commit fa8960b

Please sign in to comment.