Skip to content

Commit

Permalink
fix(db-mongodb): write migrations index file (#9071)
Browse files Browse the repository at this point in the history
fix: remove 'undefined' written into mongodb migrations
fix: write migrations index file
  • Loading branch information
DanRibbens authored Nov 8, 2024
1 parent 1f26237 commit d20445b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import type { CreateMigration, MigrationTemplateArgs } from 'payload'

import fs from 'fs'
import path from 'path'
import { getPredefinedMigration } from 'payload'
import { getPredefinedMigration, writeMigrationIndex } from 'payload'
import { fileURLToPath } from 'url'

const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {
MigrateUpArgs,
MigrateDownArgs,
MigrateUpArgs,
} from '@payloadcms/db-mongodb'
${imports}
${imports ?? ''}
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
${upSQL ?? ` // Migration code`}
}
Expand Down Expand Up @@ -51,5 +50,8 @@ export const createMigration: CreateMigration = async function createMigration({
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
const filePath = `${dir}/${fileName}`
fs.writeFileSync(filePath, migrationFileContent)

writeMigrationIndex({ migrationsDir: payload.db.migrationDir })

payload.logger.info({ msg: `Migration created at ${filePath}` })
}
6 changes: 6 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ describe('database', () => {
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 () => {
let error
try {
Expand Down

0 comments on commit d20445b

Please sign in to comment.