Skip to content

Commit

Permalink
feat: Add number parameter to getOrCreateSourceTextId function (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttizze authored Jul 14, 2024
1 parent 3889368 commit f9c3e95
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion web/app/routes/_index/utils/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function getOrCreateTranslations(
const untranslatedElements: { number: number; text: string }[] = [];
const sourceTextsId = await Promise.all(
elements.map((element) =>
getOrCreateSourceTextId(element.text, pageId, pageVersionId),
getOrCreateSourceTextId(element.text, element.number, pageId, pageVersionId),
),
);

Expand Down Expand Up @@ -188,6 +188,7 @@ async function translateUntranslatedElements(

const sourceTextId = await getOrCreateSourceTextId(
sourceText,
translation.number,
pageId,
pageVersionId,
);
Expand Down
7 changes: 5 additions & 2 deletions web/app/utils/sourceTextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { prisma } from "./prisma";

export async function getOrCreateSourceTextId(
text: string,
number: number,
pageId: number,
pageVersionId: number,
): Promise<number> {
Expand All @@ -14,14 +15,16 @@ export async function getOrCreateSourceTextId(
try {
const sourceText = await prisma.sourceText.upsert({
where: {
textHash_pageId: {
textHash_pageVersionId_number: {
textHash,
pageId,
pageVersionId,
number,
},
},
update: {},
create: {
text,
number,
textHash,
pageId,
pageVersionId,
Expand Down
15 changes: 15 additions & 0 deletions web/prisma/migrations/20240714130117_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Warnings:
- A unique constraint covering the columns `[text_hash,page_version_id,number]` on the table `source_texts` will be added. If there are existing duplicate values, this will fail.
- Added the required column `number` to the `source_texts` table without a default value. This is not possible if the table is not empty.
*/
-- DropIndex
DROP INDEX "source_texts_text_hash_page_id_key";

-- AlterTable
ALTER TABLE "source_texts" ADD COLUMN "number" INTEGER NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "source_texts_text_hash_page_version_id_number_key" ON "source_texts"("text_hash", "page_version_id", "number");
3 changes: 2 additions & 1 deletion web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ model PageVersion {
model SourceText {
id Int @id @default(autoincrement())
text String
number Int
textHash Bytes @map("text_hash")
pageId Int @map("page_id")
pageVersionId Int @map("page_version_id")
page Page @relation(fields: [pageId], references: [id])
pageVersion PageVersion @relation(fields: [pageVersionId], references: [id])
translateTexts TranslateText[]
@@unique([textHash, pageId])
@@unique([textHash, pageVersionId, number])
@@index([textHash])
@@map("source_texts")
}
Expand Down

0 comments on commit f9c3e95

Please sign in to comment.