Skip to content

Commit 5e72c04

Browse files
committed
fix: Run "ADD COLUMN" operations before "MODIFY/CHANGE COLUMN" operations
Fixes a bug where a migration would fail if an existing column was modified and a new column was added just before the modified column.
1 parent 924b78e commit 5e72c04

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

lib/Operation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const Operation = {
1616
DROP_KEY: 11,
1717
DROP_COLUMN: 12,
1818
MODIFY_TABLE_OPTIONS: 13,
19-
MODIFY_COLUMN: 14,
19+
ADD_COLUMN: 14,
2020
CHANGE_COLUMN: 15,
21-
ADD_COLUMN: 16,
21+
MODIFY_COLUMN: 16,
2222
ADD_KEY: 17,
2323
},
2424

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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('adding a column before a modified column should work', function() {
10+
11+
const pool = MySQLPlus.createPool(config);
12+
const pool2 = MySQLPlus.createPool(config);
13+
14+
pool.defineTable('add_before_modify', {
15+
columns: {
16+
id: ColTypes.int().notNull().primaryKey(),
17+
modified: ColTypes.int().notNull(),
18+
},
19+
});
20+
21+
pool2.defineTable('add_before_modify', {
22+
columns: {
23+
id: ColTypes.int().notNull().primaryKey(),
24+
added: ColTypes.int().notNull(),
25+
modified: ColTypes.bigint().notNull(),
26+
},
27+
});
28+
29+
before(done => {
30+
pool.sync(err => {
31+
if (err) {
32+
throw err;
33+
}
34+
35+
pool.end(done);
36+
});
37+
});
38+
39+
after(done => {
40+
pool2.end(done);
41+
});
42+
43+
it('should not error', done => {
44+
pool2.sync(done);
45+
});
46+
47+
});
48+
49+
describe('adding a column before a changed column should work', function() {
50+
51+
const pool = MySQLPlus.createPool(config);
52+
const pool2 = MySQLPlus.createPool(config);
53+
54+
pool.defineTable('add_before_change', {
55+
columns: {
56+
id: ColTypes.int().notNull().primaryKey(),
57+
changed: ColTypes.int().notNull(),
58+
},
59+
});
60+
61+
pool2.defineTable('add_before_change', {
62+
columns: {
63+
id: ColTypes.int().notNull().primaryKey(),
64+
added: ColTypes.int().notNull(),
65+
changedName: ColTypes.int().notNull().oldName('changed'),
66+
},
67+
});
68+
69+
before(done => {
70+
pool.sync(err => {
71+
if (err) {
72+
throw err;
73+
}
74+
75+
pool.end(done);
76+
});
77+
});
78+
79+
after(done => {
80+
pool2.end(done);
81+
});
82+
83+
it('should not error', done => {
84+
pool2.sync(done);
85+
});
86+
87+
});

test/unit/TableDefinition.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ describe('TableDefinition', () => {
140140
sql:
141141
'ALTER TABLE `' + existingTableName + '`\n' +
142142
' DROP PRIMARY KEY,\n' +
143-
' MODIFY COLUMN `id` int unsigned NOT NULL FIRST,\n' +
144-
' ADD COLUMN `newCol` tinyint AFTER `id`',
143+
' ADD COLUMN `newCol` tinyint AFTER `id`,\n' +
144+
' MODIFY COLUMN `id` int unsigned NOT NULL FIRST',
145145
columns: undefined,
146146
},
147147
];

0 commit comments

Comments
 (0)