mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
feat: initial kysely migration file (#17678)
This commit is contained in:
parent
e275f2d8b3
commit
6474a78b8b
13 changed files with 499 additions and 139 deletions
|
|
@ -1,10 +1,11 @@
|
|||
import { FileMigrationProvider, Kysely, Migrator } from 'kysely';
|
||||
import { mkdir, readdir } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { Kysely } from 'kysely';
|
||||
import { parse } from 'pg-connection-string';
|
||||
import { DB } from 'src/db';
|
||||
import { ConfigRepository } from 'src/repositories/config.repository';
|
||||
import { DatabaseRepository } from 'src/repositories/database.repository';
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { getKyselyConfig } from 'src/utils/database';
|
||||
import { GenericContainer, Wait } from 'testcontainers';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
const globalSetup = async () => {
|
||||
const postgresContainer = await new GenericContainer('tensorchord/pgvecto-rs:pg14-v0.2.0')
|
||||
|
|
@ -36,66 +37,23 @@ const globalSetup = async () => {
|
|||
|
||||
const postgresPort = postgresContainer.getMappedPort(5432);
|
||||
const postgresUrl = `postgres://postgres:postgres@localhost:${postgresPort}/immich`;
|
||||
const parsed = parse(postgresUrl);
|
||||
|
||||
process.env.IMMICH_TEST_POSTGRES_URL = postgresUrl;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
const modules = import.meta.glob('/src/migrations/*.ts', { eager: true });
|
||||
|
||||
const config = {
|
||||
type: 'postgres' as const,
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
migrations: Object.values(modules).map((module) => Object.values(module)[0]),
|
||||
migrationsRun: false,
|
||||
synchronize: false,
|
||||
connectTimeoutMS: 10_000, // 10 seconds
|
||||
parseInt8: true,
|
||||
url: postgresUrl,
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
const dataSource = new DataSource(config);
|
||||
await dataSource.initialize();
|
||||
await dataSource.runMigrations();
|
||||
await dataSource.destroy();
|
||||
|
||||
// for whatever reason, importing from test/utils causes vitest to crash
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
const migrationFolder = join(__dirname, '..', 'schema/migrations');
|
||||
// TODO remove after we have at least one kysely migration
|
||||
await mkdir(migrationFolder, { recursive: true });
|
||||
|
||||
const parsed = parse(process.env.IMMICH_TEST_POSTGRES_URL!);
|
||||
|
||||
const parsedOptions = {
|
||||
...parsed,
|
||||
ssl: false,
|
||||
host: parsed.host ?? undefined,
|
||||
port: parsed.port ? Number(parsed.port) : undefined,
|
||||
database: parsed.database ?? undefined,
|
||||
};
|
||||
|
||||
const db = new Kysely(getKyselyConfig(parsedOptions));
|
||||
|
||||
// TODO just call `databaseRepository.migrate()` (probably have to wait until TypeOrm is gone)
|
||||
const migrator = new Migrator({
|
||||
db,
|
||||
migrationLockTableName: 'kysely_migrations_lock',
|
||||
migrationTableName: 'kysely_migrations',
|
||||
provider: new FileMigrationProvider({
|
||||
fs: { readdir },
|
||||
path: { join },
|
||||
migrationFolder,
|
||||
const db = new Kysely<DB>(
|
||||
getKyselyConfig({
|
||||
...parsed,
|
||||
ssl: false,
|
||||
host: parsed.host ?? undefined,
|
||||
port: parsed.port ? Number(parsed.port) : undefined,
|
||||
database: parsed.database ?? undefined,
|
||||
}),
|
||||
});
|
||||
);
|
||||
|
||||
const { error } = await migrator.migrateToLatest();
|
||||
if (error) {
|
||||
console.error('Unable to run kysely migrations', error);
|
||||
throw error;
|
||||
}
|
||||
const configRepository = new ConfigRepository();
|
||||
const logger = new LoggingRepository(undefined, configRepository);
|
||||
await new DatabaseRepository(db, logger, configRepository).runMigrations();
|
||||
|
||||
await db.destroy();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue