fix: skip initial kysely migration for existing installs (#17690)

* fix: skip initial kysely migration for existing installs

* Update docs/src/pages/errors.md

---------

Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
Jason Rasmussen 2025-04-18 12:19:11 -04:00 committed by GitHub
parent 6474a78b8b
commit 160bb492a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 2 deletions

5
docs/src/pages/errors.md Normal file
View File

@ -0,0 +1,5 @@
# Errors
## TypeORM Upgrade
The upgrade to Immich `v2.x.x` has a required upgrade path to `v1.132.0+`. This means it is required to start up the application at least once on version `1.132.0` (or later). Doing so will complete database schema upgrades that are required for `v2.0.0`. After Immich has successfully booted on this version, shut the system down and try the `v2.x.x` upgrade again.

View File

@ -74,11 +74,21 @@ export class MyConsoleLogger extends ConsoleLogger {
export class LoggingRepository {
private logger: MyConsoleLogger;
constructor(@Inject(ClsService) cls: ClsService | undefined, configRepository: ConfigRepository) {
const { noColor } = configRepository.getEnv();
constructor(
@Inject(ClsService) cls: ClsService | undefined,
@Inject(ConfigRepository) configRepository: ConfigRepository | undefined,
) {
let noColor = false;
if (configRepository) {
noColor = configRepository.getEnv().noColor;
}
this.logger = new MyConsoleLogger(cls, { context: LoggingRepository.name, color: !noColor });
}
static create() {
return new LoggingRepository(undefined, undefined);
}
setAppName(name: string): void {
appName = name.charAt(0).toUpperCase() + name.slice(1);
}

View File

@ -1,10 +1,29 @@
import { Kysely, sql } from 'kysely';
import { DatabaseExtension } from 'src/enum';
import { ConfigRepository } from 'src/repositories/config.repository';
import { LoggingRepository } from 'src/repositories/logging.repository';
const vectorExtension = new ConfigRepository().getEnv().database.vectorExtension;
const lastMigrationSql = sql<{ name: string }>`SELECT "name" FROM "migrations" ORDER BY "timestamp" DESC LIMIT 1;`;
const tableExists = sql<{ result: string | null }>`select to_regclass('migrations') as "result"`;
const logger = LoggingRepository.create();
export async function up(db: Kysely<any>): Promise<void> {
const { rows } = await tableExists.execute(db);
const hasTypeOrmMigrations = !!rows[0]?.result;
if (hasTypeOrmMigrations) {
const {
rows: [lastMigration],
} = await lastMigrationSql.execute(db);
if (lastMigration?.name !== 'AddMissingIndex1744910873956') {
throw new Error(
'Invalid upgrade path. For more information, see https://immich.app/errors#typeorm-upgrade',
);
}
logger.log('Database has up to date TypeORM migrations, skipping initial Kysely migration');
return;
}
await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`.execute(db);
await sql`CREATE EXTENSION IF NOT EXISTS "unaccent";`.execute(db);
await sql`CREATE EXTENSION IF NOT EXISTS "cube";`.execute(db);