Skip to content

Commit 422f910

Browse files
committed
feat(migration): add renameTable method for table renaming functionality
1 parent e8e26cc commit 422f910

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎index.d.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,26 @@ export declare class MigrationInterface {
688688
*/
689689
dropForeignKey(tableName: string, foreign_key: string): void;
690690

691+
/**
692+
* insert data into table
693+
* @param table
694+
* @param data
695+
*/
691696
insertData(table: string, data: any[]): void;
692697

698+
/**
699+
* execute raw sql
700+
* @param sql
701+
* @param values
702+
*/
693703
raw(sql: string, values: any[]): void;
704+
705+
/**
706+
* rename table
707+
* @param oldTableName
708+
* @param newTableName
709+
*/
710+
renameTable(oldTableName: string, newTableName: string): void;
694711
}
695712

696713
export type MigrateAction = 'up' | 'down' | 'UP' | 'DOWN';

‎src/builder.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ class ManageSQLBuilder extends Builder {
523523
}
524524
}
525525

526+
renameTable(options) {
527+
_validate(options, {
528+
oldName: 'required|string',
529+
newName: 'required|string',
530+
});
531+
return _render('RENAME TABLE `${oldName}` TO `${newName}`', options);
532+
}
533+
526534
/**
527535
* @param {import('./migration').ManageBuilderOptions} options
528536
*/

‎src/migration.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ function _initMigration(file, queries = {}) {
325325
}, ...baseAttr
326326
});
327327

328+
Object.defineProperty(migration, 'renameTable', {
329+
value: function (oldTableName, newTableName) {
330+
const builder = new ManageSQLBuilder({
331+
operator: 'rename',
332+
target: 'table',
333+
oldName: oldTableName,
334+
newName: newTableName
335+
});
336+
queries[file].push({ sql: builder.sql, values: builder.values });
337+
}, ...baseAttr
338+
});
339+
328340
return migration;
329341
}
330342

0 commit comments

Comments
 (0)