Skip to content

Commit beddf36

Browse files
committed
feat: During migrations, add new columns in the user-defined position
The "user-defined position" refers to the position in which columns are defined in the `columns` property of the `schema` object.
1 parent b6489fe commit beddf36

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

lib/TableDefinition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class TableDefinition {
118118
const newSchema = this._schema;
119119
const operations = [];
120120
const renamedColumns = []; // List of old column names
121+
var lastColumnName = null;
121122
var columnName;
122123

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

131132
if (!oldColumnDefinition) {
133+
const position = lastColumnName ? ' AFTER ' + pool.escapeId(lastColumnName) : ' FIRST';
132134
operations.push(Operation.create(
133135
Operation.Types.ADD_COLUMN,
134-
'ADD COLUMN ' + pool.escapeId(columnName) + ' ' + newColumnDefinition.$toSQL()
136+
'ADD COLUMN ' + pool.escapeId(columnName) + ' ' + newColumnDefinition.$toSQL() + position
135137
));
136138
} else if (columnName !== oldColumnName) {
137139
operations.push(Operation.create(
@@ -147,6 +149,8 @@ class TableDefinition {
147149
[columnName]
148150
));
149151
}
152+
153+
lastColumnName = columnName;
150154
}
151155

152156
// Drop old columns (unless the column is being changed)

test/integration/MySQLPlus.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,11 @@ describe('MySQLPlus', function() {
454454
' `c` bigint(20) unsigned DEFAULT NULL,\n' +
455455
' `d` bigint(5) unsigned NOT NULL,\n' +
456456
' `ai` int(11) DEFAULT NULL,\n' +
457+
' `ci` char(1) DEFAULT NULL,\n' +
457458
' `eb` varchar(63) DEFAULT NULL,\n' +
458459
' `fb` char(1) DEFAULT \'a\',\n' +
459460
' `gc` bigint(5) unsigned NOT NULL,\n' +
460461
' `hc` varchar(255) NOT NULL,\n' +
461-
' `ci` char(1) DEFAULT NULL,\n' +
462462
' UNIQUE KEY `unique_fk_table_eb_fb` (`eb`,`fb`),\n' +
463463
' KEY `index_fk_table_c` (`c`),\n' +
464464
' KEY `index_fk_table_d` (`d`),\n' +
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const MySQLPlus = require('../../lib/MySQLPlus');
4+
5+
const config = require('../config');
6+
7+
const ColTypes = MySQLPlus.ColTypes;
8+
9+
describe('when adding new columns', function() {
10+
11+
const pool = MySQLPlus.createPool(config);
12+
const pool2 = MySQLPlus.createPool(config);
13+
14+
pool.defineTable('new_columns_positioning_test', {
15+
columns: {
16+
a: ColTypes.int(),
17+
b: ColTypes.int(),
18+
},
19+
});
20+
pool2.defineTable('new_columns_positioning_test', {
21+
columns: {
22+
preA: ColTypes.smallint(),
23+
a: ColTypes.int(),
24+
preB: ColTypes.smallint(),
25+
b: ColTypes.int(),
26+
c: ColTypes.smallint(),
27+
},
28+
});
29+
30+
before(done => {
31+
pool.sync(err => {
32+
if (err) {
33+
throw err;
34+
}
35+
36+
pool.end(done);
37+
});
38+
});
39+
40+
after(done => pool2.end(done));
41+
42+
it('should add the columns in the same position that they are defined in the JS columns object', done => {
43+
pool2.sync(err => {
44+
if (err) {
45+
throw err;
46+
}
47+
48+
pool2.query('SHOW CREATE TABLE `new_columns_positioning_test`', (err, rows) => {
49+
if (err) {
50+
throw err;
51+
}
52+
53+
rows[0]['Create Table'].should.equal(
54+
'CREATE TABLE `new_columns_positioning_test` (\n' +
55+
' `preA` smallint(6) DEFAULT NULL,\n' +
56+
' `a` int(11) DEFAULT NULL,\n' +
57+
' `preB` smallint(6) DEFAULT NULL,\n' +
58+
' `b` int(11) DEFAULT NULL,\n' +
59+
' `c` smallint(6) DEFAULT NULL\n' +
60+
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
61+
);
62+
done();
63+
});
64+
});
65+
});
66+
67+
});

test/unit/TableDefinition.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('TableDefinition', () => {
139139
sql: 'ALTER TABLE `' + existingTableName + '`\n' +
140140
' DROP PRIMARY KEY,\n' +
141141
' MODIFY COLUMN `id` int unsigned NOT NULL,\n' +
142-
' ADD COLUMN `newCol` tinyint',
142+
' ADD COLUMN `newCol` tinyint AFTER `id`',
143143
columns: undefined,
144144
},
145145
];

0 commit comments

Comments
 (0)