nuxt-hub / core

Build full-stack applications with Nuxt on CloudFlare, with zero configuration.
https://hub.nuxt.com
Apache License 2.0
992 stars 57 forks source link

Database migrations #116

Closed zacwebb closed 3 weeks ago

zacwebb commented 6 months ago

I've had a read of #46, but I'm wondering if there's plans for a built-in approach to running migrations?

As much as running the dev server in remote mode works, I'd be much more confident in shipping if there was a proper way to do it. Currently most of my production services run on and I'm quite fond of their pre-deployment command to run migrations – maybe something similar could work for NuxtHub?

Currently I've got one project deploying with nuxthub to CF using a Github action to handle the deploy/migration process:

on:
  push:
    branches:
      - master

name: Deploy

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v3
        with:
          version: latest

      - name: Install
        run: pnpm i

      - name: Build
        run: pnpm build --preset=cloudflare_pages

      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/

  migrate:
    name: Run migrations
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v3
        with:
          version: latest

      - name: Configure wrangler.toml
        run: cp wrangler.prod.toml wrangler.toml

      - name: Run migration with wrangler
        run: pnpm i && pnpm wrangler d1 migrations apply [db_name] --remote
        env:
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}    
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}    

  deploy:
    name: Deploy Built Code
    runs-on: ubuntu-latest
    needs: [migrate]
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v3
        with:
          version: latest

      - uses: actions/download-artifact@v4
        with:
          name: dist

      - name: Deploy
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy . --project-name=[project name]

But this requires me having a separate wrangler.toml with the production database details in it to run the migrations using wrangler:

d1_databases = [
  { binding = "DB", database_name = "[database_name]", database_id = "[cf_database_uuid]", migrations_dir = "server/database/migrations" },
]

Any ideas on how to improve this setup to avoid having the separate wrangler file, and is the idea of something like the pre-deploy command something that would be considered for NuxtHub?

atinux commented 6 months ago

Thank you for your feedback @zacwebb

I agree with you as we need to find a nice way to run migrations, so far having it as a server plugin is nice for development but I believe we should be able to run the migrations with a command line too.

Would having nuxthub migrate works for you?

nuxthub migrate # local migration (can replace the server plugin)
nuxthub migrate --remote # remote migration (based on current branch to know prod/preview)
nuxthub migrate --remote production # migrate production database
zacwebb commented 6 months ago

Yeah that would be great! Coming from the Laravel world being able to run migrations in local and prod (via CI/CD) with just a single command is a really great DX.

Funnily enough just saw this release from Drizzle today https://github.com/drizzle-team/drizzle-kit-mirror/releases/tag/v0.21.3 👀

NeoPrint3D commented 4 months ago

any updates? migrations to prod or preview is a pain rn my app is non-critical in the development phase but I need to delete and replace the db everytime I have conflicting changes in the migration which is a pain. Will there be plans for like a db push in drizzle to override?

manniL commented 3 months ago

Would having nuxthub migrate works for you?

nuxthub migrate # local migration (can replace the server plugin)
nuxthub migrate --remote # remote migration (based on current branch to know prod/preview)
nuxthub migrate --remote production # migrate production database

That would solve lots of issues IMO 👍🏻

atinux commented 3 months ago

Indeed, it would be nuxthub database migrate though :)

atinux commented 3 weeks ago

Database migrations is out!

https://hub.nuxt.com/changelog/database-migrations