* Rename a column * * @param {string} tableName Table name whose column to rename * @param {string} attrNameBefore Current column name * @param {string} attrNameAfter New column name * @param {object} [options] Query option * * @returns {Promise}
(tableName, attrNameBefore, attrNameAfter, options)
| 524 | * @returns {Promise} |
| 525 | */ |
| 526 | async renameColumn(tableName, attrNameBefore, attrNameAfter, options) { |
| 527 | options = options || {}; |
| 528 | const data = (await this.assertTableHasColumn(tableName, attrNameBefore, options))[attrNameBefore]; |
| 529 | |
| 530 | const _options = {}; |
| 531 | |
| 532 | _options[attrNameAfter] = { |
| 533 | attribute: attrNameAfter, |
| 534 | type: data.type, |
| 535 | allowNull: data.allowNull, |
| 536 | defaultValue: data.defaultValue |
| 537 | }; |
| 538 | |
| 539 | // fix: a not-null column cannot have null as default value |
| 540 | if (data.defaultValue === null && !data.allowNull) { |
| 541 | delete _options[attrNameAfter].defaultValue; |
| 542 | } |
| 543 | |
| 544 | const sql = this.queryGenerator.renameColumnQuery( |
| 545 | tableName, |
| 546 | attrNameBefore, |
| 547 | this.queryGenerator.attributesToSQL(_options) |
| 548 | ); |
| 549 | return await this.sequelize.query(sql, options); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Add an index to a column |