1
0
mirror of https://github.com/immich-app/immich.git synced 2025-06-16 03:40:33 +02:00
Files
immich/server/src/services/database.service.spec.ts

224 lines
9.7 KiB
TypeScript
Raw Normal View History

import { DatabaseExtension, IDatabaseRepository, VectorIndex } from 'src/interfaces/database.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { DatabaseService } from 'src/services/database.service';
2024-03-20 22:15:09 -05:00
import { Version, VersionType } from 'src/utils/version';
import { newDatabaseRepositoryMock } from 'test/repositories/database.repository.mock';
import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock';
import { Mocked } from 'vitest';
describe(DatabaseService.name, () => {
let sut: DatabaseService;
let databaseMock: Mocked<IDatabaseRepository>;
let loggerMock: Mocked<ILoggerRepository>;
beforeEach(() => {
delete process.env.DB_SKIP_MIGRATIONS;
databaseMock = newDatabaseRepositoryMock();
loggerMock = newLoggerRepositoryMock();
sut = new DatabaseService(databaseMock, loggerMock);
});
it('should work', () => {
expect(sut).toBeDefined();
});
describe.each([
[{ vectorExt: DatabaseExtension.VECTORS, extName: 'pgvecto.rs', minVersion: new Version(0, 1, 1) }],
[{ vectorExt: DatabaseExtension.VECTOR, extName: 'pgvector', minVersion: new Version(0, 5, 0) }],
] as const)('init', ({ vectorExt, extName, minVersion }) => {
beforeEach(() => {
databaseMock.getPreferredVectorExtension.mockReturnValue(vectorExt);
databaseMock.getExtensionVersion.mockResolvedValue(minVersion);
sut = new DatabaseService(databaseMock, loggerMock);
sut.minVectorVersion = minVersion;
sut.minVectorsVersion = minVersion;
sut.vectorVersionPin = VersionType.MINOR;
sut.vectorsVersionPin = VersionType.MINOR;
});
it(`should resolve successfully if minimum supported PostgreSQL and ${extName} version are installed`, async () => {
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
databaseMock.getPostgresVersion.mockResolvedValueOnce(new Version(14, 0, 0));
await expect(sut.init()).resolves.toBeUndefined();
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
expect(databaseMock.getPostgresVersion).toHaveBeenCalled();
expect(databaseMock.createExtension).toHaveBeenCalledWith(vectorExt);
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.getExtensionVersion).toHaveBeenCalled();
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
});
it('should throw an error if PostgreSQL version is below minimum supported version', async () => {
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
databaseMock.getPostgresVersion.mockResolvedValueOnce(new Version(13, 0, 0));
await expect(sut.init()).rejects.toThrow('PostgreSQL version is 13');
feat(server): add postgres major version check (#6213) * feat(server): Throw error when PostgreSQL version is not within the supported versions The pgvecto.rs extension, though not distributed, can be built for PostgreSQL 12 and 13. An installation of PostgreSQL 12 with the pgvecto.rs extensions installed will not be caught by immich. This causes immich to attempt to run the database migrations without having a proper environment. With assertPostgresql the server will throw an error if the PostgreSQL version is not within the supported range. * Replaced assertion with lesser than comparison As requested by @zackpollard * Changed the comparison to use the minPostgresVersion variable. If we define one we might as well use it. makes changing the versioning later easier * Added two new tests, modified two existing tests `should return if minimum supported PostgreSQL and vectors version are installed`: Check if init returns properly and that getPostgresVersion is called twice `should thrown an error if PostgreSQL version is below minimum supported version`: Checks if the init function correctly returns an error `should suggest image with postgres ${major} if database is ${major}`: Modified to set MockResolvedValue instead of MockResolvedValueOnce. With the new check we get the PostgreSQL version twice. So it needs to be set during the entire test. `should not suggest image if postgres version is not in 14, 15 or 16`: Modified the bounds to [14, 18]. Because values below 14 now will not get called. Also Modified to call `getPostgresVersion.MockResolvedValueOnce` for twice, because it gets called twice. * Fixed two mistakes in the jest functions from previous commit #2abcb60 `should thrown an error if PostgreSQL version is below minimum supported version`: The regex function I wrote mistakingly used the negate function which check that the error *did not* contain the phrase "PostgreSQL". Which is the opposite `should not suggest image if postgres version is not in 14, 15 or 16`: confused bounds for a normal javascript array. Changed the test to only check for values above 16. As values below 14 will get thrown out by test `should return if minimum supported PostgreSQL and vectors version are installed` I apologise for the mistakes in my previous commit. * Format fix --------- Co-authored-by: max <wak@vanling.net>
2024-01-07 00:24:09 +00:00
expect(databaseMock.getPostgresVersion).toHaveBeenCalledTimes(1);
});
it(`should resolve successfully if minimum supported ${extName} version is installed`, async () => {
await expect(sut.init()).resolves.toBeUndefined();
expect(databaseMock.createExtension).toHaveBeenCalledWith(vectorExt);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
});
it(`should throw an error if ${extName} version is not installed even after createVectorExtension`, async () => {
databaseMock.getExtensionVersion.mockResolvedValue(null);
await expect(sut.init()).rejects.toThrow(`Unexpected: ${extName} extension is not installed.`);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it(`should throw an error if ${extName} version is below minimum supported version`, async () => {
databaseMock.getExtensionVersion.mockResolvedValue(
new Version(minVersion.major, minVersion.minor - 1, minVersion.patch),
);
await expect(sut.init()).rejects.toThrow(extName);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it.each([
{ type: VersionType.EQUAL, max: 'no', actual: 'patch' },
{ type: VersionType.PATCH, max: 'patch', actual: 'minor' },
{ type: VersionType.MINOR, max: 'minor', actual: 'major' },
] as const)(
`should throw an error if $max upgrade from min version is allowed and ${extName} version is $actual`,
async ({ type, actual }) => {
const version = new Version(minVersion.major, minVersion.minor, minVersion.patch);
version[actual] = minVersion[actual] + 1;
databaseMock.getExtensionVersion.mockResolvedValue(version);
if (vectorExt === DatabaseExtension.VECTOR) {
sut.minVectorVersion = minVersion;
sut.vectorVersionPin = type;
} else {
sut.minVectorsVersion = minVersion;
sut.vectorsVersionPin = type;
}
await expect(sut.init()).rejects.toThrow(extName);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
},
);
it(`should throw an error if ${extName} version is a nightly`, async () => {
databaseMock.getExtensionVersion.mockResolvedValue(new Version(0, 0, 0));
await expect(sut.init()).rejects.toThrow(extName);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it(`should throw error if ${extName} extension could not be created`, async () => {
databaseMock.createExtension.mockRejectedValue(new Error('Failed to create extension'));
await expect(sut.init()).rejects.toThrow('Failed to create extension');
expect(loggerMock.fatal).toHaveBeenCalledTimes(1);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it(`should update ${extName} if a newer version is available`, async () => {
const version = new Version(minVersion.major, minVersion.minor + 1, minVersion.patch);
databaseMock.getAvailableExtensionVersion.mockResolvedValue(version);
await expect(sut.init()).resolves.toBeUndefined();
expect(databaseMock.updateVectorExtension).toHaveBeenCalledWith(vectorExt, version);
expect(databaseMock.updateVectorExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
});
it(`should not update ${extName} if a newer version is higher than the maximum`, async () => {
const version = new Version(minVersion.major + 1, minVersion.minor, minVersion.patch);
databaseMock.getAvailableExtensionVersion.mockResolvedValue(version);
await expect(sut.init()).resolves.toBeUndefined();
expect(databaseMock.updateVectorExtension).not.toHaveBeenCalled();
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
});
it(`should warn if attempted to update ${extName} and failed`, async () => {
const version = new Version(minVersion.major, minVersion.minor, minVersion.patch + 1);
databaseMock.getAvailableExtensionVersion.mockResolvedValue(version);
databaseMock.updateVectorExtension.mockRejectedValue(new Error('Failed to update extension'));
await expect(sut.init()).resolves.toBeUndefined();
expect(loggerMock.warn).toHaveBeenCalledTimes(1);
expect(loggerMock.warn.mock.calls[0][0]).toContain(extName);
expect(loggerMock.error).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
expect(databaseMock.updateVectorExtension).toHaveBeenCalledWith(vectorExt, version);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
});
it(`should warn if ${extName} update requires restart`, async () => {
const version = new Version(minVersion.major, minVersion.minor, minVersion.patch + 1);
databaseMock.getAvailableExtensionVersion.mockResolvedValue(version);
databaseMock.updateVectorExtension.mockResolvedValue({ restartRequired: true });
await expect(sut.init()).resolves.toBeUndefined();
expect(loggerMock.warn).toHaveBeenCalledTimes(1);
expect(loggerMock.warn.mock.calls[0][0]).toContain(extName);
expect(databaseMock.updateVectorExtension).toHaveBeenCalledWith(vectorExt, version);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
});
it.each([{ index: VectorIndex.CLIP }, { index: VectorIndex.FACE }])(
`should reindex $index if necessary`,
async ({ index }) => {
databaseMock.shouldReindex.mockImplementation((indexArg) => Promise.resolve(indexArg === index));
await expect(sut.init()).resolves.toBeUndefined();
expect(databaseMock.shouldReindex).toHaveBeenCalledWith(index);
expect(databaseMock.shouldReindex).toHaveBeenCalledTimes(2);
expect(databaseMock.reindex).toHaveBeenCalledWith(index);
expect(databaseMock.reindex).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(loggerMock.fatal).not.toHaveBeenCalled();
},
);
it.each([{ index: VectorIndex.CLIP }, { index: VectorIndex.FACE }])(
`should not reindex $index if not necessary`,
async () => {
databaseMock.shouldReindex.mockResolvedValue(false);
await expect(sut.init()).resolves.toBeUndefined();
expect(databaseMock.shouldReindex).toHaveBeenCalledTimes(2);
expect(databaseMock.reindex).not.toHaveBeenCalled();
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
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();
});
});
});