Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database): add support for multiple database migrations directories #423

Merged
merged 3 commits into from
Jan 17, 2025

Conversation

atinux
Copy link
Contributor

@atinux atinux commented Jan 16, 2025

Resolves #401

With this PR, NuxtHub scans the server/database/migrations directory for migrations for each Nuxt layer.

If you need to scan additional migrations directories, you can specify them in your nuxt.config.ts file.

export default defineNuxtConfig({
  hub: {
    databaseMigrationsDirs: [
      'my-module/db-migrations/'
    ]
  }
})

NuxtHub will scan both server/database/migrations and my-module/db-migrations directories for .sql files.

If you want more control to the migrations directories or you are working on a Nuxt module, you can use the hub:database: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:database:migrations:dirs', (dirs) => {
      dirs.push(resolve('db-migrations'))
    })
  }
})

All migrations files are copied to the .data/hub/database/migrations directory every time you run Nuxt. This is useful to see all migrations applied in the database as well as running the npx nuxthub database migrations <command> commands.

Copy link

cloudflare-workers-and-pages bot commented Jan 16, 2025

Deploying playground with  Cloudflare Pages  Cloudflare Pages

Latest commit: ef49213
Status: ✅  Deploy successful!
Preview URL: https://3ebdd258.playground-bhb.pages.dev
Branch Preview URL: https://feat-db-migrations-dirs.playground-bhb.pages.dev

View logs

Copy link

cloudflare-workers-and-pages bot commented Jan 16, 2025

Deploying nuxthub-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: ef49213
Status: ✅  Deploy successful!
Preview URL: https://eed64a14.nuxthub-module.pages.dev
Branch Preview URL: https://feat-db-migrations-dirs.nuxthub-module.pages.dev

View logs

Copy link

pkg-pr-new bot commented Jan 16, 2025

Open in Stackblitz

npm i https://pkg.pr.new/nuxt-hub/core/@nuxthub/core@423

commit: ef49213

@Eckhardt-D
Copy link

Eckhardt-D commented Jan 16, 2025

@atinux if I specify e.g. github:some-external-org/layer and this org decides to add arbitrary migrations, could that slip into my DB? Will it make sense to have either an include or exclude field to only run the hook on modules/layers I know for sure I can trust?

@atinux
Copy link
Contributor Author

atinux commented Jan 16, 2025

@Eckhardt-D then I suggest you to not depend on a GitHub repository, it is also same for Nuxt module that you trust honestly, always lock to a version and carefully check the changes when upgrading. (same for any npm packages)

@atinux atinux merged commit f845211 into main Jan 17, 2025
10 checks passed
@atinux atinux deleted the feat/db-migrations-dirs branch January 17, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database migrations hook
2 participants