Skip to content

Commit

Permalink
refactor ambigous names
Browse files Browse the repository at this point in the history
  • Loading branch information
harishdeivanayagam committed Jan 12, 2025
1 parent 9dda909 commit 1454aa7
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AddForeignKey
ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sourceIndexId_fkey" FOREIGN KEY ("sourceIndexId") REFERENCES "IndexedSource"("id") ON DELETE SET NULL ON UPDATE CASCADE;
51 changes: 51 additions & 0 deletions prisma/migrations/20250112112049_rename_cols_sheet/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Warnings:
- The primary key for the `SheetColumnValue` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `columnId` on the `SheetColumnValue` table. All the data in the column will be lost.
- You are about to drop the column `sourceId` on the `SheetColumnValue` table. All the data in the column will be lost.
- You are about to drop the column `sourceIndexId` on the `SheetColumnValue` table. All the data in the column will be lost.
- Added the required column `updatedAt` to the `SheetColumn` table without a default value. This is not possible if the table is not empty.
- Added the required column `sheetColumnId` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty.
- Added the required column `sheetSourceId` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty.
- Added the required column `updatedAt` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty.
- Added the required column `updatedAt` to the `SheetSource` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_columnId_fkey";

-- DropForeignKey
ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_sourceId_fkey";

-- DropForeignKey
ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_sourceIndexId_fkey";

-- AlterTable
ALTER TABLE "SheetColumn" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;

-- AlterTable
ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_pkey",
DROP COLUMN "columnId",
DROP COLUMN "sourceId",
DROP COLUMN "sourceIndexId",
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "indexedSourceId" TEXT,
ADD COLUMN "sheetColumnId" TEXT NOT NULL,
ADD COLUMN "sheetSourceId" TEXT NOT NULL,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL,
ADD CONSTRAINT "SheetColumnValue_pkey" PRIMARY KEY ("sheetColumnId", "sheetSourceId");

-- AlterTable
ALTER TABLE "SheetSource" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;

-- AddForeignKey
ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sheetColumnId_fkey" FOREIGN KEY ("sheetColumnId") REFERENCES "SheetColumn"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sheetSourceId_fkey" FOREIGN KEY ("sheetSourceId") REFERENCES "SheetSource"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_indexedSourceId_fkey" FOREIGN KEY ("indexedSourceId") REFERENCES "IndexedSource"("id") ON DELETE SET NULL ON UPDATE CASCADE;
62 changes: 35 additions & 27 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ model Member {
}

