diff --git a/index.d.ts b/index.d.ts index 0f27af5..3622776 100644 --- a/index.d.ts +++ b/index.d.ts @@ -708,6 +708,16 @@ export declare class MigrationInterface { * @param newTableName */ renameTable(oldTableName: string, newTableName: string): void; + + /** + * change column + * @param tableName + * @param columnName + * @param options + */ + changeColumn(tableName: string, columnName: string, options: { + type: FieldType, + } & CreateColumnOptions): void; } export type MigrateAction = 'up' | 'down' | 'UP' | 'DOWN'; diff --git a/src/builder.js b/src/builder.js index 2539a35..5bb0bb9 100644 --- a/src/builder.js +++ b/src/builder.js @@ -523,6 +523,18 @@ class ManageSQLBuilder extends Builder { } } + changeColumn(options) { + _validate(options, { + tableName: 'required|string', + columnName: 'required|string', + }); + const columnOptions = { + ...options.options, + name: options.columnName, + }; + return _render('ALTER TABLE `${tableName}` MODIFY COLUMN ' + this.renderSingleColumn(columnOptions), { tableName: options.tableName }); + } + renameTable(options) { _validate(options, { oldName: 'required|string', diff --git a/src/migration.js b/src/migration.js index 2f878fb..9987eb4 100644 --- a/src/migration.js +++ b/src/migration.js @@ -337,6 +337,19 @@ function _initMigration(file, queries = {}) { }, ...baseAttr }); + Object.defineProperty(migration, 'changeColumn', { + value: function (tableName, columnName, options) { + const builder = new ManageSQLBuilder({ + operator: 'change', + target: 'column', + tableName, + columnName, + options + }); + queries[file].push({ sql: builder.sql, values: builder.values }); + }, ...baseAttr + }); + return migration; }