-
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.
Recompute changes on commit to find all indirect changes and syntax e…
…rrors (#53) * Recompute all draft changes when updating any document * Changed recompute on demand
- Loading branch information
Showing
12 changed files
with
488 additions
and
228 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,39 @@ | ||
import { | ||
DocumentVersion, | ||
documentVersions, | ||
getDocumentsAtCommit, | ||
Result, | ||
Transaction, | ||
} from '@latitude-data/core' | ||
import { findCommitById, getDocumentsAtCommit } from '$core/data-access' | ||
import { Result, Transaction, TypedResult } from '$core/lib' | ||
import { BadRequestError } from '$core/lib/errors' | ||
|
||
import { | ||
assertCommitIsEditable, | ||
existsAnotherDocumentWithSamePath, | ||
} from './utils' | ||
import { DocumentVersion, documentVersions } from '$core/schema' | ||
|
||
export async function createNewDocument({ | ||
commitId, | ||
path, | ||
content, | ||
}: { | ||
commitId: number | ||
path: string | ||
}) { | ||
const commitResult = await assertCommitIsEditable(commitId) | ||
if (commitResult.error) return commitResult | ||
|
||
const currentDocuments = await getDocumentsAtCommit({ | ||
commitId, | ||
}) | ||
if (currentDocuments.error) return currentDocuments | ||
content?: string | ||
}): Promise<TypedResult<DocumentVersion, Error>> { | ||
return await Transaction.call(async (tx) => { | ||
const commit = (await findCommitById({ id: commitId }, tx)).unwrap() | ||
if (commit.mergedAt !== null) { | ||
return Result.error(new BadRequestError('Cannot modify a merged commit')) | ||
} | ||
|
||
if ( | ||
existsAnotherDocumentWithSamePath({ | ||
documents: currentDocuments.value, | ||
path, | ||
}) | ||
) { | ||
return Result.error( | ||
new BadRequestError('A document with the same path already exists'), | ||
) | ||
} | ||
const currentDocs = (await getDocumentsAtCommit({ commitId }, tx)).unwrap() | ||
if (currentDocs.find((d) => d.path === path)) { | ||
return Result.error( | ||
new BadRequestError('A document with the same path already exists'), | ||
) | ||
} | ||
|
||
return Transaction.call<DocumentVersion>(async (tx) => { | ||
const result = await tx | ||
const newDoc = await tx | ||
.insert(documentVersions) | ||
.values({ | ||
commitId, | ||
path, | ||
content: content ?? '', | ||
}) | ||
.returning() | ||
const documentVersion = result[0] | ||
return Result.ok(documentVersion!) | ||
|
||
return Result.ok(newDoc[0]!) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './create' | ||
export * from './update' | ||
export * from './recomputeChanges' |
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 { CompileError } from '@latitude-data/compiler' | ||
import { database } from '$core/client' | ||
import { findCommitById } from '$core/data-access' | ||
import { Result, TypedResult } from '$core/lib' | ||
import { BadRequestError } from '$core/lib/errors' | ||
import { DocumentVersion } from '$core/schema' | ||
|
||
import { | ||
getMergedAndDraftDocuments, | ||
replaceCommitChanges, | ||
resolveDocumentChanges, | ||
} from './utils' | ||
|
||
type RecomputedChanges = { | ||
documents: DocumentVersion[] | ||
errors: { [documentUuid: string]: CompileError[] } | ||
} | ||
|
||
export async function recomputeChanges( | ||
{ | ||
commitId, | ||
}: { | ||
commitId: number | ||
}, | ||
tx = database, | ||
): Promise<TypedResult<RecomputedChanges, Error>> { | ||
try { | ||
const draft = (await findCommitById({ id: commitId }, tx)).unwrap() | ||
if (draft.mergedAt !== null) { | ||
return Result.error( | ||
new BadRequestError('Cannot recompute changes in a merged commit'), | ||
) | ||
} | ||
|
||
const [mergedDocuments, draftDocuments] = ( | ||
await getMergedAndDraftDocuments( | ||
{ | ||
draft, | ||
}, | ||
tx, | ||
) | ||
).unwrap() | ||
|
||
const { documents: documentsToUpdate, errors } = | ||
await resolveDocumentChanges({ | ||
originalDocuments: mergedDocuments, | ||
newDocuments: draftDocuments, | ||
}) | ||
|
||
const newDraftDocuments = ( | ||
await replaceCommitChanges( | ||
{ | ||
commitId, | ||
documentChanges: documentsToUpdate, | ||
}, | ||
tx, | ||
) | ||
).unwrap() | ||
|
||
return Result.ok({ documents: newDraftDocuments, errors }) | ||
} catch (error) { | ||
return Result.error(error as Error) | ||
} | ||
} |
Oops, something went wrong.