immich-app / immich

High performance self-hosted photo and video management solution.
https://immich.app
GNU Affero General Public License v3.0
44.98k stars 2.18k forks source link

Run db migrations prior to first startup #9177

Closed nebulade closed 4 months ago

nebulade commented 4 months ago

The bug

Since 1.103.0 with https://github.com/immich-app/immich/pull/9118/files it seems one cannot run npm run typeorm:migrations:run anymore prior to the startup of the main app or at least it will use the hardcoded localhost for the postgres database.

For the Cloudron app package we need to configure some OpenID specific values in the database during app startup, which then naturally fails as tables don't exist yet on a fresh installation. Since we don't quite know once the main app startup has finished with the database migrations, we can't run those reliably now.

Is there any workaround for this or are we missing some other cli command to run migrations the same way the main app startup does but then exit after those have been run?

The OS that Immich Server is running on

Cloudron/Ubuntu

Version of Immich Server

v1.103.0

Version of Immich Mobile App

v1.103.0

Platform with the issue

Your docker-compose.yml content

does not apply

Your .env content

does not apply

Reproduction steps

Our startup script sets those env variables: https://git.cloudron.io/cloudron/immich-app/-/blob/main/env.sh?ref_type=heads
Then runs https://git.cloudron.io/cloudron/immich-app/-/blob/main/start.sh?ref_type=heads#L20

The OpenID related configs would be in the `start.sh` prior to running the services.

Relevant log output

No response

Additional information

No response

bo0tzz commented 4 months ago

I think the hardcoded localhost is probably wrong since people might be developing against a database running on a different host. However for your use case, maybe it would be more appropriate to support some sort of trigger/hook that is called after server startup? That might also make it possible to use the API to configure things rather than needing to directly poke the database.

@jrasm91 thoughts?

nebulade commented 4 months ago

I agree, poking the database directly is not a great idea, so far was just the only way to pre-setup this on behalf of the user. Does immich have an API which can be used on localhost without access tokens where we could just call the openid settings endpoint (and potentially other things)? Or would this be more the job for the immich-admin to have subcommands for that?

jrasm91 commented 4 months ago

I was not aware of a use case to manually run migrations through this command outside of development. We can make it possible to run against another host, but relying on it seems like a recipe for future issues. There is an API for setting and updating the configuration so maybe it is worth looking into a way to leverage the instead.

nebulade commented 4 months ago

Ideally we don't want to run the database migration manually up front. This is really only needed as the OpenID auth settings are pre-provisioned for the user on Cloudron to have SSO across many apps installed on a server.

Maybe to circumvent the database migration/app bootstrapping issue, does Immich have a way to pre-provision certain settings, like picking up those values from env variables instead of the database if provided? Briefly looked through the code but didn't catch anything like that.

bo0tzz commented 4 months ago

There is the config file, but I don't know whether that works for you as it fully locks the settings UI.

nebulade commented 4 months ago

Actually I didn't realize an optional config file exists. This solves it nicely for our use-case. Thanks a lot for pointing me in the right direction, sorry for the distraction.

For others https://immich.app/docs/install/config-file is the docs link.