Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(seeds): new environment config #851

Merged
merged 4 commits into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions adonis-typings/seeder.ts
Expand Up @@ -14,7 +14,11 @@ declare module '@ioc:Adonis/Lucid/Seeder' {
* Shape of seeder class
*/
export type SeederConstructorContract = {
/**
* @deprecated
*/
developmentOnly: boolean
environment: string[]
new (client: QueryClientContract): {
client: QueryClientContract
run(): Promise<void>
Expand Down
4 changes: 4 additions & 0 deletions src/BaseSeeder/index.ts
Expand Up @@ -10,7 +10,11 @@
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'

export class BaseSeeder {
/**
* @deprecated
*/
public static developmentOnly: boolean
public static environment: string[]
constructor(public client: QueryClientContract) {}

public async run() {}
Expand Down
12 changes: 9 additions & 3 deletions src/SeedsRunner/index.ts
Expand Up @@ -57,11 +57,17 @@ export class SeedsRunner {
file: file,
}

if ('developmentOnly' in Source) {
this.app.logger.warn(`Seeder "${file.name}" is using the deprecated flag "developmentOnly".`)
}

/**
* Ignore when running in non-development environment and seeder is development
* only
* Ignore when when the node environement is not the same as the seeder configuration.
*/
if (Source.developmentOnly && !this.app.inDev) {
if (
(Source.developmentOnly && !this.app.inDev) ||
(Source.environment && !Source.environment.includes(this.app.nodeEnvironment))
) {
seeder.status = 'ignored'
return seeder
}
Expand Down
58 changes: 58 additions & 0 deletions test/seeds/seeds-runner.spec.ts
Expand Up @@ -96,4 +96,62 @@ test.group('Seeds Runner', (group) => {
delete process.env.NODE_ENV
await db.manager.closeAll()
})

test('mark file as ignored when "environment = production" and not running in production mode', async ({
assert,
}) => {
process.env.NODE_ENV = 'development'

const app = await setupApplication()
const db = getDb(app)
const runner = new SeedsRunner(db, app)

await fs.add(
'database/seeders/User.ts',
`export default class FooSeeder {
public static invoked = false
public static environment = ['production']

run () {
(this.constructor as any).invoked = true
}
}`
)

const files = await runner.getList()
const report = await runner.run(files[0])
assert.equal(report.status, 'ignored')

delete process.env.NODE_ENV
await db.manager.closeAll()
})

test('mark file as ignored when "environment = development" and not running in development mode', async ({
assert,
}) => {
process.env.NODE_ENV = 'production'

const app = await setupApplication()
const db = getDb(app)
const runner = new SeedsRunner(db, app)

await fs.add(
'database/seeders/User.ts',
`export default class FooSeeder {
public static invoked = false
public static environment = ['development']

run () {
(this.constructor as any).invoked = true
}
}`
)

const files = await runner.getList()
const report = await runner.run(files[0])
assert.equal(report.status, 'ignored')

delete process.env.NODE_ENV
await db.manager.closeAll()
})
})