Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const DRIVERS_INFO: Record<
database: {},
}

/**
* Returns the name of the connection configured as the default one inside
* the application "config/database.ts" file
*/
function getDatabaseConnectionName(command: Configure) {
const databaseConfig = command.app.config.get<{ connection?: string }>('database', {})
return databaseConfig.connection
}

/**
* Configures the package
*/
Expand Down Expand Up @@ -69,10 +78,18 @@ export async function configure(command: Configure) {
await codemods.defineEnvValidations({ variables: envValidations })
}

/**
* Define the database connection to use. We read the default connection
* from the application database config file, so that the queue and the
* rest of the application share the same connection.
*/
const databaseConnection =
driver === 'database' ? (getDatabaseConnectionName(command) ?? 'sqlite') : undefined

/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/queue.stub', { driver })
await codemods.makeUsingStub(stubsRoot, 'config/queue.stub', { driver, databaseConnection })

/**
* Publish scheduler preload file
Expand Down
2 changes: 1 addition & 1 deletion stubs/config/queue.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
}),
{{#elif driver === 'database'}}
database: drivers.database({
connectionName: 'primary',
connectionName: '{{ databaseConnection }}',
}),
{{/if}}
sync: drivers.sync(),
Expand Down
35 changes: 35 additions & 0 deletions tests/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,39 @@ test.group('Configure', (group) => {

assert.isTrue(hasQueueMigration)
})

test('should use the database connection from the app config', async ({ assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.merge({
config: {
database: {
connection: 'postgres',
connections: {},
},
},
})
.create(BASE_URL, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, BASE_URL).href)
}

return import(filePath)
},
})

const app = ignitor.createApp('web')
await app.init().then(() => app.boot())

const ace = await app.container.make('ace')
ace.prompt.trap('Select the queue driver you plan to use').chooseOption(1) // database
ace.ui.switchMode('raw')

const command = await ace.create(Configure, ['../../index.js'])
await command.exec()

await assert.fileContains('config/queue.ts', `connectionName: 'postgres'`)
})
})