Skip to content

Commit

Permalink
fix: always generate migrations with template string literals (#7971)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
imnotjames committed Jul 30, 2021
1 parent 76e7ed9 commit e9c2af6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
25 changes: 6 additions & 19 deletions 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";

Expand Down Expand Up @@ -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();
}
Expand Down
Expand Up @@ -5,11 +5,11 @@ export class testMigration1610975184784 implements MigrationInterface {
name = 'testMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query("DROP TABLE \`test\`.\`post\`");
await queryRunner.query(\`DROP TABLE \\\`test\\\`.\\\`post\\\`\`);
}
}`,
Expand All @@ -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\\\`\`);
}
}`
};
24 changes: 12 additions & 12 deletions test/github-issues/4415/results-templates.ts
Expand Up @@ -75,24 +75,24 @@ export const resultsTemplates: Record<string, any> = {

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
`
]
Expand Down

0 comments on commit e9c2af6

Please sign in to comment.