-
Notifications
You must be signed in to change notification settings - Fork 108
Drizzle Studio in Devtools points wrong DB in cloudflare-dev emulation #805
Description
Describe the bug
When using cloudflare-module preset with @nuxthub/core, the db instance provided by @nuxthub/db correctly references the local D1 database (sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject), but Drizzle Studio launched from Nuxt Devtools creates and references an empty database (.data/db/sqlite.db) instead.
Steps to reproduce
- Create a project from nuxt-hub/starter template
- Upgrade
@nuxthub/coreto 0.10.5 - Update nuxt.config.ts with the following:
- Set
nitro.presettocloudflare-module - Set
hub.dbto{ dialect: 'sqlite', driver: 'd1', connection: { databaseId: 'Your Database ID' } }
- Set
- Run
nuxt dev - Open Nuxt Devtools and go to the Database tab
- Click the Launch button
- The migration table is not created (empty database is shown)
Expected behavior
Drizzle Studio should reference the same D1 database file that the application is using (the sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject), not create a new empty database.
Additional context
Root Cause:
Looking at https://github.com/nuxt-hub/core/blob/main/src/devtools.ts, when driver is set to d1, the db instance uses D1, but when launching Drizzle Studio, if dialect is sqlite, it uses libsql. Since connection.url is typically not specified when using driver: 'd1', Drizzle Studio is launched with the default value .data/db/sqlite.db, creating and referencing an empty sqlite file.
Proposed Fix:
In https://github.com/nuxt-hub/core/blob/main/src/devtools.ts, when driver is d1, automatically search for the sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject and set that path to the url. However, the possibility of wrangler specification changes or configuration-based storage location changes should be considered.
Workaround:
Setting hub.db.connection.url to the sqlite file auto-generated by wrangler can avoid this issue. However, since the filename is a random string and created when nuxt dev is executed, it cannot be configured in advance, and the setting cannot be committed as it's local-dependent.
Version:
@nuxthub/core: 0.10.5