2021-10-27 03:47:36 +13:00
|
|
|
require("../../tests/utilities/dbConfig")
|
|
|
|
|
2022-01-27 23:40:31 +13:00
|
|
|
const { runMigrations, getMigrationsDoc } = require("../index")
|
2022-03-30 04:03:44 +13:00
|
|
|
const { getDB } = require("../../db")
|
2021-10-27 03:47:36 +13:00
|
|
|
const {
|
|
|
|
StaticDatabases,
|
|
|
|
} = require("../../db/utils")
|
|
|
|
|
|
|
|
Date.now = jest.fn(() => 1487076708000)
|
|
|
|
let db
|
|
|
|
|
|
|
|
describe("migrations", () => {
|
|
|
|
|
|
|
|
const migrationFunction = jest.fn()
|
|
|
|
|
2022-01-27 23:40:31 +13:00
|
|
|
const MIGRATIONS = [{
|
|
|
|
type: "global",
|
|
|
|
name: "test",
|
|
|
|
fn: migrationFunction
|
|
|
|
}]
|
|
|
|
|
2021-10-27 03:47:36 +13:00
|
|
|
beforeEach(() => {
|
2022-03-30 04:03:44 +13:00
|
|
|
db = getDB(StaticDatabases.GLOBAL.name)
|
2021-10-27 03:47:36 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
await db.destroy()
|
|
|
|
})
|
|
|
|
|
2022-01-27 23:40:31 +13:00
|
|
|
const migrate = () => {
|
2022-03-30 04:03:44 +13:00
|
|
|
return runMigrations(MIGRATIONS)
|
2021-10-27 03:47:36 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
it("should run a new migration", async () => {
|
2022-01-27 23:40:31 +13:00
|
|
|
await migrate()
|
2021-10-27 03:47:36 +13:00
|
|
|
expect(migrationFunction).toHaveBeenCalled()
|
2022-01-27 23:40:31 +13:00
|
|
|
const doc = await getMigrationsDoc(db)
|
|
|
|
expect(doc.test).toBeDefined()
|
2021-10-27 03:47:36 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should match snapshot", async () => {
|
2022-01-27 23:40:31 +13:00
|
|
|
await migrate()
|
2021-10-27 03:47:36 +13:00
|
|
|
const doc = await getMigrationsDoc(db)
|
|
|
|
expect(doc).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should skip a previously run migration", async () => {
|
2022-01-27 23:40:31 +13:00
|
|
|
await migrate()
|
|
|
|
const previousMigrationTime = await getMigrationsDoc(db).test
|
|
|
|
await migrate()
|
|
|
|
const currentMigrationTime = await getMigrationsDoc(db).test
|
2021-10-27 03:47:36 +13:00
|
|
|
expect(migrationFunction).toHaveBeenCalledTimes(1)
|
2022-01-27 23:40:31 +13:00
|
|
|
expect(currentMigrationTime).toBe(previousMigrationTime)
|
2021-10-27 03:47:36 +13:00
|
|
|
})
|
|
|
|
})
|