Files
immich/server/src/domain/database/database.service.spec.ts

156 lines
6.6 KiB
TypeScript
Raw Normal View History

import { ImmichLogger } from '@app/infra/logger';
import { newDatabaseRepositoryMock } from '@test';
import { Version } from '../domain.constant';
import { DatabaseExtension, IDatabaseRepository } from '../repositories';
import { DatabaseService } from './database.service';
describe(DatabaseService.name, () => {
let sut: DatabaseService;
let databaseMock: jest.Mocked<IDatabaseRepository>;
let fatalLog: jest.SpyInstance;
beforeEach(async () => {
databaseMock = newDatabaseRepositoryMock();
fatalLog = jest.spyOn(ImmichLogger.prototype, 'fatal');
sut = new DatabaseService(databaseMock);
sut.minVectorsVersion = new Version(0, 1, 1);
sut.maxVectorsVersion = new Version(0, 1, 11);
});
afterEach(() => {
fatalLog.mockRestore();
});
it('should work', () => {
expect(sut).toBeDefined();
});
describe('init', () => {
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 return if minimum supported PostgreSQL and vectors version are installed', async () => {
databaseMock.getPostgresVersion.mockResolvedValueOnce(new Version(14, 0, 0));
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 1, 1));
await sut.init();
expect(databaseMock.getPostgresVersion).toHaveBeenCalledTimes(2);
expect(databaseMock.createExtension).toHaveBeenCalledWith(DatabaseExtension.VECTORS);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(fatalLog).not.toHaveBeenCalled();
});
it('should thrown an error if PostgreSQL version is below minimum supported version', async () => {
databaseMock.getPostgresVersion.mockResolvedValueOnce(new Version(13, 0, 0));
await expect(sut.init()).rejects.toThrow(/PostgreSQL/s);
expect(databaseMock.getPostgresVersion).toHaveBeenCalledTimes(1);
});
it('should return if minimum supported vectors version is installed', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 1, 1));
await sut.init();
expect(databaseMock.createExtension).toHaveBeenCalledWith(DatabaseExtension.VECTORS);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(fatalLog).not.toHaveBeenCalled();
});
it('should return if maximum supported vectors version is installed', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 1, 11));
await sut.init();
expect(databaseMock.createExtension).toHaveBeenCalledWith(DatabaseExtension.VECTORS);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).toHaveBeenCalledTimes(1);
expect(fatalLog).not.toHaveBeenCalled();
});
it('should throw an error if vectors version is not installed even after createVectors', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(null);
await expect(sut.init()).rejects.toThrow('Unexpected: The pgvecto.rs extension is not installed.');
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it('should throw an error if vectors version is below minimum supported version', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 0, 1));
await expect(sut.init()).rejects.toThrow(/('tensorchord\/pgvecto-rs:pg14-v0.1.11')/s);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it('should throw an error if vectors version is above maximum supported version', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 1, 12));
await expect(sut.init()).rejects.toThrow(
/('DROP EXTENSION IF EXISTS vectors').*('tensorchord\/pgvecto-rs:pg14-v0\.1\.11')/s,
);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it('should throw an error if vectors version is a nightly', async () => {
databaseMock.getExtensionVersion.mockResolvedValueOnce(new Version(0, 0, 0));
await expect(sut.init()).rejects.toThrow(
/(nightly).*('DROP EXTENSION IF EXISTS vectors').*('tensorchord\/pgvecto-rs:pg14-v0\.1\.11')/s,
);
expect(databaseMock.getExtensionVersion).toHaveBeenCalledTimes(1);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
it('should throw error if vectors extension could not be created', async () => {
databaseMock.createExtension.mockRejectedValueOnce(new Error('Failed to create extension'));
await expect(sut.init()).rejects.toThrow('Failed to create extension');
expect(fatalLog).toHaveBeenCalledTimes(1);
expect(fatalLog.mock.calls[0][0]).toMatch(/('tensorchord\/pgvecto-rs:pg14-v0\.1\.11').*(v1\.91\.0)/s);
expect(databaseMock.createExtension).toHaveBeenCalledTimes(1);
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
});
for (const major of [14, 15, 16]) {
it(`should suggest image with postgres ${major} if database is ${major}`, async () => {
databaseMock.getExtensionVersion.mockResolvedValue(new Version(0, 0, 1));
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.mockResolvedValue(new Version(major, 0, 0));
await expect(sut.init()).rejects.toThrow(new RegExp(`tensorchord\/pgvecto-rs:pg${major}-v0\\.1\\.11`, 's'));
});
}
it('should not suggest image if postgres version is not in 14, 15 or 16', 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(17, 0, 0));
databaseMock.getPostgresVersion.mockResolvedValueOnce(new Version(17, 0, 0));
await expect(sut.init()).rejects.toThrow(/^(?:(?!tensorchord\/pgvecto-rs).)*$/s);
});
it('should set the image to the maximum supported version', async () => {
databaseMock.getExtensionVersion.mockResolvedValue(new Version(0, 0, 1));
await expect(sut.init()).rejects.toThrow(/('tensorchord\/pgvecto-rs:pg14-v0\.1\.11')/s);
sut.maxVectorsVersion = new Version(0, 1, 12);
await expect(sut.init()).rejects.toThrow(/('tensorchord\/pgvecto-rs:pg14-v0\.1\.12')/s);
});
});
});