From 6d93bf1bbd437fbdf0f9664b732cad5af6625aa6 Mon Sep 17 00:00:00 2001 From: Gerard Date: Fri, 19 Jul 2024 15:38:40 +0200 Subject: [PATCH] feature: remove document snapshots (#46) The query to materialize documents at a particular commit is at most two selects with low cardinality which is honestly pan comido for the database there is no point in having the snapshots table specially considering we are not a proxy anymore. --- .../commits/[commitUuid]/documents/route.ts | 4 +- apps/web/src/components/Sidebar/index.tsx | 4 +- .../drizzle/0006_strong_norman_osborn.sql | 1 + packages/core/drizzle/meta/0005_snapshot.json | 2 +- packages/core/drizzle/meta/0006_snapshot.json | 654 ++++++++++++++++++ packages/core/drizzle/meta/_journal.json | 7 + packages/core/src/data-access/commits.ts | 10 +- .../core/src/data-access/documentSnapshots.ts | 18 - packages/core/src/data-access/index.ts | 1 - packages/core/src/schema/index.ts | 1 - packages/core/src/schema/models/commits.ts | 5 +- .../src/schema/models/documentSnapshots.ts | 53 -- .../src/services/documentVersions/index.ts | 1 - .../documentVersions/materializeAtCommit.ts | 20 - 14 files changed, 677 insertions(+), 104 deletions(-) create mode 100644 packages/core/drizzle/0006_strong_norman_osborn.sql create mode 100644 packages/core/drizzle/meta/0006_snapshot.json delete mode 100644 packages/core/src/data-access/documentSnapshots.ts delete mode 100644 packages/core/src/schema/models/documentSnapshots.ts delete mode 100644 packages/core/src/services/documentVersions/materializeAtCommit.ts diff --git a/apps/web/src/app/api/projects/[projectId]/commits/[commitUuid]/documents/route.ts b/apps/web/src/app/api/projects/[projectId]/commits/[commitUuid]/documents/route.ts index 88da2a0fa..4d195ad15 100644 --- a/apps/web/src/app/api/projects/[projectId]/commits/[commitUuid]/documents/route.ts +++ b/apps/web/src/app/api/projects/[projectId]/commits/[commitUuid]/documents/route.ts @@ -1,4 +1,4 @@ -import { materializeDocumentsAtCommit } from '@latitude-data/core' +import { getDocumentsAtCommit } from '@latitude-data/core' import { NextRequest, NextResponse } from 'next/server' export async function GET( @@ -8,7 +8,7 @@ export async function GET( }: { params: { commitUuid: string; projectId: number } }, ) { try { - const documents = await materializeDocumentsAtCommit({ + const documents = await getDocumentsAtCommit({ commitUuid, projectId: Number(projectId), }) diff --git a/apps/web/src/components/Sidebar/index.tsx b/apps/web/src/components/Sidebar/index.tsx index 82a93b76a..ff92b79b3 100644 --- a/apps/web/src/components/Sidebar/index.tsx +++ b/apps/web/src/components/Sidebar/index.tsx @@ -1,4 +1,4 @@ -import { materializeDocumentsAtCommit } from '@latitude-data/core' +import { getDocumentsAtCommit } from '@latitude-data/core' import DocumentTree, { CreateNode } from './DocumentTree' @@ -9,7 +9,7 @@ export default async function Sidebar({ commitUuid: string projectId: number }) { - const documentsResult = await materializeDocumentsAtCommit({ + const documentsResult = await getDocumentsAtCommit({ projectId, commitUuid, }) diff --git a/packages/core/drizzle/0006_strong_norman_osborn.sql b/packages/core/drizzle/0006_strong_norman_osborn.sql new file mode 100644 index 000000000..b7d9ee8b9 --- /dev/null +++ b/packages/core/drizzle/0006_strong_norman_osborn.sql @@ -0,0 +1 @@ +DROP TABLE "latitude"."document_snapshots"; \ No newline at end of file diff --git a/packages/core/drizzle/meta/0005_snapshot.json b/packages/core/drizzle/meta/0005_snapshot.json index 51e0338c0..a7a58706d 100644 --- a/packages/core/drizzle/meta/0005_snapshot.json +++ b/packages/core/drizzle/meta/0005_snapshot.json @@ -753,4 +753,4 @@ "schemas": {}, "tables": {} } -} \ No newline at end of file +} diff --git a/packages/core/drizzle/meta/0006_snapshot.json b/packages/core/drizzle/meta/0006_snapshot.json new file mode 100644 index 000000000..ffd57fc17 --- /dev/null +++ b/packages/core/drizzle/meta/0006_snapshot.json @@ -0,0 +1,654 @@ +{ + "id": "147b5696-593a-4fbb-a871-4e9038ee7635", + "prevId": "95e08da6-8793-4095-b035-f2873c82f634", + "version": "7", + "dialect": "postgresql", + "tables": { + "latitude.users": { + "name": "users", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "encrypted_password": { + "name": "encrypted_password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + } + }, + "latitude.sessions": { + "name": "sessions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "sessions_user_id_users_id_fk": { + "name": "sessions_user_id_users_id_fk", + "tableFrom": "sessions", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.workspaces": { + "name": "workspaces", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "creator_id": { + "name": "creator_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "workspaces_creator_id_users_id_fk": { + "name": "workspaces_creator_id_users_id_fk", + "tableFrom": "workspaces", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "creator_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.memberships": { + "name": "memberships", + "schema": "latitude", + "columns": { + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "memberships_workspace_id_workspaces_id_fk": { + "name": "memberships_workspace_id_workspaces_id_fk", + "tableFrom": "memberships", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "memberships_user_id_users_id_fk": { + "name": "memberships_user_id_users_id_fk", + "tableFrom": "memberships", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "memberships_workspace_id_user_id_pk": { + "name": "memberships_workspace_id_user_id_pk", + "columns": [ + "workspace_id", + "user_id" + ] + } + }, + "uniqueConstraints": {} + }, + "latitude.api_keys": { + "name": "api_keys", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "workspace_id_idx": { + "name": "workspace_id_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "api_keys_workspace_id_workspaces_id_fk": { + "name": "api_keys_workspace_id_workspaces_id_fk", + "tableFrom": "api_keys", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "api_keys_uuid_unique": { + "name": "api_keys_uuid_unique", + "nullsNotDistinct": false, + "columns": [ + "uuid" + ] + } + } + }, + "latitude.projects": { + "name": "projects", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "workspace_idx": { + "name": "workspace_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "projects_workspace_id_workspaces_id_fk": { + "name": "projects_workspace_id_workspaces_id_fk", + "tableFrom": "projects", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.commits": { + "name": "commits", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "title": { + "name": "title", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "project_id": { + "name": "project_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "merged_at": { + "name": "merged_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "project_commit_order_idx": { + "name": "project_commit_order_idx", + "columns": [ + { + "expression": "merged_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "commits_project_id_projects_id_fk": { + "name": "commits_project_id_projects_id_fk", + "tableFrom": "commits", + "tableTo": "projects", + "schemaTo": "latitude", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "commits_uuid_unique": { + "name": "commits_uuid_unique", + "nullsNotDistinct": false, + "columns": [ + "uuid" + ] + } + } + }, + "latitude.document_versions": { + "name": "document_versions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "document_uuid": { + "name": "document_uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "path": { + "name": "path", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "hash": { + "name": "hash", + "type": "varchar", + "primaryKey": false, + "notNull": false + }, + "commit_id": { + "name": "commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "document_uuid_commit_id_idx": { + "name": "document_uuid_commit_id_idx", + "columns": [ + { + "expression": "document_uuid", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "commit_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "document_versions_commit_id_commits_id_fk": { + "name": "document_versions_commit_id_commits_id_fk", + "tableFrom": "document_versions", + "tableTo": "commits", + "schemaTo": "latitude", + "columnsFrom": [ + "commit_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "unique_document_uuid_commit_id": { + "name": "unique_document_uuid_commit_id", + "nullsNotDistinct": false, + "columns": [ + "document_uuid", + "commit_id" + ] + }, + "unique_path_commit_id": { + "name": "unique_path_commit_id", + "nullsNotDistinct": false, + "columns": [ + "path", + "commit_id" + ] + } + } + } + }, + "enums": {}, + "schemas": { + "latitude": "latitude" + }, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/packages/core/drizzle/meta/_journal.json b/packages/core/drizzle/meta/_journal.json index a73ffe62b..4b137eeb6 100644 --- a/packages/core/drizzle/meta/_journal.json +++ b/packages/core/drizzle/meta/_journal.json @@ -43,6 +43,13 @@ "when": 1721394470350, "tag": "0005_right_magik", "breakpoints": true + }, + { + "idx": 6, + "version": "7", + "when": 1721396158426, + "tag": "0006_strong_norman_osborn", + "breakpoints": true } ] } \ No newline at end of file diff --git a/packages/core/src/data-access/commits.ts b/packages/core/src/data-access/commits.ts index 9cc60af27..613e5a562 100644 --- a/packages/core/src/data-access/commits.ts +++ b/packages/core/src/data-access/commits.ts @@ -26,13 +26,19 @@ export async function findHeadCommit( export type FindCommitProps = { uuid: string - projectId: number + projectId?: number } export async function findCommit( { projectId, uuid }: FindCommitProps, tx = database, ): Promise> { - if (uuid === HEAD_COMMIT) return findHeadCommit({ projectId }, tx) + if (uuid === HEAD_COMMIT) { + if (!projectId) { + return Result.error(new NotFoundError('Project ID is required')) + } + + return findHeadCommit({ projectId }, tx) + } const commit = await tx.query.commits.findFirst({ where: eq(commits.uuid, uuid), diff --git a/packages/core/src/data-access/documentSnapshots.ts b/packages/core/src/data-access/documentSnapshots.ts deleted file mode 100644 index c5699962c..000000000 --- a/packages/core/src/data-access/documentSnapshots.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { - database, - documentSnapshots, - documentVersions, -} from '@latitude-data/core' -import { eq } from 'drizzle-orm' - -export async function listdocumentSnapshots() { - const documents = await database - .select() - .from(documentSnapshots) - .innerJoin( - documentVersions, - eq(documentSnapshots.documentVersionId, documentVersions.id), - ) - - return documents -} diff --git a/packages/core/src/data-access/index.ts b/packages/core/src/data-access/index.ts index f381efc40..49ba75f59 100644 --- a/packages/core/src/data-access/index.ts +++ b/packages/core/src/data-access/index.ts @@ -3,7 +3,6 @@ import { Database } from '@latitude-data/core' export * from './users' export * from './projects' export * from './commits' -export * from './documentSnapshots' export * from './documentVersions' export type AppContext = { db: Database } diff --git a/packages/core/src/schema/index.ts b/packages/core/src/schema/index.ts index 0b33a0bb1..84189c8aa 100644 --- a/packages/core/src/schema/index.ts +++ b/packages/core/src/schema/index.ts @@ -10,5 +10,4 @@ export * from './models/apiKeys' // Document tables export * from './models/projects' export * from './models/commits' -export * from './models/documentSnapshots' export * from './models/documentVersions' diff --git a/packages/core/src/schema/models/commits.ts b/packages/core/src/schema/models/commits.ts index 3d8bf1dcb..1c82361d0 100644 --- a/packages/core/src/schema/models/commits.ts +++ b/packages/core/src/schema/models/commits.ts @@ -9,7 +9,7 @@ import { varchar, } from 'drizzle-orm/pg-core' -import { documentSnapshots, latitudeSchema, projects } from '..' +import { latitudeSchema, projects } from '..' import { timestamps } from '../schemaHelpers' export const commits = latitudeSchema.table( @@ -36,8 +36,7 @@ export const commits = latitudeSchema.table( }), ) -export const commitRelations = relations(commits, ({ one, many }) => ({ - snapshots: many(documentSnapshots, { relationName: 'snapshots' }), +export const commitRelations = relations(commits, ({ one }) => ({ project: one(projects, { fields: [commits.projectId], references: [projects.id], diff --git a/packages/core/src/schema/models/documentSnapshots.ts b/packages/core/src/schema/models/documentSnapshots.ts deleted file mode 100644 index b9765a0ad..000000000 --- a/packages/core/src/schema/models/documentSnapshots.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { InferSelectModel, relations } from 'drizzle-orm' -import { AnyPgColumn, bigint, bigserial, index } from 'drizzle-orm/pg-core' - -import { - Commit, - commits, - DocumentVersion, - documentVersions, - latitudeSchema, -} from '..' -import { timestamps } from '../schemaHelpers' - -export const documentSnapshots = latitudeSchema.table( - 'document_snapshots', - { - id: bigserial('id', { mode: 'number' }).notNull().primaryKey(), - commitId: bigint('commit_id', { mode: 'number' }) - .references((): AnyPgColumn => commits.id, { onDelete: 'restrict' }) - .notNull(), - documentVersionId: bigint('document_version_id', { mode: 'number' }) - .references((): AnyPgColumn => documentVersions.id, { - onDelete: 'restrict', - }) - .notNull(), - ...timestamps(), - }, - (doc) => ({ - commitIdx: index('prompt_commit_idx').on(doc.commitId), - documentVersionIdx: index('document_snapshot_document_version_idx').on( - doc.documentVersionId, - ), - }), -) - -export const documentSnapshotRelations = relations( - documentSnapshots, - ({ one }) => ({ - commit: one(commits, { - relationName: 'snapshots', - fields: [documentSnapshots.commitId], - references: [commits.id], - }), - version: one(documentVersions, { - fields: [documentSnapshots.documentVersionId], - references: [documentVersions.id], - }), - }), -) - -export type DocumentSnapshot = InferSelectModel & { - commit: Commit - version: DocumentVersion -} diff --git a/packages/core/src/services/documentVersions/index.ts b/packages/core/src/services/documentVersions/index.ts index f51d7fc92..f756a8bb3 100644 --- a/packages/core/src/services/documentVersions/index.ts +++ b/packages/core/src/services/documentVersions/index.ts @@ -1,2 +1 @@ export * from './create' -export * from './materializeAtCommit' diff --git a/packages/core/src/services/documentVersions/materializeAtCommit.ts b/packages/core/src/services/documentVersions/materializeAtCommit.ts deleted file mode 100644 index e0c9f91bc..000000000 --- a/packages/core/src/services/documentVersions/materializeAtCommit.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { HEAD_COMMIT } from '$core/constants' -import { getDocumentsAtCommit, listdocumentSnapshots } from '$core/data-access' -import { Result } from '$core/lib' - -export async function materializeDocumentsAtCommit({ - commitUuid = HEAD_COMMIT, - projectId, -}: { - commitUuid: string - projectId: number -}) { - if (commitUuid === HEAD_COMMIT) { - const snapshots = (await listdocumentSnapshots()).map( - (snap) => snap.document_versions, - ) - return Result.ok(snapshots) - } else { - return await getDocumentsAtCommit({ commitUuid, projectId }) - } -}