Migrations

Manage database schema changes in Nuxt with Drizzle ORM migrations, including creating, applying, and tracking migration files safely.

Database migrations provide version control for your database schema. NuxtHub supports SQL migration files (.sql) and automatically applies them during development and deployment. Making them fully compatible with Drizzle Kit generated migrations.

Create dialect-specific migrations with .<dialect>.sql suffix (e.g., 0001_create-todos.postgresql.sql).

Migrations Directories

NuxtHub scans server/db/migrations for migrations in each Nuxt layer.

To scan additional directories, specify them in your config:

nuxt.config.ts
export default defineNuxtConfig({
  hub: {
    db: {
      dialect: 'postgresql',
      migrationsDirs: [
        'server/db/custom-migrations/'
      ]
    }
  }
})

For more control (e.g., in Nuxt modules), use the hub:db:migrations:dirs hook:

import { createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: 'my-auth-module'
  },
  setup(options, nuxt) {
    const { resolve } = createResolver(import.meta.url)

    nuxt.hook('hub:db:migrations:dirs', (dirs) => {
      dirs.push(resolve('./db-migrations'))
    })
  }
})
All migration files are copied to .data/db/migrations when you run Nuxt, giving you a consolidated view.

Automatic migrations

Migrations are automatically applied when you:

  • Start the development server (npx nuxi dev)
  • Build the application (npx nuxi build)

Applied migrations are tracked in the _hub_migrations database table.

Generating migrations

Once you have updates your database schema, you can generate new migrations using the following command:

Terminal
npx nuxt db generate

This will generate new migrations files in server/db/migrations/{dialect}/ which are automatically applied during development and deployment.

Applying migrations

Once you have generated new migrations, you can apply them using the following command:

Terminal
npx nuxt db migrate

This will apply the new migrations to your database.

When running the development server, NuxtHub will automatically apply the migrations for you.

Post-migration queries

Advanced use case: These queries run after migrations but aren't tracked in _hub_migrations. Ensure they're idempotent.

Use the hub:db:queries:paths hook to run additional queries after migrations:

import { createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: 'my-auth-module'
  },
  setup(options, nuxt) {
    const { resolve } = createResolver(import.meta.url)

    nuxt.hook('hub:db:queries:paths', (paths, dialect) => {
      paths.push(resolve(`./db-queries/seed-admin.${dialect}.sql`))
    })
  }
})
All migrations queries are copied to .data/db/queries when you run Nuxt, giving you a consolidated view.

Foreign-key constraints

For Cloudflare D1 with Drizzle ORM migrations, replace:

Example
-PRAGMA foreign_keys = OFF;
+PRAGMA defer_foreign_keys = on;

ALTER TABLE ...

-PRAGMA foreign_keys = ON;
+PRAGMA defer_foreign_keys = off;
Learn more about defer foreign key constraints in Cloudflare D1.