|
| 1 | +/* eslint-disable jest/require-top-level-describe */ |
| 2 | +import { existsSync, rmdirSync, rmSync } from 'fs' |
| 3 | +import path from 'path' |
| 4 | +import { buildConfig, getPayload } from 'payload' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | + |
| 7 | +const filename = fileURLToPath(import.meta.url) |
| 8 | +const dirname = path.dirname(filename) |
| 9 | + |
| 10 | +const describe = |
| 11 | + process.env.PAYLOAD_DATABASE === 'postgres' ? global.describe : global.describe.skip |
| 12 | + |
| 13 | +const clearMigrations = () => { |
| 14 | + if (existsSync(path.resolve(dirname, 'migrations'))) { |
| 15 | + rmSync(path.resolve(dirname, 'migrations'), { force: true, recursive: true }) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('SQL migrations', () => { |
| 20 | + // If something fails - an error will be thrown. |
| 21 | + // eslint-disable-next-line jest/expect-expect |
| 22 | + it('should up and down migration successfully', async () => { |
| 23 | + clearMigrations() |
| 24 | + |
| 25 | + const { databaseAdapter } = await import(path.resolve(dirname, '../../databaseAdapter.js')) |
| 26 | + |
| 27 | + const init = databaseAdapter.init |
| 28 | + |
| 29 | + // set options |
| 30 | + databaseAdapter.init = ({ payload }) => { |
| 31 | + const adapter = init({ payload }) |
| 32 | + adapter.migrationDir = path.resolve(dirname, 'migrations') |
| 33 | + adapter.push = false |
| 34 | + return adapter |
| 35 | + } |
| 36 | + |
| 37 | + const config = await buildConfig({ |
| 38 | + db: databaseAdapter, |
| 39 | + secret: 'secret', |
| 40 | + collections: [ |
| 41 | + { |
| 42 | + slug: 'users', |
| 43 | + auth: true, |
| 44 | + fields: [], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }) |
| 48 | + |
| 49 | + const payload = await getPayload({ config }) |
| 50 | + |
| 51 | + await payload.db.createMigration({ payload }) |
| 52 | + await payload.db.migrate() |
| 53 | + await payload.db.migrateDown() |
| 54 | + |
| 55 | + await payload.db.dropDatabase({ adapter: payload.db as any }) |
| 56 | + await payload.db.destroy?.() |
| 57 | + }) |
| 58 | +}) |
0 commit comments