Skip to content

Commit

Permalink
fix: handle esm exports when requiring migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 10, 2019
1 parent 2751527 commit 6349508
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Migrator/MigrationSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/// <reference path="../../adonis-typings/index.ts" />

import { readdir } from 'fs'
import { esmRequire } from '@poppinss/utils'
import { join, isAbsolute, extname } from 'path'
import { MigrationNode } from '@ioc:Adonis/Lucid/Migrator'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
Expand Down Expand Up @@ -42,7 +43,7 @@ export class MigrationSource {
return {
absPath: join(path, file),
name: join(directoryPath, file.replace(RegExp(`${extname(file)}$`), '')),
source: require(join(path, file)),
source: esmRequire(join(path, file)),
}
}))
})
Expand Down
11 changes: 10 additions & 1 deletion test/migrations/migration-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test.group('MigrationSource', (group) => {

test('sort multiple migration directories seperately', async (assert) => {
const app = new Application(fs.basePath, {} as any, {} as any, {})
const config = Object.assign(db.getRawConnection('primary')!.config, {
const config = Object.assign({}, db.getRawConnection('primary')!.config, {
migrations: {
paths: ['database/secondary', 'database/primary'],
},
Expand Down Expand Up @@ -98,4 +98,13 @@ test.group('MigrationSource', (group) => {
},
])
})

test('handle esm default exports properly', async (assert) => {
const app = new Application(fs.basePath, {} as any, {} as any, {})
const migrationSource = new MigrationSource(db.getRawConnection('primary')!.config, app)

await fs.add('database/migrations/foo.ts', 'export default class Foo {}')
const directories = await migrationSource.getMigrations()
assert.equal(directories[0].source.name, 'Foo')
})
})

0 comments on commit 6349508

Please sign in to comment.