Skip to content

Commit

Permalink
feat: During migrations, add new columns in the user-defined position
Browse files Browse the repository at this point in the history
The "user-defined position" refers to the position in which columns
are defined in the `columns` property of the `schema` object.
  • Loading branch information
nwoltman committed Apr 5, 2017
1 parent b6489fe commit beddf36
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/TableDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class TableDefinition {
const newSchema = this._schema;
const operations = [];
const renamedColumns = []; // List of old column names
var lastColumnName = null;
var columnName;

// Add new columns and modify existing columns
Expand All @@ -129,9 +130,10 @@ class TableDefinition {
const oldColumnDefinition = oldSchema.columns[oldColumnName];

if (!oldColumnDefinition) {
const position = lastColumnName ? ' AFTER ' + pool.escapeId(lastColumnName) : ' FIRST';
operations.push(Operation.create(
Operation.Types.ADD_COLUMN,
'ADD COLUMN ' + pool.escapeId(columnName) + ' ' + newColumnDefinition.$toSQL()
'ADD COLUMN ' + pool.escapeId(columnName) + ' ' + newColumnDefinition.$toSQL() + position
));
} else if (columnName !== oldColumnName) {
operations.push(Operation.create(
Expand All @@ -147,6 +149,8 @@ class TableDefinition {
[columnName]
));
}

lastColumnName = columnName;
}

// Drop old columns (unless the column is being changed)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/MySQLPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,11 @@ describe('MySQLPlus', function() {
' `c` bigint(20) unsigned DEFAULT NULL,\n' +
' `d` bigint(5) unsigned NOT NULL,\n' +
' `ai` int(11) DEFAULT NULL,\n' +
' `ci` char(1) DEFAULT NULL,\n' +
' `eb` varchar(63) DEFAULT NULL,\n' +
' `fb` char(1) DEFAULT \'a\',\n' +
' `gc` bigint(5) unsigned NOT NULL,\n' +
' `hc` varchar(255) NOT NULL,\n' +
' `ci` char(1) DEFAULT NULL,\n' +
' UNIQUE KEY `unique_fk_table_eb_fb` (`eb`,`fb`),\n' +
' KEY `index_fk_table_c` (`c`),\n' +
' KEY `index_fk_table_d` (`d`),\n' +
Expand Down
67 changes: 67 additions & 0 deletions test/integration/newColumnPositioning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

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

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

const ColTypes = MySQLPlus.ColTypes;

describe('when adding new columns', function() {

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

pool.defineTable('new_columns_positioning_test', {
columns: {
a: ColTypes.int(),
b: ColTypes.int(),
},
});
pool2.defineTable('new_columns_positioning_test', {
columns: {
preA: ColTypes.smallint(),
a: ColTypes.int(),
preB: ColTypes.smallint(),
b: ColTypes.int(),
c: ColTypes.smallint(),
},
});

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

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

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

it('should add the columns in the same position that they are defined in the JS columns object', done => {
pool2.sync(err => {
if (err) {
throw err;
}

pool2.query('SHOW CREATE TABLE `new_columns_positioning_test`', (err, rows) => {
if (err) {
throw err;
}

rows[0]['Create Table'].should.equal(
'CREATE TABLE `new_columns_positioning_test` (\n' +
' `preA` smallint(6) DEFAULT NULL,\n' +
' `a` int(11) DEFAULT NULL,\n' +
' `preB` smallint(6) DEFAULT NULL,\n' +
' `b` int(11) DEFAULT NULL,\n' +
' `c` smallint(6) DEFAULT NULL\n' +
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
);
done();
});
});
});

});
2 changes: 1 addition & 1 deletion test/unit/TableDefinition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('TableDefinition', () => {
sql: 'ALTER TABLE `' + existingTableName + '`\n' +
' DROP PRIMARY KEY,\n' +
' MODIFY COLUMN `id` int unsigned NOT NULL,\n' +
' ADD COLUMN `newCol` tinyint',
' ADD COLUMN `newCol` tinyint AFTER `id`',
columns: undefined,
},
];
Expand Down

0 comments on commit beddf36

Please sign in to comment.