-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: output Javascript Migrations instead of TypeScript (#7294)
* docs / test: Added tests and documentation for Feature 7253 - Migrations Javascript output * Change in the test * test: Re-arranged the tests to move them to the core tests directory * tests: Adjusted Tests a bit * tests - renamed tests to follow the other functional tests naming * tests - renamed tests to follow the other functional tests naming * tests - Fixed issues with the test connections setup * tests - Removed unnecesary restore
- Loading branch information
Showing
7 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { PrimaryGeneratedColumn, Entity, Column, CreateDateColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id?: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@CreateDateColumn() | ||
readonly createdAt?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import sinon from "sinon"; | ||
import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../../src"; | ||
import { | ||
setupTestingConnections, | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases | ||
} from "../../utils/test-utils"; | ||
import { CommandUtils } from "../../../src/commands/CommandUtils"; | ||
import { MigrationCreateCommand } from "../../../src/commands/MigrationCreateCommand"; | ||
import { Post } from "./entity/Post"; | ||
import { resultsTemplates } from "./templates/result-templates-create"; | ||
|
||
describe("commands - migration create", () => { | ||
let connectionOptions: ConnectionOptions[]; | ||
let createFileStub: sinon.SinonStub; | ||
let timerStub: sinon.SinonFakeTimers; | ||
let getConnectionOptionsStub: sinon.SinonStub; | ||
let migrationCreateCommand: MigrationCreateCommand; | ||
let connectionOptionsReader: ConnectionOptionsReader; | ||
let baseConnectionOptions: ConnectionOptions; | ||
|
||
const enabledDrivers = [ | ||
"postgres", | ||
"mssql", | ||
"mysql", | ||
"mariadb", | ||
"sqlite", | ||
"better-sqlite3", | ||
"oracle", | ||
"cockroachdb" | ||
] as DatabaseType[]; | ||
|
||
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory` | ||
const testHandlerArgs = (options: Record<string, any>) => ({ | ||
"$0": "test", | ||
"_": ["test"], | ||
"name": "test-migration", | ||
"dir": "test-directory", | ||
...options | ||
}); | ||
|
||
before(async () => { | ||
// clean out db from any prior tests in case previous state impacts the generated migrations | ||
const connections = await createTestingConnections({ | ||
entities: [], | ||
enabledDrivers | ||
}); | ||
await reloadTestingDatabases(connections); | ||
await closeTestingConnections(connections); | ||
|
||
connectionOptions = setupTestingConnections({ | ||
entities: [Post], | ||
enabledDrivers | ||
}); | ||
connectionOptionsReader = new ConnectionOptionsReader(); | ||
migrationCreateCommand = new MigrationCreateCommand(); | ||
createFileStub = sinon.stub(CommandUtils, "createFile"); | ||
|
||
timerStub = sinon.useFakeTimers(1610975184784); | ||
}); | ||
|
||
after(async () => { | ||
timerStub.restore(); | ||
createFileStub.restore(); | ||
}); | ||
|
||
afterEach(async () => { | ||
getConnectionOptionsStub.restore(); | ||
}); | ||
|
||
it("should write regular empty migration file when no option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
|
||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Post] | ||
}); | ||
|
||
await migrationCreateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name | ||
})); | ||
|
||
// compare against control test strings in results-templates.ts | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.ts/), | ||
sinon.match(resultsTemplates.control) | ||
); | ||
|
||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
|
||
it("should write Javascript empty migration file when option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
|
||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Post] | ||
}); | ||
|
||
await migrationCreateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name, | ||
"outputJs": true | ||
})); | ||
|
||
// compare against control test strings in results-templates.ts | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.js/), | ||
sinon.match(resultsTemplates.javascript) | ||
); | ||
|
||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import sinon from "sinon"; | ||
import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../../src"; | ||
import { | ||
setupTestingConnections, | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases | ||
} from "../../utils/test-utils"; | ||
import { CommandUtils } from "../../../src/commands/CommandUtils"; | ||
import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerateCommand"; | ||
import { Post } from "./entity/Post"; | ||
import { resultsTemplates } from "./templates/result-templates-generate"; | ||
|
||
describe("commands - migration generate", () => { | ||
let connectionOptions: ConnectionOptions[]; | ||
let createFileStub: sinon.SinonStub; | ||
let timerStub: sinon.SinonFakeTimers; | ||
let getConnectionOptionsStub: sinon.SinonStub; | ||
let migrationGenerateCommand: MigrationGenerateCommand; | ||
let connectionOptionsReader: ConnectionOptionsReader; | ||
let baseConnectionOptions: ConnectionOptions; | ||
|
||
const enabledDrivers = [ | ||
"mysql", | ||
] as DatabaseType[]; | ||
|
||
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory` | ||
const testHandlerArgs = (options: Record<string, any>) => ({ | ||
"$0": "test", | ||
"_": ["test"], | ||
"name": "test-migration", | ||
"dir": "test-directory", | ||
...options | ||
}); | ||
|
||
before(async () => { | ||
// clean out db from any prior tests in case previous state impacts the generated migrations | ||
const connections = await createTestingConnections({ | ||
entities: [], | ||
enabledDrivers | ||
}); | ||
await reloadTestingDatabases(connections); | ||
await closeTestingConnections(connections); | ||
|
||
connectionOptions = setupTestingConnections({ | ||
entities: [Post], | ||
enabledDrivers | ||
}); | ||
connectionOptionsReader = new ConnectionOptionsReader(); | ||
migrationGenerateCommand = new MigrationGenerateCommand(); | ||
createFileStub = sinon.stub(CommandUtils, "createFile"); | ||
|
||
timerStub = sinon.useFakeTimers(1610975184784); | ||
}); | ||
|
||
after(async () => { | ||
timerStub.restore(); | ||
createFileStub.restore(); | ||
}); | ||
|
||
it("writes regular migration file when no option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
|
||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Post] | ||
}); | ||
|
||
await migrationGenerateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name | ||
})); | ||
|
||
// compare against control test strings in results-templates.ts | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.ts/), | ||
sinon.match(resultsTemplates.control) | ||
); | ||
|
||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
|
||
it("writes Javascript printed file when option is passed", async () => { | ||
for (const connectionOption of connectionOptions) { | ||
createFileStub.resetHistory(); | ||
|
||
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string); | ||
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({ | ||
...baseConnectionOptions, | ||
entities: [Post] | ||
}); | ||
|
||
await migrationGenerateCommand.handler(testHandlerArgs({ | ||
"connection": connectionOption.name, | ||
"outputJs": true | ||
})); | ||
|
||
// compare against "pretty" test strings in results-templates.ts | ||
sinon.assert.calledWith( | ||
createFileStub, | ||
sinon.match(/test-directory.*test-migration.js/), | ||
sinon.match(resultsTemplates.javascript) | ||
); | ||
getConnectionOptionsStub.restore(); | ||
} | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
test/functional/commands/templates/result-templates-create.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const resultsTemplates: Record<string, any> = { | ||
control: `import {MigrationInterface, QueryRunner} from "typeorm"; | ||
export class testMigration1610975184784 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
} | ||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
} | ||
}`, | ||
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm"); | ||
module.exports = class testMigration1610975184784 { | ||
async up(queryRunner) { | ||
} | ||
async down(queryRunner) { | ||
} | ||
}` | ||
}; |
29 changes: 29 additions & 0 deletions
29
test/functional/commands/templates/result-templates-generate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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\`"); | ||
} | ||
}` | ||
}; |