diff --git a/docs/docs/administration/backup-and-restore.md b/docs/docs/administration/backup-and-restore.md index 2b847fc4b..964586a0b 100644 --- a/docs/docs/administration/backup-and-restore.md +++ b/docs/docs/administration/backup-and-restore.md @@ -60,6 +60,10 @@ docker compose up -d # Start remainder of Immich apps Note that for the database restore to proceed properly, it requires a completely fresh install (i.e. the Immich server has never run since creating the Docker containers). If the Immich app has run, Postgres conflicts may be encountered upon database restoration (relation already exists, violated foreign key constraints, multiple primary keys, etc.). +:::tip +Some deployment methods make it difficult to start the database without also starting the server or microservices. In these cases, you may set the environmental variable `DB_SKIP_MIGRATIONS=true` before starting the services. This will prevent the server from running migrations that interfere with the restore process. Note that both the server and microservices must have this variable set to prevent the migrations from running. Be sure to remove this variable and restart the services after the database is restored. +::: + The database dumps can also be automated (using [this image](https://github.com/prodrigestivill/docker-postgres-backup-local)) by editing the docker compose file to match the following: ```yaml diff --git a/docs/docs/administration/postgres-standalone.md b/docs/docs/administration/postgres-standalone.md index 57cb75a78..e0a0cb875 100644 --- a/docs/docs/administration/postgres-standalone.md +++ b/docs/docs/administration/postgres-standalone.md @@ -32,10 +32,6 @@ DB_URL='postgresql://immichdbusername:immichdbpassword@postgreshost:postgresport # DB_URL='postgresql://immichdbusername:immichdbpassword@postgreshost:postgresport/immichdatabasename?sslmode=require&sslmode=no-verify' ``` -:::info -When `DB_URL` is defined, the other database (`DB_*`) variables are ignored, with the exception of `DB_VECTOR_EXTENSION`. -::: - ## With superuser permission Typically Immich expects superuser permission in the database, which you can grant by running `ALTER USER WITH SUPERUSER;` at the `psql` console. If you prefer not to grant superuser permissions, follow the instructions in the next section. diff --git a/docs/docs/install/environment-variables.md b/docs/docs/install/environment-variables.md index dce9d4fa5..7e9d9c9a9 100644 --- a/docs/docs/install/environment-variables.md +++ b/docs/docs/install/environment-variables.md @@ -58,21 +58,22 @@ These environment variables are used by the `docker-compose.yml` file and do **N ## Database -| Variable | Description | Default | Services | -| :---------------------------------- | :------------------------------------------------------------ | :----------: | :-------------------- | -| `DB_URL` | Database URL | | server, microservices | -| `DB_HOSTNAME` | Database Host | `localhost` | server, microservices | -| `DB_PORT` | Database Port | `5432` | server, microservices | -| `DB_USERNAME` | Database User | `postgres` | server, microservices | -| `DB_PASSWORD` | Database Password | `postgres` | server, microservices | -| `DB_DATABASE_NAME` | Database Name | `immich` | server, microservices | -| `DB_VECTOR_EXTENSION`\*1 | Database Vector Extension (one of [`pgvector`, `pgvecto.rs`]) | `pgvecto.rs` | server, microservices | +| Variable | Description | Default | Services | +| :---------------------------------- | :----------------------------------------------------------------------- | :----------: | :-------------------- | +| `DB_URL` | Database URL | | server, microservices | +| `DB_HOSTNAME` | Database Host | `localhost` | server, microservices | +| `DB_PORT` | Database Port | `5432` | server, microservices | +| `DB_USERNAME` | Database User | `postgres` | server, microservices | +| `DB_PASSWORD` | Database Password | `postgres` | server, microservices | +| `DB_DATABASE_NAME` | Database Name | `immich` | server, microservices | +| `DB_VECTOR_EXTENSION`\*1 | Database Vector Extension (one of [`pgvector`, `pgvecto.rs`]) | `pgvecto.rs` | server, microservices | +| `DB_SKIP_MIGRATIONS` | Whether to skip running migrations on startup (one of [`true`, `false`]) | `false` | server, microservices | -\*1: This setting cannot be changed after the server has successfully started up +\*1: This setting cannot be changed after the server has successfully started up. :::info -When `DB_URL` is defined, the other database (`DB_*`) variables are ignored, with the exception of `DB_VECTOR_EXTENSION`. +When `DB_URL` is defined, the `DB_HOSTNAME`, `DB_PORT`, `DB_USERNAME`, `DB_PASSWORD` and `DB_DATABASE_NAME` database variables are ignored. ::: diff --git a/server/src/config.ts b/server/src/config.ts index 068d0b3a9..85139b1b0 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -28,6 +28,7 @@ export const immichAppConfig: ConfigModuleOptions = { DB_DATABASE_NAME: WHEN_DB_URL_SET, DB_URL: Joi.string().optional(), DB_VECTOR_EXTENSION: Joi.string().optional().valid('pgvector', 'pgvecto.rs').default('pgvecto.rs'), + DB_SKIP_MIGRATIONS: Joi.boolean().optional().default(false), MACHINE_LEARNING_PORT: Joi.number().optional(), MICROSERVICES_PORT: Joi.number().optional(), diff --git a/server/src/services/database.service.spec.ts b/server/src/services/database.service.spec.ts index 28ed7d7df..778422c1b 100644 --- a/server/src/services/database.service.spec.ts +++ b/server/src/services/database.service.spec.ts @@ -12,6 +12,7 @@ describe(DatabaseService.name, () => { let loggerMock: Mocked; beforeEach(() => { + delete process.env.DB_SKIP_MIGRATIONS; databaseMock = newDatabaseRepositoryMock(); loggerMock = newLoggerRepositoryMock(); sut = new DatabaseService(databaseMock, loggerMock); @@ -210,5 +211,13 @@ describe(DatabaseService.name, () => { expect(loggerMock.fatal).not.toHaveBeenCalled(); }, ); + + it('should skip migrations if DB_SKIP_MIGRATIONS=true', async () => { + process.env.DB_SKIP_MIGRATIONS = 'true'; + + await expect(sut.init()).resolves.toBeUndefined(); + + expect(databaseMock.runMigrations).not.toHaveBeenCalled(); + }); }); }); diff --git a/server/src/services/database.service.ts b/server/src/services/database.service.ts index 8da48eb15..7a25238ea 100644 --- a/server/src/services/database.service.ts +++ b/server/src/services/database.service.ts @@ -49,7 +49,9 @@ export class DatabaseService { throw error; } - await this.databaseRepository.runMigrations(); + if (process.env.DB_SKIP_MIGRATIONS !== 'true') { + await this.databaseRepository.runMigrations(); + } }); }