From a08674f708b8f0aa548c168a85dab3299e183ccc Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 4 Mar 2024 15:58:56 -0500 Subject: [PATCH] test: database int --- .../database/migrations/readMigrationFiles.ts | 3 +- test/buildConfigWithDefaults.ts | 13 ----- test/database/int.spec.ts | 50 +++++++------------ 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/packages/payload/src/database/migrations/readMigrationFiles.ts b/packages/payload/src/database/migrations/readMigrationFiles.ts index 2216b4f93..5e79a8318 100644 --- a/packages/payload/src/database/migrations/readMigrationFiles.ts +++ b/packages/payload/src/database/migrations/readMigrationFiles.ts @@ -35,7 +35,8 @@ export const readMigrationFiles = async ({ return Promise.all( files.map(async (filePath) => { - const migration = eval(`(await import(${filePath}))`) as Migration + // eslint-disable-next-line @typescript-eslint/no-var-requires + const migration = require(filePath) as Migration migration.name = path.basename(filePath).split('.')?.[0] return migration }), diff --git a/test/buildConfigWithDefaults.ts b/test/buildConfigWithDefaults.ts index a12a2e05f..bb56a2dd6 100644 --- a/test/buildConfigWithDefaults.ts +++ b/test/buildConfigWithDefaults.ts @@ -32,27 +32,16 @@ import { // process.env.PAYLOAD_DATABASE = 'postgres' -const [testSuiteDir] = process.argv.slice(4) -const migrationDir = path.resolve( - (process.env.PAYLOAD_CONFIG_PATH - ? path.join(process.env.PAYLOAD_CONFIG_PATH, '..') - : testSuiteDir) || __dirname, - 'migrations', -) - const databaseAdapters = { mongoose: mongooseAdapter({ - migrationDir, url: 'mongodb://127.0.0.1/payloadtests', }), postgres: postgresAdapter({ - migrationDir, pool: { connectionString: process.env.POSTGRES_URL || 'postgres://127.0.0.1:5432/payloadtests', }, }), 'postgres-custom-schema': postgresAdapter({ - migrationDir, pool: { connectionString: process.env.POSTGRES_URL || 'postgres://127.0.0.1:5432/payloadtests', }, @@ -60,13 +49,11 @@ const databaseAdapters = { }), 'postgres-uuid': postgresAdapter({ idType: 'uuid', - migrationDir, pool: { connectionString: process.env.POSTGRES_URL || 'postgres://127.0.0.1:5432/payloadtests', }, }), supabase: postgresAdapter({ - migrationDir, pool: { connectionString: process.env.POSTGRES_URL || 'postgresql://postgres:postgres@127.0.0.1:54322/postgres', diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index 7f1375327..8a56ec689 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -1,4 +1,3 @@ -import { sql } from 'drizzle-orm' import fs from 'fs' import path from 'path' @@ -8,7 +7,6 @@ import type { TypeWithID } from '../../packages/payload/src/collections/config/t import type { PayloadRequest } from '../../packages/payload/src/types' import { getPayload } from '../../packages/payload/src' -import { migrate } from '../../packages/payload/src/bin/migrate' import { commitTransaction } from '../../packages/payload/src/utilities/commitTransaction' import { initTransaction } from '../../packages/payload/src/utilities/initTransaction' import { devUser } from '../credentials' @@ -26,6 +24,7 @@ describe('database', () => { beforeAll(async () => { const config = await startMemoryDB(configPromise) payload = await getPayload({ config }) + payload.db.migrationDir = path.join(__dirname, './migrations') const loginResult = await payload.login({ collection: 'users', @@ -37,7 +36,6 @@ describe('database', () => { user = loginResult.user }) - describe('migrations', () => { beforeAll(async () => { if (process.env.PAYLOAD_DROP_DATABASE === 'true' && 'drizzle' in payload.db) { @@ -53,15 +51,15 @@ describe('database', () => { }) afterAll(() => { - removeFiles(path.normalize(payload.db.migrationDir)) + removeFiles(path.join(__dirname, './migrations')) }) it('should run migrate:create', async () => { - const args = { - _: ['migrate:create', 'test'], + await payload.db.createMigration({ forceAcceptWarning: true, - } - await migrate(args) + migrationName: 'test', + payload, + }) // read files names in migrationsDir const migrationFile = path.normalize(fs.readdirSync(payload.db.migrationDir)[0]) @@ -69,25 +67,23 @@ describe('database', () => { }) it('should run migrate', async () => { - const args = { - _: ['migrate'], + try { + await payload.db.migrate() + } catch (e) { + console.error(e) } - await migrate(args) const { docs } = await payload.find({ collection: 'payload-migrations', }) const migration = docs[0] - expect(migration.name).toContain('_test') - expect(migration.batch).toStrictEqual(1) + expect(migration?.name).toContain('_test') + expect(migration?.batch).toStrictEqual(1) }) it('should run migrate:status', async () => { let error - const args = { - _: ['migrate:status'], - } try { - await migrate(args) + await payload.db.migrateStatus() } catch (e) { error = e } @@ -95,11 +91,7 @@ describe('database', () => { }) it('should run migrate:fresh', async () => { - const args = { - _: ['migrate:fresh'], - forceAcceptWarning: true, - } - await migrate(args) + await payload.db.migrateFresh({ forceAcceptWarning: true }) const { docs } = await payload.find({ collection: 'payload-migrations', }) @@ -111,11 +103,8 @@ describe('database', () => { // known issue: https://github.com/payloadcms/payload/issues/4597 it.skip('should run migrate:down', async () => { let error - const args = { - _: ['migrate:down'], - } try { - await migrate(args) + await payload.db.migrateDown() } catch (e) { error = e } @@ -125,11 +114,8 @@ describe('database', () => { // known issue: https://github.com/payloadcms/payload/issues/4597 it.skip('should run migrate:refresh', async () => { let error - const args = { - _: ['migrate:refresh'], - } try { - await migrate(args) + await payload.db.migrateRefresh() } catch (e) { error = e } @@ -161,7 +147,7 @@ describe('database', () => { collection, // omitting req for isolation }), - ).rejects.toThrow('The requested resource was not found.') + ).rejects.toThrow('Not Found') const second = await payload.create({ collection, @@ -282,7 +268,7 @@ describe('database', () => { collection, req, }), - ).rejects.toThrow('The requested resource was not found.') + ).rejects.toThrow('Not Found') }) }) })