Skip to content

Commit f2d75ec

Browse files
committed
fix: options merging bug what caused missing migrations error message with mysql
1 parent e2e0ad5 commit f2d75ec

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export default defineNuxtModule<RuntimeModuleOptions>({
6060

6161
// Add runtime config (server-side)
6262
// Priority: runtimeConfig (strongest) > top-level app config > default options (weakest)
63-
const runtimeConfigOptions = defu(nuxt.options.runtimeConfig.nuxtUsers || {}, options, defaultOptions) as ModuleOptions
63+
// Exclude connector from defaults to prevent mixing MySQL/PostgreSQL with SQLite settings
64+
const { connector: _defaultConnector, ...defaultsWithoutConnector } = defaultOptions
65+
const runtimeConfigOptions = defu(nuxt.options.runtimeConfig.nuxtUsers || {}, options, defaultsWithoutConnector) as ModuleOptions
66+
67+
// Use the configured connector or fallback to default (don't merge connector options)
68+
const configuredConnector = (nuxt.options.runtimeConfig.nuxtUsers as ModuleOptions)?.connector || options.connector
69+
runtimeConfigOptions.connector = configuredConnector || defaultOptions.connector
6470

6571
nuxt.options.runtimeConfig.nuxtUsers = {
6672
...runtimeConfigOptions,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { defu } from 'defu'
3+
import { defaultOptions } from '../../src/module'
4+
import type { ModuleOptions } from '../../src/types'
5+
6+
/**
7+
* Test to demonstrate the connector merging bug in module.ts
8+
*
9+
* When a user configures MySQL in runtimeConfig, defu merges it with defaultOptions
10+
* which contains SQLite settings, resulting in MySQL config having a 'path' property.
11+
*/
12+
describe('Module connector merging bug', () => {
13+
it('should demonstrate how defu mixes MySQL and SQLite configs (OLD BEHAVIOR)', () => {
14+
// Simulate what the user has in their nuxt.config.ts
15+
const userRuntimeConfig = {
16+
connector: {
17+
name: 'mysql' as const,
18+
options: {
19+
host: 'localhost',
20+
port: 3306,
21+
user: '',
22+
password: '',
23+
database: ''
24+
}
25+
}
26+
}
27+
28+
// This is the OLD way (before the fix) - directly merging with defaultOptions
29+
const runtimeConfigOptions = defu(userRuntimeConfig, {}, defaultOptions) as ModuleOptions
30+
31+
// BUG: The merged config INCORRECTLY has a path property for MySQL
32+
console.log('Merged connector options (old way):', runtimeConfigOptions.connector.options)
33+
34+
// This demonstrates the bug - path should be undefined for MySQL but isn't
35+
expect(runtimeConfigOptions.connector.options.path).toBe('./data/users.sqlite3')
36+
})
37+
38+
it('should show the correct way to merge without mixing connectors', () => {
39+
const userRuntimeConfig = {
40+
connector: {
41+
name: 'mysql' as const,
42+
options: {
43+
host: 'localhost',
44+
port: 3306,
45+
user: '',
46+
password: '',
47+
database: ''
48+
}
49+
}
50+
}
51+
52+
// FIX: Exclude connector from defaults before merging
53+
const { connector: _defaultConnector, ...defaultsWithoutConnector } = defaultOptions
54+
const runtimeConfigOptions = defu(userRuntimeConfig, {}, defaultsWithoutConnector) as ModuleOptions
55+
56+
// Use the configured connector (don't merge with default)
57+
runtimeConfigOptions.connector = userRuntimeConfig.connector || defaultOptions.connector
58+
59+
// This should pass - MySQL config should not have SQLite path
60+
expect(runtimeConfigOptions.connector.options.path).toBeUndefined()
61+
expect(runtimeConfigOptions.connector.name).toBe('mysql')
62+
expect(runtimeConfigOptions.connector.options.host).toBe('localhost')
63+
})
64+
})

0 commit comments

Comments
 (0)