From 102fa63d58a1ffd24ef82a3d23ca90ede336a499 Mon Sep 17 00:00:00 2001 From: bkellam Date: Mon, 18 Aug 2025 08:07:29 -0400 Subject: [PATCH] add seed script --- packages/db/tools/scriptRunner.ts | 2 + packages/db/tools/scripts/seed.ts | 72 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 packages/db/tools/scripts/seed.ts diff --git a/packages/db/tools/scriptRunner.ts b/packages/db/tools/scriptRunner.ts index 8e72063a0..3f883a296 100644 --- a/packages/db/tools/scriptRunner.ts +++ b/packages/db/tools/scriptRunner.ts @@ -4,6 +4,7 @@ import { migrateDuplicateConnections } from "./scripts/migrate-duplicate-connect import { injectAuditData } from "./scripts/inject-audit-data"; import { confirmAction } from "./utils"; import { createLogger } from "@sourcebot/logger"; +import { seed } from "./scripts/seed"; export interface Script { run: (prisma: PrismaClient) => Promise; @@ -12,6 +13,7 @@ export interface Script { export const scripts: Record = { "migrate-duplicate-connections": migrateDuplicateConnections, "inject-audit-data": injectAuditData, + "seed": seed, } const parser = new ArgumentParser(); diff --git a/packages/db/tools/scripts/seed.ts b/packages/db/tools/scripts/seed.ts new file mode 100644 index 000000000..3d9473ee1 --- /dev/null +++ b/packages/db/tools/scripts/seed.ts @@ -0,0 +1,72 @@ +import { ConnectionSyncStatus, Prisma, PrismaClient, RepoIndexingStatus } from "@prisma/client"; +import { v4 as uuidv4 } from 'uuid'; +import { Script } from "../scriptRunner"; + +const TEST_ORG_ID = 1; +const TEST_CONNECTION_ID = 1; + +export const seed: Script = { + run: async (prisma: PrismaClient) => { + + const org = await prisma.org.findUnique({ + where: { + id: TEST_ORG_ID, + } + }); + + if (!org) { + await prisma.org.create({ + data: { + id: TEST_ORG_ID, + name: 'Test Org', + domain: 'test-org.com', + } + }); + } + + const connection = await prisma.connection.findUnique({ + where: { + id: TEST_CONNECTION_ID, + } + }); + + if (!connection) { + await prisma.connection.create({ + data: { + id: TEST_CONNECTION_ID, + name: 'Test Connection', + orgId: TEST_ORG_ID, + syncedAt: new Date(), + syncStatus: ConnectionSyncStatus.SYNCED, + config: {} as unknown as Prisma.InputJsonValue, + connectionType: 'github', + } + }); + } + + for (let i = 0; i < 20000; i++) { + const name = uuidv4(); + await prisma.repo.create({ + data: { + name: name, + orgId: TEST_ORG_ID, + external_id: name, + external_codeHostType: 'github', + external_codeHostUrl: 'https://github.com', + repoIndexingStatus: RepoIndexingStatus.INDEXED, + isFork: false, + isArchived: false, + indexedAt: new Date(), + metadata: {}, + cloneUrl: 'https://github.com/sourcebot-dev/sourcebot-does-not-exist.git', + connections: { + create: { + addedAt: new Date(), + connectionId: TEST_CONNECTION_ID, + } + } + } + }); + } + } +};