-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
378 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use server' | ||
|
||
import { CommitsRepository } from '@latitude-data/core/repositories' | ||
import { renameDocumentPaths } from '@latitude-data/core/services/documents/renameDocumentPaths' | ||
import { z } from 'zod' | ||
|
||
import { withProject } from '../procedures' | ||
|
||
export const renameDocumentPathsAction = withProject | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
commitUuid: z.string(), | ||
oldPath: z.string(), | ||
newPath: z.string(), | ||
}), | ||
{ type: 'json' }, | ||
) | ||
.handler(async ({ input, ctx }) => { | ||
const commitsScope = new CommitsRepository(ctx.project.workspaceId) | ||
const commit = await commitsScope | ||
.getCommitByUuid({ uuid: input.commitUuid, projectId: ctx.project.id }) | ||
.then((r) => r.unwrap()) | ||
|
||
const result = await renameDocumentPaths({ | ||
commit, | ||
oldPath: input.oldPath, | ||
newPath: input.newPath, | ||
}) | ||
|
||
return result.unwrap() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/core/src/services/documents/renameDocumentPaths.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { Providers } from '../../constants' | ||
import { BadRequestError } from '../../lib' | ||
import { DocumentVersionsRepository } from '../../repositories' | ||
import * as factories from '../../tests/factories' | ||
import { renameDocumentPaths } from './renameDocumentPaths' | ||
|
||
describe('renameDocumentPaths', () => { | ||
it('renames a single document path', async () => { | ||
const { project, user } = await factories.createProject({ | ||
providers: [{ type: Providers.OpenAI, name: 'openai' }], | ||
documents: { | ||
'a/b': factories.helpers.createPrompt({ provider: 'openai' }), | ||
'a/b/c': factories.helpers.createPrompt({ provider: 'openai' }), | ||
}, | ||
}) | ||
const { commit: draft } = await factories.createDraft({ project, user }) | ||
|
||
const result = await renameDocumentPaths({ | ||
commit: draft, | ||
oldPath: 'a/b', // a/b/c should not be affected, since I'm only renaming a file | ||
newPath: 'new/path', | ||
}) | ||
|
||
expect(result.ok).toBeTruthy() | ||
|
||
const docsScope = new DocumentVersionsRepository(project.workspaceId) | ||
const docs = await docsScope | ||
.getDocumentsAtCommit(draft) | ||
.then((r) => r.unwrap()) | ||
const paths = docs.map((d) => d.path).sort() | ||
expect(paths).toEqual(['a/b/c', 'new/path']) | ||
}) | ||
|
||
it('renames a folder path', async () => { | ||
const { project, user } = await factories.createProject({ | ||
providers: [{ type: Providers.OpenAI, name: 'openai' }], | ||
documents: { | ||
'a/b/c': factories.helpers.createPrompt({ provider: 'openai' }), | ||
'a/b/c/d': factories.helpers.createPrompt({ provider: 'openai' }), | ||
'not/affected': factories.helpers.createPrompt({ provider: 'openai' }), | ||
}, | ||
}) | ||
const { commit: draft } = await factories.createDraft({ project, user }) | ||
|
||
const result = await renameDocumentPaths({ | ||
commit: draft, | ||
oldPath: 'a/b/', | ||
newPath: 'newpath/', | ||
}) | ||
|
||
expect(result.ok).toBeTruthy() | ||
|
||
const docsScope = new DocumentVersionsRepository(project.workspaceId) | ||
const docs = await docsScope | ||
.getDocumentsAtCommit(draft) | ||
.then((r) => r.unwrap()) | ||
const paths = docs.map((d) => d.path).sort() | ||
expect(paths).toEqual(['newpath/c', 'newpath/c/d', 'not/affected']) | ||
}) | ||
|
||
it('fails when trying to rename a folder as a document', async () => { | ||
const { project, user } = await factories.createProject({ | ||
providers: [{ type: Providers.OpenAI, name: 'openai' }], | ||
documents: { | ||
'a/b': factories.helpers.createPrompt({ provider: 'openai' }), | ||
}, | ||
}) | ||
const { commit: draft } = await factories.createDraft({ project, user }) | ||
|
||
const result = await renameDocumentPaths({ | ||
commit: draft, | ||
oldPath: 'a/', | ||
newPath: 'new/path', | ||
}) | ||
|
||
expect(result.error).toBeInstanceOf(BadRequestError) | ||
}) | ||
}) |
64 changes: 64 additions & 0 deletions
64
packages/core/src/services/documents/renameDocumentPaths.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Commit, DocumentVersion } from '../../browser' | ||
import { database } from '../../client' | ||
import { findWorkspaceFromCommit } from '../../data-access' | ||
import { Result, Transaction, TypedResult } from '../../lib' | ||
import { BadRequestError } from '../../lib/errors' | ||
import { DocumentVersionsRepository } from '../../repositories' | ||
import { updateDocument } from './update' | ||
|
||
export async function renameDocumentPaths( | ||
{ | ||
commit, | ||
oldPath, | ||
newPath, | ||
}: { | ||
commit: Commit | ||
oldPath: string | ||
newPath: string | ||
}, | ||
db = database, | ||
): Promise<TypedResult<DocumentVersion[], Error>> { | ||
return await Transaction.call(async (tx) => { | ||
if (commit.mergedAt !== null) { | ||
return Result.error(new BadRequestError('Cannot modify a merged commit')) | ||
} | ||
|
||
if (oldPath.endsWith('/') !== newPath.endsWith('/')) { | ||
return Result.error( | ||
new BadRequestError( | ||
'Trying to rename a folder as a document or vice versa', | ||
), | ||
) | ||
} | ||
|
||
const workspace = await findWorkspaceFromCommit(commit, tx) | ||
const docsScope = new DocumentVersionsRepository(workspace!.id, tx) | ||
|
||
const currentDocs = await docsScope | ||
.getDocumentsAtCommit(commit) | ||
.then((r) => r.unwrap()) | ||
|
||
const docsToUpdate = currentDocs.filter((d) => | ||
oldPath.endsWith('/') ? d.path.startsWith(oldPath) : d.path === oldPath, | ||
) | ||
|
||
const updatedDocs = await Promise.all( | ||
docsToUpdate.map(async (document) => { | ||
const updatedPath = newPath + document.path.slice(oldPath.length) | ||
// A simple replace would also replace other instances of oldPath in the path | ||
// For example, relpacing "a" to "b" in "a/name" would result in "b/nbme" | ||
const updatedDoc = await updateDocument( | ||
{ | ||
commit, | ||
document, | ||
path: updatedPath, | ||
}, | ||
tx, | ||
) | ||
return updatedDoc.unwrap() | ||
}), | ||
) | ||
|
||
return Result.ok(updatedDocs) | ||
}, db) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.