You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/content/1.docs/2.features/database.md
+83-16
Original file line number
Diff line number
Diff line change
@@ -241,11 +241,63 @@ This method can have poorer performance (prepared statements can be reused in so
241
241
242
242
## Database Migrations
243
243
244
-
Database migrations provide version control for your database schema. They track changes and ensure consistent schema evolution across all environments through incremental updates.
244
+
Database migrations provide version control for your database schema. They track changes and ensure consistent schema evolution across all environments through incremental updates. NuxtHub supports SQL migration files (`.sql`).
245
+
246
+
### Migrations Directories
247
+
248
+
NuxtHub scans the `server/database/migrations` directory for migrations **for each [Nuxt layer](https://nuxt.com/docs/getting-started/layers)**.
249
+
250
+
If you need to scan additional migrations directories, you can specify them in your `nuxt.config.ts` file.
251
+
252
+
```ts [nuxt.config.ts]
253
+
exportdefaultdefineNuxtConfig({
254
+
hub: {
255
+
// Array of additional migration directories to scan
256
+
databaseMigrationsDirs: [
257
+
'my-module/db-migrations/'
258
+
]
259
+
}
260
+
})
261
+
```
262
+
::note
263
+
NuxtHub will scan both `server/database/migrations` and `my-module/db-migrations` directories for `.sql` files.
264
+
::
265
+
266
+
If you want more control to the migrations directories or you are working on a [Nuxt module](https://nuxt.com/docs/guide/going-further/modules), you can use the `hub:database:migrations:dirs` hook:
All migrations files are copied to the `.data/hub/database/migrations` directory when you run Nuxt. This consolidated view helps you track all migrations and enables you to use `npx nuxthub database migrations <command>` commands.
296
+
::
245
297
246
298
### Automatic Application
247
299
248
-
SQL migrations in `server/database/migrations/*.sql` are automatically applied when you:
300
+
All `.sql` files in the databasemigrations directories are automatically applied when you:
249
301
- Start the development server (`npx nuxt dev` or [`npx nuxt dev --remote`](/docs/getting-started/remote-storage))
- Deploy via [`npx nuxthub deploy`](/docs/getting-started/deploy#nuxthub-cli) or [Cloudflare Pages CI](/docs/getting-started/deploy#cloudflare-pages-ci)
@@ -275,7 +327,6 @@ Migration files are created in `server/database/migrations/`.
275
327
276
328
After creation, add your SQL queries to modify the database schema.
Since NuxtHub doesn't recognize previously applied Drizzle ORM migrations (stored in `__drizzle_migrations`), it will attempt to rerun all migrations in `server/database/migrations/*.sql`. To prevent this:
383
+
::important
384
+
This feature is for advanced use cases. As the queries are run after the migrations process (see [Automatic Application](#automatic-application)), you want to make sure your queries are idempotent.
385
+
::
333
386
334
-
1. Mark existing migrations as applied in each environment:
387
+
Sometimes you need to run additional queries after migrations are applied without tracking them in the migrations table.
335
388
336
-
```bash [Terminal]
337
-
# Local environment
338
-
npx nuxthub database migrations mark-all-applied
389
+
NuxtHub provides the `hub:database:queries:paths` hook for this purpose:
INSERT OR IGNORE INTO admin_users (id, email, password) VALUES (1, '[email protected]', 'admin');
411
+
```
412
+
::
348
413
349
-
That's it! You can keep using `npx drizzle-kit generate` to generate migrations when updating your Drizzle ORM schema.
414
+
::note
415
+
These queries run after all migrations are applied but are not tracked in the `_hub_migrations` table. Use this for operations that should run when deploying your project.
0 commit comments