directus-sync icon indicating copy to clipboard operation
directus-sync copied to clipboard

Upload latest schema on startup?

Open NilsBaumgartner1994 opened this issue 10 months ago • 11 comments

Is your feature request related to a problem? Please describe. I would like to always upload the latest schema when i am about to start my remote servers.

Describe the solution you'd like A small folder where it uses it to automatically push the schema from.

Describe alternatives you've considered A script which waits for directus to boot up and then to upload the schema.

Additional context Great work of your project. Realy hope to see it come directly from directus.

NilsBaumgartner1994 avatar Feb 07 '25 17:02 NilsBaumgartner1994

Hi @NilsBaumgartner1994 I had few feature requests similar to this one. I was thinking to add a command wait that would complete once the server is started (relying on the info endpoint of Directus). This could be used along with other commands, like this:

npx directus-sync wait --interval 5 --timeout 60 && npx directus-sync push

Could this help for your use case ?

EdouardDem avatar Feb 10 '25 14:02 EdouardDem

I thought about a custom api hook which would start during the init server start hook. And then maybe when completed and uploaded the new schema force the server instance to restart aka. Crash

NilsBaumgartner1994 avatar Feb 10 '25 17:02 NilsBaumgartner1994

@EdouardDem i created in directus a feature request: https://github.com/directus/directus/discussions/24647

But is it possible for directus-sync to somehow be called before directus even starts? Like passing a path to the extension, where to find the latest schema?

NilsBaumgartner1994 avatar Feb 19 '25 23:02 NilsBaumgartner1994

If you're using docker compose you can deploy directus-sync as a separate container and make other services depend on it so they start only when configuration is uploaded.

Here's my setup:

services:
  directus:
    depends_on:
      database:
        condition: service_healthy

    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://directus:8055/server/health"]

  directus-sync:
    environment:
      DIRECTUS_URL: http://directus:8055
      DIRECTUS_ADMIN_EMAIL: ${DIRECTUS_ADMIN_EMAIL}
      DIRECTUS_ADMIN_PASSWORD: ${DIRECTUS_ADMIN_PASSWORD}

    depends_on:
      directus:
        condition: service_healthy

  app:
    depends_on:
      directus-sync:
        condition: service_completed_successfully

directus-sync is just this:

FROM node:23-slim

RUN npm install --global directus-sync

WORKDIR /app

# You can mount a volume instead for development deploys
COPY ./configuration/directus /app/configuration/directus/ 

CMD ["npx", "directus-sync", "push", "--dump-path", "./configuration/directus"]

bgenia avatar May 11 '25 22:05 bgenia

If you're using docker compose you can deploy directus-sync as a separate container and make other services depend on it so they start only when configuration is uploaded.

Here's my setup:

services: directus: depends_on: database: condition: service_healthy

healthcheck:
  test: ["CMD", "wget", "--spider", "-q", "http://directus:8055/server/health"]

directus-sync: environment: DIRECTUS_URL: http://directus:8055 DIRECTUS_ADMIN_EMAIL: ${DIRECTUS_ADMIN_EMAIL} DIRECTUS_ADMIN_PASSWORD: ${DIRECTUS_ADMIN_PASSWORD}

depends_on:
  directus:
    condition: service_healthy

app: depends_on: directus-sync: condition: service_completed_successfully directus-sync is just this:

FROM node:23-slim

RUN npm install --global directus-sync

WORKDIR /app

You can mount a volume instead for development deploys

COPY ./configuration/directus /app/configuration/directus/

CMD ["npx", "directus-sync", "push", "--dump-path", "./configuration/directus"]

Yes awesome that’s something that could work.

NilsBaumgartner1994 avatar May 12 '25 04:05 NilsBaumgartner1994

Is there any way to call it inside node / typescript directly?

NilsBaumgartner1994 avatar Aug 17 '25 20:08 NilsBaumgartner1994

If you are able to start a new process, you could directly call the CLI using child_process.spawn(). However, usually you can't do it from the CI.

In that case, you could import the package directus-sync and use the function createProgram as it is done in the tests suite.

EdouardDem avatar Aug 18 '25 01:08 EdouardDem

Awesome. I think this would be nicer than to Call spawn. The problem I encounter is now trickier. The goal is to setup everything so that other hooks can use the migrated database directly.

So start up Directus. Migrate database with hook app.after The problem seems currently to call it from inside Directus. I will have a look at the test suite.

NilsBaumgartner1994 avatar Aug 18 '25 08:08 NilsBaumgartner1994

Does the test suite avoid to be caked by multiple Directus instances when docker is started with duplicate (load balancing)?

NilsBaumgartner1994 avatar Aug 18 '25 08:08 NilsBaumgartner1994

Does the test suite avoid to be caked by multiple Directus instances when docker is started with duplicate (load balancing)?

No. The test suite uses a single Directus instance.

However, I just published a new version with a feature that may help you: https://tractr.github.io/directus-sync/docs/features/helpers#wait-for-server-ready

This allows to wait for the server to be ready.

EdouardDem avatar Aug 18 '25 13:08 EdouardDem

Thanks for the Update. I think for the current use case in order to deploy a server and have it set up, I think it will be required for the hooks to loop and wait until the migration is done. It would be preferable if it could be somehow done in the app.after or app.before init hook.

But I think I will go with this workaround. Thanks for the fast response. I still hope your project get the attention needed from official Directus, so that it could become a core part.

NilsBaumgartner1994 avatar Aug 18 '25 15:08 NilsBaumgartner1994