mastra-ai/mastra · error

Repository ${id} not found in organization ${orgId}

Error message

Repository ${id} not found in organization ${orgId}

What it means

Thrown inside migrateInstallation when the repository identified by { orgId, id } does not exist. Migration of a repository to a new installation is only valid for an existing repository row.

Source

Thrown at mastracode/factory/src/storage/domains/source-control/base.ts:698

        },
        upsert: async ({ orgId, input }) => {
          await requireInstallation({ orgId, id: input.installationId });
          const now = new Date();
          const row = await db().upsertOne<RepositoryDbRow>(REPOSITORIES, ['installation_id', 'external_id'], {
            installation_id: input.installationId,
            external_id: input.externalId,
            slug: input.slug,
            default_branch: input.defaultBranch,
            provider_metadata: input.providerMetadata ?? {},
            created_at: now,
            updated_at: now,
          });
          return toRepository(row);
        },
        migrateInstallation: async ({ orgId, id, newInstallationId }) => {
          const existing = await getRepository({ orgId, id });
          if (!existing) {
            throw new Error(`Repository ${id} not found in organization ${orgId}`);
          }
          await requireInstallation({ orgId, id: newInstallationId });
          try {
            await db().updateMany(REPOSITORIES, { id }, { installation_id: newInstallationId, updated_at: new Date() });
            // Migrate dependent connections to the new installation
            await db().updateMany(
              CONNECTIONS,
              { installation_id: existing.installationId },
              { installation_id: newInstallationId },
            );
            // Return the updated repository
            const updated = await getRepository({ orgId, id });
            if (!updated) throw new Error('Repository disappeared after update');
            return updated;
          } catch (error) {
            // Unique constraint violation: repository already exists under the new installation
            if (!(error instanceof UniqueViolationError)) throw error;
            // Find and return the existing repository under the new installation

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the repository exists via repository.get before migrating
  2. Create/upsert the repository first if missing
  3. Confirm orgId matches the org the repository was created under

Example fix

// before
await api.migrateInstallation({ orgId, id: repoId, newInstallationId });
// after
if (!(await api.getRepository({ orgId, id: repoId }))) {
  throw new Error(`Repository ${repoId} missing; cannot migrate`);
}
await api.migrateInstallation({ orgId, id: repoId, newInstallationId });
Defensive patterns

Strategy: validation

Validate before calling

const existing = await getRepository({ orgId, id });
if (!existing) throw new Error(`Cannot migrate: repository ${id} not found`);

Type guard

function isMigratable(v: SourceControlRepository | null): v is SourceControlRepository {
  return v !== null && typeof v.installationId === 'string';
}

Try / catch

try {
  await migrateInstallation({ orgId, id, newInstallationId });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Repository')) {
    await upsertRepository({ orgId, installationId: oldInstallationId, externalId, name });
    await migrateInstallation({ orgId, id, newInstallationId });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling migrateInstallation with a repository id that was never created, was deleted, or belongs to another org.

Common situations: Rotating a GitHub App installation with a stale repository id; migrating repos across orgs; referencing a repo created under a different integration.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/b3c835e3e0c3092a. Report an issue: GitHub.