* 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)
| 502 | * @returns {Promise} |
| 503 | */ |
| 504 | async renameColumn(tableName, attrNameBefore, attrNameAfter, options) { |
| 505 | options = options || {}; |
| 506 | const data = (await this.assertTableHasColumn(tableName, attrNameBefore, options))[attrNameBefore]; |
| 507 | |
| 508 | const _options = {}; |
| 509 | |
| 510 | _options[attrNameAfter] = { |
| 511 | attribute: attrNameAfter, |
| 512 | type: data.type, |
| 513 | allowNull: data.allowNull, |
| 514 | defaultValue: data.defaultValue |
| 515 | }; |
| 516 | |
| 517 | // fix: a not-null column cannot have null as default value |
| 518 | if (data.defaultValue === null && !data.allowNull) { |
| 519 | delete _options[attrNameAfter].defaultValue; |
| 520 | } |
| 521 | |
| 522 | const sql = this.queryGenerator.renameColumnQuery( |
| 523 | tableName, |
| 524 | attrNameBefore, |
| 525 | this.queryGenerator.attributesToSQL(_options) |
| 526 | ); |
| 527 | return await this.sequelize.query(sql, options); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Add an index to a column |