Skip to content

Commit

Permalink
feat: add name to MigrationInterface (fixes #3933 and fixes #2549) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardofalk authored and pleerock committed Oct 18, 2019
1 parent d744966 commit 4a73fde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/commands/MigrationGenerateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
* Gets contents of the migration file.
*/
protected static getTemplate(name: string, timestamp: number, upSqls: string[], downSqls: string[]): string {
const migrationName = `${camelCase(name, true)}${timestamp}`;

return `import {MigrationInterface, QueryRunner} from "typeorm";
export class ${camelCase(name, true)}${timestamp} implements MigrationInterface {
export class ${migrationName} implements MigrationInterface {
name = '${migrationName}'
public async up(queryRunner: QueryRunner): Promise<any> {
${upSqls.join(`
Expand Down
2 changes: 1 addition & 1 deletion src/migration/MigrationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class MigrationExecutor {
*/
protected getMigrations(): Migration[] {
const migrations = this.connection.migrations.map(migration => {
const migrationClassName = (migration.constructor as any).name;
const migrationClassName = migration.name || (migration.constructor as any).name;
const migrationTimestamp = parseInt(migrationClassName.substr(-13));
if (!migrationTimestamp)
throw new Error(`${migrationClassName} migration name is wrong. Migration class name should have a JavaScript timestamp appended.`);
Expand Down
4 changes: 4 additions & 0 deletions src/migration/MigrationInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {QueryRunner} from "../query-runner/QueryRunner";
* Migrations should implement this interface and all its methods.
*/
export interface MigrationInterface {
/**
* Optional migration name, defaults to class name.
*/
name?: string;

/**
* Run the migrations.
Expand Down

0 comments on commit 4a73fde

Please sign in to comment.