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OptionsReader} from \"../connection/ConnectionOptionsReader\";\nimport {CommandUtils} from \"./CommandUtils\";\nimport {Connection} from \"../connection/Connection\";\nimport {createConnection} from \"../index\";\nimport {MysqlDriver} from \"../driver/mysql/MysqlDriver\";\nimport {camelCase} from \"../util/StringUtils\";\nimport * as yargs from \"yargs\";\nconst chalk = require(\"chalk\");\n\n/**\n * Generates a new migration file with sql needs to be executed to update schema.\n */\nexport class MigrationGenerateCommand implements yargs.CommandModule {\n\n command = \"migration:generate\";\n describe = \"Generates a new migration file with sql needs to be executed to update schema.\";\n aliases = \"migrations:generate\";\n\n builder(args: yargs.Argv) {\n return args\n .option(\"c\", {\n alias: \"connection\",\n default: \"default\",\n describe: \"Name of the connection on which run a query.\"\n })\n .option(\"n\", {\n alias: \"name\",\n describe: \"Name of the migration class.\",\n demand: true\n })\n .option(\"d\", {\n alias: \"dir\",\n describe: \"Directory where migration should be created.\"\n })\n .option(\"f\", {\n alias: \"config\",\n default: \"ormconfig\",\n describe: \"Name of the file with connection configuration.\"\n });\n }\n\n async handler(args: yargs.Arguments) {\n if (args._[0] === \"migrations:generate\") {\n console.log(\"'migrations:generate' is deprecated, please use 'migration:generate' instead\");\n }\n\n const timestamp = new Date().getTime();\n const filename = timestamp + \"-\" + args.name + \".ts\";\n let directory = args.dir;\n\n // if directory is not set then try to open tsconfig and find default path there\n if (!directory) {\n try {\n const connectionOptionsReader = new ConnectionOptionsReader({\n root: process.cwd(),\n configName: args.config as any\n });\n const connectionOptions = await connectionOptionsReader.get(args.connection as any);\n directory = connectionOptions.cli ? connectionOptions.cli.migrationsDir : undefined;\n } catch (err) { }\n }\n\n let connection: Connection|undefined = undefined;\n try {\n const connectionOptionsReader = new ConnectionOptionsReader({\n root: process.cwd(),\n configName: args.config as any\n });\n const connectionOptions = await connectionOptionsReader.get(args.connection as any);\n Object.assign(connectionOptions, {\n synchronize: false,\n migrationsRun: false,\n dropSchema: false,\n logging: false\n });\n connection = await createConnection(connectionOptions);\n const sqlInMemory = await connection.driver.createSchemaBuilder().log();\n const upSqls: string[] = [], downSqls: string[] = [];\n\n // mysql is exceptional here because it uses ` character in to escape names in queries, that's why for mysql\n // we are using simple quoted string instead of template string syntax\n if (connection.driver instanceof MysqlDriver) {\n sqlInMemory.upQueries.forEach(upQuery => {\n upSqls.push(\" await queryRunner.query(\\\"\" + upQuery.query.replace(new RegExp(`\"`, \"g\"), `\\\\\"`) + \"\\\");\");\n });\n sqlInMemory.downQueries.forEach(downQuery => {\n downSqls.push(\" await queryRunner.query(\\\"\" + downQuery.query.replace(new RegExp(`\"`, \"g\"), `\\\\\"`) + \"\\\");\");\n });\n } else {\n sqlInMemory.upQueries.forEach(upQuery => {\n upSqls.push(\" await queryRunner.query(`\" + upQuery.query.replace(new RegExp(\"`\", \"g\"), \"\\\\`\") + \"`);\");\n });\n sqlInMemory.downQueries.forEach(downQuery => {\n downSqls.push(\" await queryRunner.query(`\" + downQuery.query.replace(new RegExp(\"`\", \"g\"), \"\\\\`\") + \"`);\");\n });\n }\n\n if (upSqls.length) {\n if (args.name) {\n const fileContent = MigrationGenerateCommand.getTemplate(args.name as any, timestamp, upSqls, downSqls.reverse());\n const path = process.cwd() + \"/\" + (directory ? (directory + \"/\") : \"\") + filename;\n await CommandUtils.createFile(path, fileContent);\n\n console.log(chalk.green(`Migration ${chalk.blue(path)} has been generated successfully.`));\n } else {\n console.log(chalk.yellow(\"Please specify migration name\"));\n }\n } else {\n console.log(chalk.yellow(`No changes in database schema were found - cannot generate a migration. To create a new empty migration use \"typeorm migration:create\" command`));\n }\n await connection.close();\n\n } catch (err) {\n if (connection) await (connection as Connection).close();\n\n console.log(chalk.black.bgRed(\"Error during migration generation:\"));\n console.error(err);\n process.exit(1);\n }\n }\n\n // -------------------------------------------------------------------------\n // Protected Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets contents of the migration file.\n */\n protected static getTemplate(name: string, timestamp: number, upSqls: string[], downSqls: string[]): string {\n return `import {MigrationInterface, QueryRunner} from \"typeorm\";\n\nexport class ${camelCase(name, true)}${timestamp} implements MigrationInterface {\n\n public async up(queryRunner: QueryRunner): Promise<any> {\n${upSqls.join(`\n`)}\n }\n\n public async down(queryRunner: QueryRunner): Promise<any> {\n${downSqls.join(`\n`)}\n }\n\n}\n`;\n }\n\n}\n"],"sourceRoot":".."}
|