{"record":{"id":"143b0a5e13c643c6","repo":"immich-app/immich","slug":"migration-result-migrationname-failed","errorCode":null,"errorMessage":"Migration \"${result.migrationName}\" failed","messagePattern":"Migration \"(.+?)\" failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/src/repositories/database.repository.ts","lineNumber":392,"sourceCode":"    const { count } = await this.db\n      .selectFrom(this.db.dynamic.table(table).as('t'))\n      .select((eb) => eb.fn.countAll<number>().as('count'))\n      .executeTakeFirstOrThrow();\n    return count;\n  }\n\n  async runMigrations(): Promise<number> {\n    this.logger.log('Running migrations');\n\n    const migrator = this.createMigrator();\n\n    const { error, results = [] } = await migrator.migrateToLatest();\n\n    for (const result of results) {\n      if (result.status === 'Success') {\n        this.logger.log(`Migration \"${result.migrationName}\" succeeded`);\n      } else if (result.status === 'Error') {\n        this.logger.warn(`Migration \"${result.migrationName}\" failed`);\n      }\n    }\n\n    if (error) {\n      this.logger.error(`Migrations failed: ${error}`);\n\n      const missing =\n        error instanceof Error ? error.message.match(/previously executed migration (.+) is missing/u) : null;\n      if (missing) {\n        throw new Error(\n          `Migration \"${missing[1]}\" was already applied to this database but is not in this version of Immich (${serverVersion}). ` +\n            `This usually means the database was migrated by a newer version. Downgrades are not supported.`,\n          { cause: error },\n        );\n      }\n\n      throw error;\n    }","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/immich-app/immich/blob/e55ac299a4ec7cb372e35dbf2c6c05ee9ce77f6c/server/src/repositories/database.repository.ts#L374-L410","documentation":"After running Kysely migrations to latest, the repository inspects each migration result. A migration with status 'Error' logs this warning; the accompanying migrator error is then logged and rethrown downstream. It signals that a specific named migration failed to apply.","triggerScenarios":"runMigrations() calls migrator.migrateToLatest() and one or more migration scripts failed — e.g. SQL error, lock timeout, or a migration already half-applied.","commonSituations":"Upgrading Immich versions where a new migration conflicts with existing data/schema; database permissions missing for ALTER/CREATE; partially applied migration from a crashed previous run; slow migrations killed by connection timeout.","solutions":["Read the accompanying 'Migrations failed: <error>' log for the root cause and fix the underlying SQL/data issue","Restore from backup and retry the upgrade if the schema is half-migrated","Ensure the database user has CREATE/ALTER privileges in the public schema","Increase statement timeouts (statement_timeout=0) for long migrations and restart the server to retry"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// pre-flight privilege check before migrating\nawait sql`SELECT has_schema_privilege(current_user, 'public', 'CREATE') AS ok`.execute(db);\nawait sql`SELECT has_table_privilege(current_user, 'migrations', 'INSERT') AS ok`.execute(db);","typeGuard":null,"tryCatchPattern":"try {\n  await db.runMigrations();\n} catch (err) {\n  logger.error(`Migration failed: ${err}. Fix root cause or restore backup before restarting.`);\n  process.exit(1); // don't boot a half-migrated server\n}","preventionTips":["Take a DB snapshot before every Immich upgrade","Ensure the DB user has CREATE/ALTER privileges","Set statement_timeout=0 for the migration session","Read release notes for breaking migrations before upgrading"],"tags":["database","migration","kysely","postgres"],"backgroundTag":"database-write-failed","analyzedSha":"e55ac299a4ec7cb372e35dbf2c6c05ee9ce77f6c","analyzedAt":"2026-09-15T07:20:19.675Z","contentChangedAt":"2026-09-15T07:20:19.675Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}