mirror of
https://github.com/knex/knex.git
synced 2025-08-09 09:11:54 +00:00
Merge pull request #1326 from wubzz/bugfix/renameCol_drops_default_value
.renameColumn should not drop defaultValue or nullable state.
This commit is contained in:
commit
bb9663f2b3
@ -77,8 +77,17 @@ assign(TableCompiler_MySQL.prototype, {
|
|||||||
if (!refs.length) { return; }
|
if (!refs.length) { return; }
|
||||||
return compiler.dropFKRefs(runner, refs);
|
return compiler.dropFKRefs(runner, refs);
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;
|
||||||
|
|
||||||
|
if(String(column.Null).toUpperCase() !== 'YES') {
|
||||||
|
sql += ` NOT NULL`
|
||||||
|
}
|
||||||
|
if(column.Default !== void 0 && column.Default !== null) {
|
||||||
|
sql += ` DEFAULT '${column.Default}'`
|
||||||
|
}
|
||||||
|
|
||||||
return runner.query({
|
return runner.query({
|
||||||
sql: 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type
|
sql: sql
|
||||||
});
|
});
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
if (!refs.length) { return; }
|
if (!refs.length) { return; }
|
||||||
|
@ -438,6 +438,33 @@ module.exports = function(knex) {
|
|||||||
tbl.renameColumn('id', 'id_new');
|
tbl.renameColumn('id', 'id_new');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('#933 - .renameColumn should not drop null or default value', function() {
|
||||||
|
return knex.transaction(function (tr) {
|
||||||
|
var getColInfo = function() { return tr('renameColTest').columnInfo()};
|
||||||
|
return tr.schema.dropTableIfExists('renameColTest')
|
||||||
|
.createTable('renameColTest', function (table) {
|
||||||
|
table.integer('colnameint').defaultTo(1);
|
||||||
|
table.string('colnamestring').defaultTo('knex').notNullable();
|
||||||
|
})
|
||||||
|
.then(getColInfo)
|
||||||
|
.then(function (colInfo) {
|
||||||
|
expect(String(colInfo.colnameint.defaultValue)).to.contain('1');
|
||||||
|
expect(colInfo.colnamestring.defaultValue).to.contain('knex'); //Using contain because of different response per dialect. IE mysql 'knex', postgres 'knex::character varying'
|
||||||
|
expect(colInfo.colnamestring.nullable).to.equal(false);
|
||||||
|
return tr.schema.table('renameColTest', function (table) {
|
||||||
|
table.renameColumn('colnameint', 'colnameintchanged');
|
||||||
|
table.renameColumn('colnamestring', 'colnamestringchanged');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(getColInfo)
|
||||||
|
.then(function (columnInfo) {
|
||||||
|
expect(String(columnInfo.colnameintchanged.defaultValue)).to.contain('1');
|
||||||
|
expect(columnInfo.colnamestringchanged.defaultValue).to.contain('knex');
|
||||||
|
expect(columnInfo.colnamestringchanged.nullable).to.equal(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user