From acbceeb2c8685496c09520f71c7830c87fff7b14 Mon Sep 17 00:00:00 2001 From: Guo Yunhe Date: Tue, 22 Sep 2026 12:26:08 +0800 Subject: [PATCH] fix(stubs): detect actual database connection name --- configure.ts | 19 ++++++++++++++++++- stubs/config/queue.stub | 2 +- tests/configure.spec.ts | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/configure.ts b/configure.ts index 567db05..cefdea2 100644 --- a/configure.ts +++ b/configure.ts @@ -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 */ @@ -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 diff --git a/stubs/config/queue.stub b/stubs/config/queue.stub index 135c9e9..6ba0a41 100644 --- a/stubs/config/queue.stub +++ b/stubs/config/queue.stub @@ -14,7 +14,7 @@ export default defineConfig({ }), {{#elif driver === 'database'}} database: drivers.database({ - connectionName: 'primary', + connectionName: '{{ databaseConnection }}', }), {{/if}} sync: drivers.sync(), diff --git a/tests/configure.spec.ts b/tests/configure.spec.ts index d9b252e..fa7b7a5 100644 --- a/tests/configure.spec.ts +++ b/tests/configure.spec.ts @@ -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'`) + }) })