model Sheet {
id String @id @default(uuid())
name String
createdById String
createdBy User @relation(fields: [createdById], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sources SheetSource[]
columns SheetColumn[]
columnValues SheetColumnValue[]
id String @id @default(uuid())
name String
createdById String
createdBy User @relation(fields: [createdById], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sheetSources SheetSource[]
sheetColumns SheetColumn[]
sheetColumnValues SheetColumnValue[]
}

model Source {
Expand Down Expand Up @@ -107,6 +107,8 @@ model SheetSource {
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
rowValues SheetColumnValue[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model SheetColumn {
Expand All @@ -121,32 +123,38 @@ model SheetColumn {
dataType ColumnDataType
defaultValue String @default("N/A")
columnValues SheetColumnValue[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model SheetColumnValue {
sheetId String
sheet Sheet @relation(fields: [sheetId], references: [id])
columnId String
column SheetColumn @relation(fields: [columnId], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
value String?
sourceIndexId String?
sourceId String
source SheetSource @relation(fields: [sourceId], references: [id])
sheetId String
sheet Sheet @relation(fields: [sheetId], references: [id])
sheetColumnId String
sheetColumn SheetColumn @relation(fields: [sheetColumnId], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
value String?
sheetSourceId String
sheetSource SheetSource @relation(fields: [sheetSourceId], references: [id])
indexedSourceId String?
indexedSource IndexedSource? @relation(fields: [indexedSourceId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@id([columnId, sourceId])
@@id([sheetColumnId, sheetSourceId])
}

model IndexedSource {
id String @id @default(uuid())
id String @id @default(uuid())
indexId String
referenceImageFileName String?
referenceText String?
sourceId String
source Source @relation(fields: [sourceId], references: [id])
source Source @relation(fields: [sourceId], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
organization Organization @relation(fields: [organizationId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sheetColumnValues SheetColumnValue[]
}
3 changes: 2 additions & 1 deletion src/app/console/searchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sendMessageToSearch } from "./actions";
import ReactMarkdown from "react-markdown"
import { produce } from "immer"
import Image from "next/image";
import { stripCodeBlockBackTicks } from "@/lib/utils";

export default function SearchDialog({ open }: { open: boolean }) {

Expand Down Expand Up @@ -131,7 +132,7 @@ export default function SearchDialog({ open }: { open: boolean }) {
</DialogHeader>
<ScrollArea className="h-[400px] pr-5">
<div className="mdc">
<ReactMarkdown>{source.referenceText}</ReactMarkdown>
<ReactMarkdown>{stripCodeBlockBackTicks(source.referenceText || "No reference text")}</ReactMarkdown>
</div>
</ScrollArea>
</DialogContent>
Expand Down
68 changes: 33 additions & 35 deletions src/app/console/sheets/[slug]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const fetchSheet = async (id: string) => {
let columnValues: any = {}

for (const value of values) {
columnValues[`${value.sourceId}_${value.columnId}`] = value
columnValues[`${value.sheetSourceId}_${value.sheetColumnId}`] = value
}

return {
Expand Down Expand Up @@ -87,7 +87,7 @@ export const updateSheetName = async (id: string, name: string) => {
return
}

export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) => {
export const updateSourceToSheet = async (selectedSourceIds: string[], sheetId: string) => {
const { organizationId, userId } = await getAuthToken()

const sheet = await prisma.sheet.findFirstOrThrow({
Expand All @@ -109,10 +109,10 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string)
const existingSheetSourceIds = sheetSources.map(source => source.sourceId)

// Find source ids that are present in sourceIds but not in sheetSourceIds
const newSourceIds = sourceIds.filter(sourceId => !existingSheetSourceIds.includes(sourceId))
const newSourceIds = selectedSourceIds.filter(sourceId => !existingSheetSourceIds.includes(sourceId))

// Find source ids that are present in sheetSourceIds but not in sourceIds
const oldSourceIds = existingSheetSourceIds.filter(sourceId => !sourceIds.includes(sourceId))
const oldSourceIds = existingSheetSourceIds.filter(sourceId => !selectedSourceIds.includes(sourceId))


for (const sourceId of newSourceIds) {
Expand All @@ -124,26 +124,26 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string)
}
})

await tx.sheetSource.create({
const sheetSource = await tx.sheetSource.create({
data: {
sourceId: source.id,
sheetId: sheet.id,
organizationId
}
})

const columns = await tx.sheetColumn.findMany({
const sheetColumns = await tx.sheetColumn.findMany({
where: {
sheetId: sheet.id,
organizationId
}
})

for (const column of columns) {
for (const sheetColumn of sheetColumns) {
await tx.sheetColumnValue.create({
data: {
sourceId: source.id,
columnId: column.id,
sheetSourceId: sheetSource.id,
sheetColumnId: sheetColumn.id,
value: "",
sheetId: sheet.id,
organizationId
Expand All @@ -157,30 +157,29 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string)

await prisma.$transaction(async (tx) => {

const source = await tx.source.findFirstOrThrow({
const sheetSource = await tx.sheetSource.findFirstOrThrow({
where: {
id: sourceId,
sourceId: sourceId,
sheetId: sheet.id,
organizationId
}
})


await tx.sheetSource.deleteMany({
await tx.sheetColumnValue.deleteMany({
where: {
sourceId: source.id,
sheetSourceId: sheetSource.id,
sheetId: sheet.id,
organizationId
}
})

await tx.sheetColumnValue.deleteMany({
await tx.sheetSource.deleteMany({
where: {
sourceId: source.id,
sourceId: sourceId,
sheetId: sheet.id,
organizationId
}
})

})
}

Expand Down Expand Up @@ -209,7 +208,7 @@ export async function addColumnToSheet(

await prisma.$transaction(async (tx) => {

const column = await tx.sheetColumn.create({
const sheetColumn = await tx.sheetColumn.create({
data: {
sheetId: sheet.id,
organizationId,
Expand All @@ -230,8 +229,8 @@ export async function addColumnToSheet(
for (const sheetSource of sheetSources) {
await tx.sheetColumnValue.create({
data: {
sourceId: sheetSource.id,
columnId: column.id,
sheetSourceId: sheetSource.id,
sheetColumnId: sheetColumn.id,
sheetId: sheet.id,
organizationId
}
Expand Down Expand Up @@ -281,7 +280,7 @@ export async function updateColumnToSheet(
}


export async function deleteColumnFromSheet(sheetId: string, columnId: string) {
export async function deleteColumnFromSheet(sheetId: string, sheetColumnId: string) {
const { organizationId, userId } = await getAuthToken()
const sheet = await prisma.sheet.findFirstOrThrow({
where: {
Expand All @@ -291,35 +290,34 @@ export async function deleteColumnFromSheet(sheetId: string, columnId: string) {
}
})

const column = await prisma.sheetColumn.findFirstOrThrow({
const sheetColumn = await prisma.sheetColumn.findFirstOrThrow({
where: {
id: columnId,
id: sheetColumnId,
sheetId: sheetId,
organizationId
}
})

await prisma.$transaction(async (tx) => {

await prisma.sheetColumn.delete({
await tx.sheetColumnValue.deleteMany({
where: {
id: column.id,
sheetColumnId: sheetColumn.id,
sheetId: sheet.id,
organizationId
}
})

await tx.sheetColumnValue.deleteMany({
await prisma.sheetColumn.delete({
where: {
columnId: column.id,
id: sheetColumn.id,
sheetId: sheet.id,
organizationId
}
})

})


return
}

Expand Down Expand Up @@ -403,20 +401,20 @@ export async function runColumnSourceTask(sheetId: string, sheetColumnId: string

let data = ""
let refrenceImage = ""
let sourceIndexId = ""
let indexedSourceId = ""

if (sourceIndexes.length > 1) {
const foundIndex = await queryVectorDB(column.instruction, organizationId, sheetSource.sourceId)
const sourceIndex = sourceIndexes.find(index => index.indexId === foundIndex)
if (sourceIndex) {
data = sourceIndex.referenceText || ""
refrenceImage = sourceIndex.referenceImageFileName || ""
sourceIndexId = sourceIndex.indexId || ""
indexedSourceId = sourceIndex.id || ""
}
} else {
data = sourceIndexes[0].referenceText || ""
refrenceImage = sourceIndexes[0].referenceImageFileName || ""
sourceIndexId = sourceIndexes[0].indexId || ""
indexedSourceId = sourceIndexes[0].id || ""
}

let query = `
Expand All @@ -438,20 +436,20 @@ export async function runColumnSourceTask(sheetId: string, sheetColumnId: string

await prisma.sheetColumnValue.updateMany({
where: {
columnId: sheetColumnId,
sourceId: sheetSourceId,
sheetColumnId: sheetColumnId,
sheetSourceId: sheetSourceId,
sheetId: sheet.id,
organizationId
},
data: {
value: answer,
sourceIndexId: sourceIndexId
indexedSourceId: indexedSourceId
}
})

return {
answer,
sourceIndexId
indexedSourceId: indexedSourceId
}

}
Expand Down
Loading

0 comments on commit 1454aa7

Please sign in to comment.