test: fix migration-generate test suite (#10361)

* test: fix migration-generate test suite

WIP

Closes: #10360

* fixed mssql test hang;
added option to disable process exit;

---------

Co-authored-by: Alex Messer <dmzt08@gmail.com>
This commit is contained in:
Dinko Osrecki 2023-12-29 11:02:44 +01:00 committed by GitHub
parent 45e31cc57a
commit ccb9aff89a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 292 additions and 49 deletions

View File

@ -201,7 +201,9 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
)} has been generated successfully.`,
),
)
process.exit(0)
if (args.exitProcess !== false) {
process.exit(0)
}
}
} catch (err) {
PlatformTools.logCmdErr("Error during migration generation:", err)

View File

@ -20,7 +20,6 @@ describe("commands - migration generate", () => {
let connectionOptions: DataSourceOptions[]
let createFileStub: sinon.SinonStub
let loadDataSourceStub: sinon.SinonStub
let timerStub: sinon.SinonFakeTimers
let getConnectionOptionsStub: sinon.SinonStub
let migrationGenerateCommand: MigrationGenerateCommand
let connectionOptionsReader: ConnectionOptionsReader
@ -62,12 +61,9 @@ describe("commands - migration generate", () => {
migrationGenerateCommand = new MigrationGenerateCommand()
createFileStub = sinon.stub(CommandUtils, "createFile")
loadDataSourceStub = sinon.stub(CommandUtils, "loadDataSource")
timerStub = sinon.useFakeTimers(1610975184784)
})
after(async () => {
timerStub.restore()
createFileStub.restore()
loadDataSourceStub.restore()
})
@ -91,6 +87,8 @@ describe("commands - migration generate", () => {
await migrationGenerateCommand.handler(
testHandlerArgs({
dataSource: "dummy-path",
timestamp: "1610975184784",
exitProcess: false,
}),
)
@ -98,7 +96,7 @@ describe("commands - migration generate", () => {
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.ts/),
sinon.match(resultsTemplates.control),
sinon.match(resultsTemplates[connectionOption.type]?.control),
)
getConnectionOptionsStub.restore()
@ -124,7 +122,9 @@ describe("commands - migration generate", () => {
await migrationGenerateCommand.handler(
testHandlerArgs({
dataSource: "dummy-path",
timestamp: "1610975184784",
outputJs: true,
exitProcess: false,
}),
)
@ -132,7 +132,9 @@ describe("commands - migration generate", () => {
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.js/),
sinon.match(resultsTemplates.javascript),
sinon.match(
resultsTemplates[connectionOption.type]?.javascript,
),
)
getConnectionOptionsStub.restore()
@ -159,6 +161,7 @@ describe("commands - migration generate", () => {
testHandlerArgs({
dataSource: "dummy-path",
timestamp: "1641163894670",
exitProcess: false,
}),
)
@ -166,7 +169,7 @@ describe("commands - migration generate", () => {
sinon.assert.calledWith(
createFileStub,
sinon.match("test-directory/1641163894670-test-migration.ts"),
sinon.match(resultsTemplates.timestamp),
sinon.match(resultsTemplates[connectionOption.type]?.timestamp),
)
getConnectionOptionsStub.restore()

View File

@ -0,0 +1,49 @@
export const cockroachdb: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE SEQUENCE "post_id_seq"\`);
await queryRunner.query(\`CREATE TABLE "post" ("id" INT DEFAULT nextval('"post_id_seq"') NOT NULL, "title" varchar NOT NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
await queryRunner.query(\`DROP SEQUENCE "post_id_seq"\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE SEQUENCE "post_id_seq"\`);
await queryRunner.query(\`CREATE TABLE "post" ("id" INT DEFAULT nextval('"post_id_seq"') NOT NULL, "title" varchar NOT NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
async down(queryRunner) {
await queryRunner.query(\`DROP TABLE "post"\`);
await queryRunner.query(\`DROP SEQUENCE "post_id_seq"\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE SEQUENCE "post_id_seq"\`);
await queryRunner.query(\`CREATE TABLE "post" ("id" INT DEFAULT nextval('"post_id_seq"') NOT NULL, "title" varchar NOT NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
await queryRunner.query(\`DROP SEQUENCE "post_id_seq"\`);
}
}`,
}

View File

@ -0,0 +1,43 @@
export const mssql: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" int NOT NULL IDENTITY(1,1), "title" nvarchar(255) NOT NULL, "createdAt" datetime2 NOT NULL CONSTRAINT "DF_fb91bea2d37140a877b775e6b2a" DEFAULT getdate(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE "post" ("id" int NOT NULL IDENTITY(1,1), "title" nvarchar(255) NOT NULL, "createdAt" datetime2 NOT NULL CONSTRAINT "DF_fb91bea2d37140a877b775e6b2a" DEFAULT getdate(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
async down(queryRunner) {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" int NOT NULL IDENTITY(1,1), "title" nvarchar(255) NOT NULL, "createdAt" datetime2 NOT NULL CONSTRAINT "DF_fb91bea2d37140a877b775e6b2a" DEFAULT getdate(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
}

View File

@ -0,0 +1,43 @@
export const mysql: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
}

View File

@ -0,0 +1,43 @@
export const oracle: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" number GENERATED BY DEFAULT AS IDENTITY, "title" varchar2(255) NOT NULL, "createdAt" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE "post" ("id" number GENERATED BY DEFAULT AS IDENTITY, "title" varchar2(255) NOT NULL, "createdAt" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
async down(queryRunner) {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" number GENERATED BY DEFAULT AS IDENTITY, "title" varchar2(255) NOT NULL, "createdAt" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
}

View File

@ -0,0 +1,43 @@
export const postgres: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE "post" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
async down(queryRunner) {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_be5fda3aac270b134ff9c21cdee" PRIMARY KEY ("id"))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
}

View File

@ -0,0 +1,43 @@
export const sqlite: Record<string, string> = {
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1610975184784 implements MigrationInterface {
name = 'TestMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class TestMigration1610975184784 {
name = 'TestMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))\`);
}
async down(queryRunner) {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
name = 'TestMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')))\`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`DROP TABLE "post"\`);
}
}`,
}

View File

@ -1,43 +1,17 @@
import { mysql } from "./generate/mysql"
import { postgres } from "./generate/postgres"
import { sqlite } from "./generate/sqlite"
import { oracle } from "./generate/oracle"
import { cockroachdb } from "./generate/cockroachdb"
import { mssql } from "./generate/mssql.js"
export const resultsTemplates: Record<string, any> = {
control: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1610975184784 implements MigrationInterface {
name = 'testMigration1610975184784'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class testMigration1610975184784 {
name = 'testMigration1610975184784'
async up(queryRunner) {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
timestamp: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1641163894670 implements MigrationInterface {
name = 'testMigration1641163894670'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(\`CREATE TABLE \\\`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 \\\`post\\\`\`);
}
}`,
mysql,
mariadb: mysql,
mssql,
sqlite,
"better-sqlite3": sqlite,
postgres,
oracle,
cockroachdb,
}