-
-
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: add fake migrations running and reverting (#8976)
* feat: add fake migrations running and reverting Added a cli option to fake-run or fake-revert a migration, adding to the executed migrations table, but not actually running it. This feature is useful for when migrations are added after the fact or for interoperability between applications which are desired to each keep a consistent migration history Closes: #6195 * changed enabled drivers in test * added docs to the property * fixed lint issue Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com> Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
- Loading branch information
1 parent
5e5abbd
commit 340ab67
Showing
7 changed files
with
203 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
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
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
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
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
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,115 @@ | ||
import "reflect-metadata" | ||
import { expect } from "chai" | ||
|
||
import { DataSource, QueryRunner, Table } from "../../../src" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
|
||
export const testTableName = "test_table" | ||
export const testColumnName = "test_column" | ||
export const nonExistentColumnName = "nonexistent_column" | ||
|
||
const createTestTable = async (queryRunner: QueryRunner) => { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: testTableName, | ||
columns: [ | ||
{ | ||
name: "id", | ||
type: "integer", | ||
isPrimary: true, | ||
}, | ||
{ | ||
name: testColumnName, | ||
type: "varchar", | ||
}, | ||
], | ||
}), | ||
) | ||
} | ||
|
||
describe("github issues > #6195 feature: fake migrations for existing tables", () => { | ||
let dataSources: DataSource[] | ||
|
||
before(async () => { | ||
dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: false, | ||
dropSchema: false, | ||
migrations: [__dirname + "/migrations/**/*{.ts,.js}"], | ||
// logging: true, | ||
}) | ||
|
||
await reloadTestingDatabases(dataSources) | ||
|
||
for (const dataSource of dataSources) { | ||
const queryRunner = dataSource.createQueryRunner() | ||
await dataSource.showMigrations() // To initialize migrations table | ||
await createTestTable(queryRunner) | ||
await queryRunner.release() | ||
} | ||
}) | ||
|
||
after(async () => { | ||
await closeTestingConnections(dataSources) | ||
}) | ||
|
||
describe("fake run tests", () => { | ||
it("should fail for duplicate column", async () => { | ||
for (const dataSource of dataSources) { | ||
if (dataSource.options.type === "mongodb") return | ||
await expect( | ||
dataSource.runMigrations({ transaction: "all" }), | ||
).to.be.rejectedWith(Error) | ||
} | ||
}) | ||
|
||
it("should not fail for duplicate column when run with the fake option", async () => { | ||
for (const dataSource of dataSources) { | ||
if (dataSource.options.type === "mongodb") return | ||
await expect( | ||
dataSource.runMigrations({ | ||
transaction: "all", | ||
fake: true, | ||
}), | ||
).not.to.be.rejectedWith(Error) | ||
} | ||
}) | ||
}) | ||
|
||
describe("fake rollback tests", () => { | ||
before(async () => { | ||
for (const dataSource of dataSources) { | ||
if (dataSource.options.type === "mongodb") return | ||
await dataSource.runMigrations({ | ||
transaction: "all", | ||
fake: true, | ||
}) | ||
} | ||
}) | ||
|
||
it("should fail for non-existent column", async () => { | ||
for (const dataSource of dataSources) { | ||
if (dataSource.options.type === "mongodb") return | ||
await expect( | ||
dataSource.undoLastMigration({ transaction: "all" }), | ||
).to.be.rejectedWith(Error) | ||
} | ||
}) | ||
|
||
it("should not fail for non-existent column when run with the fake option", async () => { | ||
for (const dataSource of dataSources) { | ||
if (dataSource.options.type === "mongodb") return | ||
await expect( | ||
dataSource.undoLastMigration({ | ||
transaction: "all", | ||
fake: true, | ||
}), | ||
).not.to.be.rejectedWith(Error) | ||
} | ||
}) | ||
}) | ||
}) |
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,24 @@ | ||
import { MigrationInterface, QueryRunner, TableColumn } from "../../../../src" | ||
import { | ||
testColumnName, | ||
testTableName, | ||
nonExistentColumnName, | ||
} from "../issue-6195" | ||
|
||
export class MigrationToFakeRun implements MigrationInterface { | ||
name = "MigrationToFakeRun" + Date.now() | ||
|
||
async up(queryRunner: QueryRunner) { | ||
await queryRunner.addColumn( | ||
testTableName, | ||
new TableColumn({ | ||
name: testColumnName, | ||
type: "varchar", | ||
}), | ||
) | ||
} | ||
|
||
async down(queryRunner: QueryRunner) { | ||
await queryRunner.dropColumn(testTableName, nonExistentColumnName) | ||
} | ||
} |