moonrepo / moon

A build system and monorepo management tool for the web ecosystem, written in Rust.
https://moonrepo.dev/moon
MIT License
2.84k stars 155 forks source link

[bug] Persistent Tasks and Dependencies #1365

Open rawkode opened 6 months ago

rawkode commented 6 months ago

Describe the bug

It's my understanding from the docs that persistent tasks are always run last, and kept running.

I'm curious how we should handle cases where we need to apply migrations to a database that needs to be running?

I've included my setup below, which runs turso dev to get us a libsql database running. Once it's running, I want to apply the migrations and run grafbase; also a persistent task.

Steps to reproduce

# yaml-language-server: $schema=https://moonrepo.dev/schemas/project.json

# We should add a global flake
# to set MOON_TOOLCHAIN_FORCE_GLOBALS=true
type: application
platform: "system"

project:
  name: shows-service
  description: Microservice for Show Management

tasks:
  install-dependencies:
    command: bun install
    inputs:
      - ./bun.lockb
      - ./package.json
    outputs:
      - ./node_modules

  dev-grafbase:
    deps:
      - install-dependencies
    command: bun x grafbase dev
    options:
      persistent: true

  dev-turso:
    command: turso
    args:
      - dev
      - --port
      - "4021"
    options:
      persistent: true

  dev-turso-generate-migrations:
    deps:
      - install-dependencies
    inputs:
      - ./drizzle/schema
    command: cd drizzle && bunx drizzle-kit generate:sqlite

  dev-turso-apply-migrations:
    deps:
      - dev-turso-generate-migrations
    inputs:
      - ./drizzle/migrations
      - ./drizzle/migrate.ts
    command: cd drizzle && bun migrate.ts

  dev-turso-seed:
    deps:
      - dev-turso-apply-migrations
    inputs:
      - ./drizzle/seed.ts
    command: cd drizzle && bun seed.ts

  dev:
    local: true
    deps:
      - install-dependencies
      - dev-turso
      - dev-grafbase
      - dev-turso-seed
milesj commented 6 months ago

@rawkode Yeah, this is kind of a tricky one. I honestly don't think this is possible with moon directly at the moment, since all persistent tasks are batched together. You may have to wrap the migration part in a bash/js script that waits to execute until the DB is online, and then update the moon task to execute the script.

On a side note, you shouldn't need install-dependencies since moon does that automatically. https://moonrepo.dev/docs/how-it-works/action-graph#install-dependencies