From 29cf1d6266f64551016b0756487e0f3e67ee8a92 Mon Sep 17 00:00:00 2001 From: James Ward Date: Wed, 28 Jul 2021 15:05:50 -0400 Subject: [PATCH] fix: always generate migrations with template string literals because we escape the backticsk in template string literals we can use them for mysql. this is important because standard strings cannot cross multiple lines when you do `pretty` + migrations --- src/commands/MigrationGenerateCommand.ts | 25 +++++-------------- .../templates/result-templates-generate.ts | 8 +++--- test/github-issues/4415/results-templates.ts | 24 +++++++++--------- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/commands/MigrationGenerateCommand.ts b/src/commands/MigrationGenerateCommand.ts index b8dec27fa9a..fb5f8f7743e 100644 --- a/src/commands/MigrationGenerateCommand.ts +++ b/src/commands/MigrationGenerateCommand.ts @@ -1,10 +1,8 @@ import {ConnectionOptionsReader} from "../connection/ConnectionOptionsReader"; import {CommandUtils} from "./CommandUtils"; import {createConnection} from "../globals"; -import {MysqlDriver} from "../driver/mysql/MysqlDriver"; import {camelCase} from "../util/StringUtils"; import * as yargs from "yargs"; -import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver"; import chalk from "chalk"; import { format } from "@sqltools/formatter/lib/sqlFormatter"; @@ -115,23 +113,12 @@ export class MigrationGenerateCommand implements yargs.CommandModule { }); } - // mysql is exceptional here because it uses ` character in to escape names in queries, that's why for mysql - // we are using simple quoted string instead of template string syntax - if (connection.driver instanceof MysqlDriver || connection.driver instanceof AuroraDataApiDriver) { - sqlInMemory.upQueries.forEach(upQuery => { - upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");"); - }); - sqlInMemory.downQueries.forEach(downQuery => { - downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");"); - }); - } else { - sqlInMemory.upQueries.forEach(upQuery => { - upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");"); - }); - sqlInMemory.downQueries.forEach(downQuery => { - downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");"); - }); - } + sqlInMemory.upQueries.forEach(upQuery => { + upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");"); + }); + sqlInMemory.downQueries.forEach(downQuery => { + downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");"); + }); } finally { await connection.close(); } diff --git a/test/functional/commands/templates/result-templates-generate.ts b/test/functional/commands/templates/result-templates-generate.ts index 790bbe11fb4..09de8a5d917 100644 --- a/test/functional/commands/templates/result-templates-generate.ts +++ b/test/functional/commands/templates/result-templates-generate.ts @@ -5,11 +5,11 @@ export class testMigration1610975184784 implements MigrationInterface { name = 'testMigration1610975184784' public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query("CREATE TABLE \`test\`.\`post\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB"); + await queryRunner.query(\`CREATE TABLE \\\`test\\\`.\\\`post\\\` (\\\`id\\\` int NOT NULL AUTO_INCREMENT, \\\`title\\\` varchar(255) NOT NULL, \\\`createdAt\\\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\\\`id\\\`)) ENGINE=InnoDB\`); } public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query("DROP TABLE \`test\`.\`post\`"); + await queryRunner.query(\`DROP TABLE \\\`test\\\`.\\\`post\\\`\`); } }`, @@ -19,11 +19,11 @@ module.exports = class testMigration1610975184784 { name = 'testMigration1610975184784' async up(queryRunner) { - await queryRunner.query("CREATE TABLE \`test\`.\`post\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB"); + await queryRunner.query(\`CREATE TABLE \\\`test\\\`.\\\`post\\\` (\\\`id\\\` int NOT NULL AUTO_INCREMENT, \\\`title\\\` varchar(255) NOT NULL, \\\`createdAt\\\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\\\`id\\\`)) ENGINE=InnoDB\`); } async down(queryRunner) { - await queryRunner.query("DROP TABLE \`test\`.\`post\`"); + await queryRunner.query(\`DROP TABLE \\\`test\\\`.\\\`post\\\`\`); } }` }; diff --git a/test/github-issues/4415/results-templates.ts b/test/github-issues/4415/results-templates.ts index 0b840b25a69..8baa62b27e3 100644 --- a/test/github-issues/4415/results-templates.ts +++ b/test/github-issues/4415/results-templates.ts @@ -75,24 +75,24 @@ export const resultsTemplates: Record = { mysql: { control: [ - `CREATE TABLE \`test\`.\`post\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, - `CREATE TABLE \`test\`.\`username\` (\`username\` varchar(255) NOT NULL, \`email\` varchar(255) NOT NULL, \`something\` varchar(255) NOT NULL, PRIMARY KEY (\`username\`)) ENGINE=InnoDB` + `CREATE TABLE \\\`test\\\`.\\\`post\\\` (\\\`id\\\` int NOT NULL AUTO_INCREMENT, \\\`title\\\` varchar(255) NOT NULL, \\\`createdAt\\\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\\\`id\\\`)) ENGINE=InnoDB`, + `CREATE TABLE \\\`test\\\`.\`username\\\` (\\\`username\\\` varchar(255) NOT NULL, \\\`email\\\` varchar(255) NOT NULL, \\\`something\\\` varchar(255) NOT NULL, PRIMARY KEY (\\\`username\\\`)) ENGINE=InnoDB` ], pretty: [ ` - CREATE TABLE \`test\`.\`post\` ( - \`id\` int NOT NULL AUTO_INCREMENT, - \`title\` varchar(255) NOT NULL, - \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), - PRIMARY KEY (\`id\`) + CREATE TABLE \\\`test\\\`.\\\`post\\\` ( + \\\`id\\\` int NOT NULL AUTO_INCREMENT, + \\\`title\\\` varchar(255) NOT NULL, + \\\`createdAt\\\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + PRIMARY KEY (\\\`id\\\`) ) ENGINE = InnoDB `, ` - CREATE TABLE \`test\`.\`username\` ( - \`username\` varchar(255) NOT NULL, - \`email\` varchar(255) NOT NULL, - \`something\` varchar(255) NOT NULL, - PRIMARY KEY (\`username\`) + CREATE TABLE \\\`test\\\`.\\\`username\\\` ( + \\\`username\\\` varchar(255) NOT NULL, + \\\`email\\\` varchar(255) NOT NULL, + \\\`something\\\` varchar(255) NOT NULL, + PRIMARY KEY (\\\`username\\\`) ) ENGINE = InnoDB ` ]