feat: test for non-standard database name (#19386)

This commit is contained in:
Jason Rasmussen 2025-06-20 15:31:16 -04:00 committed by GitHub
parent c707f9cef4
commit 1a90fc8e58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -7,12 +7,13 @@ import { getKyselyConfig } from 'src/utils/database';
import { GenericContainer, Wait } from 'testcontainers'; import { GenericContainer, Wait } from 'testcontainers';
const globalSetup = async () => { const globalSetup = async () => {
const templateName = 'mich';
const postgresContainer = await new GenericContainer('ghcr.io/immich-app/postgres:14-vectorchord0.4.3') const postgresContainer = await new GenericContainer('ghcr.io/immich-app/postgres:14-vectorchord0.4.3')
.withExposedPorts(5432) .withExposedPorts(5432)
.withEnvironment({ .withEnvironment({
POSTGRES_PASSWORD: 'postgres', POSTGRES_PASSWORD: 'postgres',
POSTGRES_USER: 'postgres', POSTGRES_USER: 'postgres',
POSTGRES_DB: 'immich', POSTGRES_DB: templateName,
}) })
.withCommand([ .withCommand([
'postgres', 'postgres',
@ -35,7 +36,7 @@ const globalSetup = async () => {
.start(); .start();
const postgresPort = postgresContainer.getMappedPort(5432); const postgresPort = postgresContainer.getMappedPort(5432);
const postgresUrl = `postgres://postgres:postgres@localhost:${postgresPort}/immich`; const postgresUrl = `postgres://postgres:postgres@localhost:${postgresPort}/${templateName}`;
process.env.IMMICH_TEST_POSTGRES_URL = postgresUrl; process.env.IMMICH_TEST_POSTGRES_URL = postgresUrl;

View File

@ -373,18 +373,23 @@ function* newPngFactory() {
const pngFactory = newPngFactory(); const pngFactory = newPngFactory();
const withDatabase = (url: string, name: string) => url.replace('/immich', `/${name}`); const templateName = 'mich';
const withDatabase = (url: string, name: string) => url.replace(`/${templateName}`, `/${name}`);
export const getKyselyDB = async (suffix?: string): Promise<Kysely<DB>> => { export const getKyselyDB = async (suffix?: string): Promise<Kysely<DB>> => {
const testUrl = process.env.IMMICH_TEST_POSTGRES_URL!; const testUrl = process.env.IMMICH_TEST_POSTGRES_URL!;
const sql = postgres({ const sql = postgres({
...asPostgresConnectionConfig({ connectionType: 'url', url: withDatabase(testUrl, 'postgres') }), ...asPostgresConnectionConfig({
connectionType: 'url',
url: withDatabase(testUrl, 'postgres'),
}),
max: 1, max: 1,
}); });
const randomSuffix = Math.random().toString(36).slice(2, 7); const randomSuffix = Math.random().toString(36).slice(2, 7);
const dbName = `immich_${suffix ?? randomSuffix}`; const dbName = `immich_${suffix ?? randomSuffix}`;
await sql.unsafe(`CREATE DATABASE ${dbName} WITH TEMPLATE immich OWNER postgres;`); await sql.unsafe(`CREATE DATABASE ${dbName} WITH TEMPLATE ${templateName} OWNER postgres;`);
return new Kysely<DB>(getKyselyConfig({ connectionType: 'url', url: withDatabase(testUrl, dbName) })); return new Kysely<DB>(getKyselyConfig({ connectionType: 'url', url: withDatabase(testUrl, dbName) }));
}; };