2023-12-21 17:06:26 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-02-07 03:46:38 +01:00
|
|
|
import {
|
|
|
|
DatabaseExtension,
|
|
|
|
DatabaseLock,
|
|
|
|
IDatabaseRepository,
|
|
|
|
VectorExtension,
|
|
|
|
VectorIndex,
|
|
|
|
extName,
|
2024-03-21 12:59:49 +01:00
|
|
|
} from 'src/interfaces/database.interface';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 04:15:09 +01:00
|
|
|
import { Version, VersionType } from 'src/utils/version';
|
2023-12-21 17:06:26 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DatabaseService {
|
2024-02-07 03:46:38 +01:00
|
|
|
private vectorExt: VectorExtension;
|
2024-01-07 01:24:09 +01:00
|
|
|
minPostgresVersion = 14;
|
2024-02-07 03:46:38 +01:00
|
|
|
minVectorsVersion = new Version(0, 2, 0);
|
|
|
|
vectorsVersionPin = VersionType.MINOR;
|
|
|
|
minVectorVersion = new Version(0, 5, 0);
|
|
|
|
vectorVersionPin = VersionType.MAJOR;
|
2023-12-21 17:06:26 +01:00
|
|
|
|
2024-04-16 23:30:31 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository,
|
|
|
|
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
|
|
|
) {
|
|
|
|
this.logger.setContext(DatabaseService.name);
|
2024-02-07 03:46:38 +01:00
|
|
|
this.vectorExt = this.databaseRepository.getPreferredVectorExtension();
|
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
|
|
|
|
async init() {
|
2024-01-07 01:24:09 +01:00
|
|
|
await this.assertPostgresql();
|
2024-02-07 03:46:38 +01:00
|
|
|
await this.databaseRepository.withLock(DatabaseLock.Migrations, async () => {
|
|
|
|
await this.createVectorExtension();
|
|
|
|
await this.updateVectorExtension();
|
|
|
|
await this.assertVectorExtension();
|
2023-12-21 17:06:26 +01:00
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
try {
|
|
|
|
if (await this.databaseRepository.shouldReindex(VectorIndex.CLIP)) {
|
|
|
|
await this.databaseRepository.reindex(VectorIndex.CLIP);
|
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
if (await this.databaseRepository.shouldReindex(VectorIndex.FACE)) {
|
|
|
|
await this.databaseRepository.reindex(VectorIndex.FACE);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
'Could not run vector reindexing checks. If the extension was updated, please restart the Postgres instance.',
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
|
2024-04-25 04:52:38 +02:00
|
|
|
if (process.env.DB_SKIP_MIGRATIONS !== 'true') {
|
|
|
|
await this.databaseRepository.runMigrations();
|
|
|
|
}
|
2024-02-07 03:46:38 +01:00
|
|
|
});
|
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
private async assertPostgresql() {
|
|
|
|
const { major } = await this.databaseRepository.getPostgresVersion();
|
|
|
|
if (major < this.minPostgresVersion) {
|
2023-12-21 17:06:26 +01:00
|
|
|
throw new Error(`
|
2024-02-07 03:46:38 +01:00
|
|
|
The PostgreSQL version is ${major}, which is older than the minimum supported version ${this.minPostgresVersion}.
|
|
|
|
Please upgrade to this version or later.`);
|
2023-12-21 17:06:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
private async createVectorExtension() {
|
2024-03-05 23:23:06 +01:00
|
|
|
try {
|
|
|
|
await this.databaseRepository.createExtension(this.vectorExt);
|
|
|
|
} catch (error) {
|
2024-02-07 03:46:38 +01:00
|
|
|
const otherExt =
|
|
|
|
this.vectorExt === DatabaseExtension.VECTORS ? DatabaseExtension.VECTOR : DatabaseExtension.VECTORS;
|
2023-12-21 17:06:26 +01:00
|
|
|
this.logger.fatal(`
|
2024-02-07 03:46:38 +01:00
|
|
|
Failed to activate ${extName[this.vectorExt]} extension.
|
|
|
|
Please ensure the Postgres instance has ${extName[this.vectorExt]} installed.
|
|
|
|
|
|
|
|
If the Postgres instance already has ${extName[this.vectorExt]} installed, Immich may not have the necessary permissions to activate it.
|
|
|
|
In this case, please run 'CREATE EXTENSION IF NOT EXISTS ${this.vectorExt}' manually as a superuser.
|
|
|
|
See https://immich.app/docs/guides/database-queries for how to query the database.
|
|
|
|
|
2024-02-22 00:47:43 +01:00
|
|
|
Alternatively, if your Postgres instance has ${extName[otherExt]}, you may use this instead by setting the environment variable 'DB_VECTOR_EXTENSION=${extName[otherExt]}'.
|
2024-02-07 03:46:38 +01:00
|
|
|
Note that switching between the two extensions after a successful startup is not supported.
|
|
|
|
The exception is if your version of Immich prior to upgrading was 1.90.2 or earlier.
|
|
|
|
In this case, you may set either extension now, but you will not be able to switch to the other extension following a successful startup.
|
2023-12-21 17:06:26 +01:00
|
|
|
`);
|
2024-02-02 04:18:00 +01:00
|
|
|
throw error;
|
2024-03-05 23:23:06 +01:00
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
private async updateVectorExtension() {
|
|
|
|
const [version, availableVersion] = await Promise.all([
|
|
|
|
this.databaseRepository.getExtensionVersion(this.vectorExt),
|
|
|
|
this.databaseRepository.getAvailableExtensionVersion(this.vectorExt),
|
|
|
|
]);
|
|
|
|
if (version == null) {
|
|
|
|
throw new Error(`Unexpected: ${extName[this.vectorExt]} extension is not installed.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (availableVersion == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.vectorVersionPin : this.vectorsVersionPin;
|
|
|
|
const isNewer = availableVersion.isNewerThan(version);
|
|
|
|
if (isNewer == null || isNewer > maxVersion) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.logger.log(`Updating ${extName[this.vectorExt]} extension to ${availableVersion}`);
|
|
|
|
const { restartRequired } = await this.databaseRepository.updateVectorExtension(this.vectorExt, availableVersion);
|
|
|
|
if (restartRequired) {
|
|
|
|
this.logger.warn(`
|
|
|
|
The ${extName[this.vectorExt]} extension has been updated to ${availableVersion}.
|
|
|
|
Please restart the Postgres instance to complete the update.`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(`
|
|
|
|
The ${extName[this.vectorExt]} extension version is ${version}, but ${availableVersion} is available.
|
|
|
|
Immich attempted to update the extension, but failed to do so.
|
|
|
|
This may be because Immich does not have the necessary permissions to update the extension.
|
|
|
|
|
|
|
|
Please run 'ALTER EXTENSION ${this.vectorExt} UPDATE' manually as a superuser.
|
|
|
|
See https://immich.app/docs/guides/database-queries for how to query the database.`);
|
|
|
|
this.logger.error(error);
|
2023-12-21 17:06:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-07 01:24:09 +01:00
|
|
|
|
2024-02-07 03:46:38 +01:00
|
|
|
private async assertVectorExtension() {
|
|
|
|
const version = await this.databaseRepository.getExtensionVersion(this.vectorExt);
|
|
|
|
if (version == null) {
|
|
|
|
throw new Error(`Unexpected: The ${extName[this.vectorExt]} extension is not installed.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version.isEqual(new Version(0, 0, 0))) {
|
2024-01-07 01:24:09 +01:00
|
|
|
throw new Error(`
|
2024-02-07 03:46:38 +01:00
|
|
|
The ${extName[this.vectorExt]} extension version is ${version}, which means it is a nightly release.
|
|
|
|
|
|
|
|
Please run 'DROP EXTENSION IF EXISTS ${this.vectorExt}' and switch to a release version.
|
|
|
|
See https://immich.app/docs/guides/database-queries for how to query the database.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const minVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.minVectorVersion : this.minVectorsVersion;
|
|
|
|
const maxVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.vectorVersionPin : this.vectorsVersionPin;
|
|
|
|
|
|
|
|
if (version.isOlderThan(minVersion) || version.isNewerThan(minVersion) > maxVersion) {
|
|
|
|
const allowedReleaseType = maxVersion === VersionType.MAJOR ? '' : ` ${VersionType[maxVersion].toLowerCase()}`;
|
|
|
|
const releases =
|
|
|
|
maxVersion === VersionType.EQUAL
|
|
|
|
? minVersion.toString()
|
|
|
|
: `${minVersion} and later${allowedReleaseType} releases`;
|
|
|
|
|
|
|
|
throw new Error(`
|
|
|
|
The ${extName[this.vectorExt]} extension version is ${version}, but Immich only supports ${releases}.
|
|
|
|
|
|
|
|
If the Postgres instance already has a compatible version installed, Immich may not have the necessary permissions to activate it.
|
|
|
|
In this case, please run 'ALTER EXTENSION UPDATE ${this.vectorExt}' manually as a superuser.
|
|
|
|
See https://immich.app/docs/guides/database-queries for how to query the database.
|
|
|
|
|
|
|
|
Otherwise, please update the version of ${extName[this.vectorExt]} in the Postgres instance to a compatible version.`);
|
2024-01-07 01:24:09 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-21 17:06:26 +01:00
|
|
|
}
|