fix(db-mongodb): write migrations index file (#9071)
fix: remove 'undefined' written into mongodb migrations fix: write migrations index file
This commit is contained in:
@@ -2,15 +2,14 @@ import type { CreateMigration, MigrationTemplateArgs } from 'payload'
|
|||||||
|
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { getPredefinedMigration } from 'payload'
|
import { getPredefinedMigration, writeMigrationIndex } from 'payload'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {
|
const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {
|
||||||
MigrateUpArgs,
|
|
||||||
MigrateDownArgs,
|
MigrateDownArgs,
|
||||||
|
MigrateUpArgs,
|
||||||
} from '@payloadcms/db-mongodb'
|
} from '@payloadcms/db-mongodb'
|
||||||
${imports}
|
${imports ?? ''}
|
||||||
|
|
||||||
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
|
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
|
||||||
${upSQL ?? ` // Migration code`}
|
${upSQL ?? ` // Migration code`}
|
||||||
}
|
}
|
||||||
@@ -51,5 +50,8 @@ export const createMigration: CreateMigration = async function createMigration({
|
|||||||
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
|
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
|
||||||
const filePath = `${dir}/${fileName}`
|
const filePath = `${dir}/${fileName}`
|
||||||
fs.writeFileSync(filePath, migrationFileContent)
|
fs.writeFileSync(filePath, migrationFileContent)
|
||||||
|
|
||||||
|
writeMigrationIndex({ migrationsDir: payload.db.migrationDir })
|
||||||
|
|
||||||
payload.logger.info({ msg: `Migration created at ${filePath}` })
|
payload.logger.info({ msg: `Migration created at ${filePath}` })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -145,6 +145,12 @@ describe('database', () => {
|
|||||||
expect(migrationFile).toContain('_test')
|
expect(migrationFile).toContain('_test')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should create index.ts file in the migrations directory with file imports', () => {
|
||||||
|
const indexFile = path.join(payload.db.migrationDir, 'index.ts')
|
||||||
|
const indexFileContent = fs.readFileSync(indexFile, 'utf8')
|
||||||
|
expect(indexFileContent).toContain("_test from './")
|
||||||
|
})
|
||||||
|
|
||||||
it('should run migrate', async () => {
|
it('should run migrate', async () => {
|
||||||
let error
|
let error
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user