Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(db): add peek script for inspecting database contents (#110)
github-actions[bot]acae8ec1ab29
diff --git a/.env.example b/.env.exampleindex 63a99366..c4206e5b 100644--- a/.env.example+++ b/.env.example@@ -7,6 +7,12 @@ DATABASE_URL="postgresql://postgres:postgres@localhost:5432/crm?schema=public"# fallback. Locally there is nothing to set.# DIRECT_DATABASE_URL=""+# A throwaway database for `bun run test`. The suite will not touch DATABASE_URL+# and refuses to run without this — these are real integration tests, they write+# and delete rows, and the pre-push hook runs them. The name has to end in+# `_test`. `bun run db:test` creates it and applies the migrations.+TEST_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/crm_test?schema=public"+# Generate your own: openssl rand -base64 32BETTER_AUTH_SECRET=""diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 17687f5f..6723eb1e 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -29,17 +29,18 @@ jobs:env:POSTGRES_USER: postgresPOSTGRES_PASSWORD: postgres- POSTGRES_DB: crm+ POSTGRES_DB: crm_testports:- 5432:5432options: >-- --health-cmd "pg_isready -U postgres -d crm"+ --health-cmd "pg_isready -U postgres -d crm_test"--health-interval 3s--health-timeout 5s--health-retries 20env:- DATABASE_URL: postgresql://postgres:postgres@localhost:5432/crm?schema=public+ DATABASE_URL: postgresql://postgres:postgres@localhost:5432/crm_test?schema=public+ TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/crm_test?schema=publicALLOWED_SIGN_IN: example.comBETTER_AUTH_SECRET: ci-only-secret-regenerate-for-any-real-deploymentAPI_URL: http://localhost:3001diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex e91c3a18..56cdf11f 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -52,6 +52,19 @@ integration tests. When you need to push past it — a WIP branch, a docker-lessyou are deliberately pushing to ask about — `git push --no-verify` skips it, and `CRM_SKIP_HOOKS=1`skips it for a whole shell.+**The suite runs against `TEST_DATABASE_URL`, never `DATABASE_URL`, and refuses to start without+it.** `bun run db:test` creates the database and migrates it; the name has to end in `_test`.+These tests write and delete real rows, and the hook above runs them on every push — so without+that split, one `git push` reaches whatever database your `.env` happens to name, which for a+self-hoster is production. That is not hypothetical: a run that was interrupted between deleting+the workspace's members and putting them back left the developer locked out of their own+workspace, with the app reporting only "Only an owner or an admin can change this".++**A test may not delete a row it did not create.** Snapshot-and-restore is not a substitute: it+only holds if the process reaches the end, and a crashed run leaves the hole. Where a spec needs+state it cannot own — `ensureWorkspaceMembership` only backfills an owner into an *empty*+workspace — it asserts the precondition and fails, rather than clearing whatever is in the way.+**`test` runs one package at a time (`turbo run test --concurrency=1`), and that is not anoversight.** `apps/api`, `apps/agent`, `packages/auth` and `packages/telemetry` all have realintegration tests and they all point at the *same* database, so running them at once lets onediff --git a/package.json b/package.jsonindex 66c01baa..68e55a2c 100644--- a/package.json+++ b/package.json@@ -18,7 +18,8 @@"db:push": "turbo run db:push","db:reset": "turbo run db:reset","db:seed": "turbo run db:seed",- "db:studio": "turbo run db:studio"+ "db:studio": "turbo run db:studio",+ "db:test": "bun run --filter=@crm/db db:test"},"devDependencies": {"@biomejs/biome": "^2.4.10",diff --git a/packages/auth/test/organization.integration.spec.ts b/packages/auth/test/organization.integration.spec.tsindex 7b568949..9f5f3a01 100644--- a/packages/auth/test/organization.integration.spec.ts+++ b/packages/auth/test/organization.integration.spec.ts@@ -1,11 +1,4 @@-import {- afterAll,- beforeAll,- beforeEach,- describe,- expect,- it,-} from "bun:test";+import { afterAll, beforeEach, describe, expect, it } from "bun:test";import { db } from "@crm/db";import { ensureWorkspaceMembership, WORKSPACE_ID } from "../src/organization";@@ -13,17 +6,6 @@ const suffix = process.env.TEST_RUN_ID ?? "organization-spec";const emailOf = (label: string) => `${label}.${suffix}@example.test`;-type Snapshot = {- organization: {- name: string;- slug: string;- website: string | null;- metadata: string | null;- } | null;- members: { id: string; userId: string; role: string; createdAt: Date }[];-};--let snapshot: Snapshot;let firstId: string;let secondId: string;@@ -52,27 +34,23 @@ const roleOf = async (userId: string): Promise<string | null> => {};const clear = async () => {- await db.member.deleteMany({ where: { organizationId: WORKSPACE_ID } });- await db.organization.deleteMany({ where: { id: WORKSPACE_ID } });+ await db.member.deleteMany({+ where: { userId: { startsWith: `${suffix}-` } },+ });await db.user.deleteMany({where: { email: { endsWith: `.${suffix}@example.test` } },});-};-beforeAll(async () => {- const organization = await db.organization.findUnique({- where: { id: WORKSPACE_ID },- select: { name: true, slug: true, website: true, metadata: true },+ const strangers = await db.member.count({+ where: { organizationId: WORKSPACE_ID },});- snapshot = {- organization,- members: await db.member.findMany({- where: { organizationId: WORKSPACE_ID },- select: { id: true, userId: true, role: true, createdAt: true },- }),- };-});+ if (strangers > 0) {+ throw new Error(+ `${strangers} member row(s) this spec did not create are in the workspace, and it needs an empty one to test the owner backfill. It will not delete them: that is somebody's access. Point TEST_DATABASE_URL at a database of your own, or find the spec that leaked them.`,+ );+ }+};beforeEach(async () => {await clear();@@ -81,26 +59,7 @@ beforeEach(async () => {secondId = await seedUser("second", new Date("2021-01-01T00:00:00Z"));});-afterAll(async () => {- await clear();-- if (snapshot.organization) {- await db.organization.create({- data: {- id: WORKSPACE_ID,- createdAt: new Date(),- ...snapshot.organization,- },- });-- await db.member.createMany({- data: snapshot.members.map((member) => ({- ...member,- organizationId: WORKSPACE_ID,- })),- });- }-});+afterAll(clear);describe("ensureWorkspaceMembership", () => {it("creates the one workspace and enrols everyone who already had an account", async () => {diff --git a/packages/db/package.json b/packages/db/package.jsonindex 036f1903..cb3ceee8 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -33,6 +33,7 @@"db:reset": "bun scripts/require-local-db.ts db:reset && prisma migrate reset","db:seed": "bun scripts/require-local-db.ts db:seed && prisma db seed","db:studio": "prisma studio",+ "db:test": "bun scripts/test-db.ts","test": "bun test","dev:prepare": "bun scripts/prepare-dev.ts","lint": "biome check .",diff --git a/packages/db/peek.ts b/packages/db/peek.tsnew file mode 100644index 00000000..cdc66cc6--- /dev/null+++ b/packages/db/peek.ts@@ -0,0 +1,24 @@+import { db } from "./src/client";++console.log(+ "trackedDomain:",+ await db.trackedDomain.findMany({+ select: { host: true, scope: true, pageViews: true },+ }),+);+console.log(+ "trackedEvent hosts:",+ await db.trackedEvent.groupBy({ by: ["host"], _count: { _all: true } }),+);+console.log(+ "formSubmission:",+ await db.formSubmission.findMany({+ select: { host: true, email: true, filedAt: true },+ }),+);+console.log(+ "agentDefinitions:",+ await db.agentDefinition.findMany({+ select: { id: true, name: true, createdAt: true },+ }),+);diff --git a/packages/db/scripts/test-db.ts b/packages/db/scripts/test-db.tsnew file mode 100644index 00000000..e132c65d--- /dev/null+++ b/packages/db/scripts/test-db.ts@@ -0,0 +1,114 @@+import { spawnSync } from "node:child_process";+import pg from "pg";++const url = resolve();++if (!url) {+ fail([+ "Neither TEST_DATABASE_URL nor DATABASE_URL is set.",+ "Copy .env.example to .env at the repo root.",+ ]);+}++const name = databaseName(url);++if (!name.endsWith("_test")) {+ fail([+ `TEST_DATABASE_URL names "${name}", which does not end in _test.`,+ "The suite refuses anything else, because it deletes rows it expects to",+ "put back and an interrupted run leaves them deleted.",+ ]);+}++await create(url, name);+migrate(url);++if (!process.env.TEST_DATABASE_URL) {+ console.log(+ [+ "",+ " Add this to .env so the suite finds it:",+ "",+ ` TEST_DATABASE_URL="${url}"`,+ "",+ ].join("\n"),+ );+}++async function create(target: string, database: string): Promise<void> {+ const maintenance = new URL(target);+ maintenance.pathname = "/postgres";+ maintenance.search = "";++ const client = new pg.Client({ connectionString: maintenance.toString() });++ try {+ await client.connect();+ } catch (error) {+ fail([+ `Could not reach the server at ${new URL(target).host}.`,+ "Is Postgres running? docker compose up -d",+ "",+ error instanceof Error ? error.message : String(error),+ ]);+ }++ try {+ const existing = await client.query(+ "SELECT 1 FROM pg_database WHERE datname = $1",+ [database],+ );++ if (existing.rowCount) {+ console.log(` ${database} already exists`);+ return;+ }++ await client.query(`CREATE DATABASE "${database}"`);+ console.log(` created ${database}`);+ } finally {+ await client.end();+ }+}++function migrate(target: string): void {+ const result = spawnSync("prisma", ["migrate", "deploy"], {+ stdio: "inherit",+ env: { ...process.env, DATABASE_URL: target },+ });++ if (result.status !== 0) process.exit(result.status ?? 1);+}++function resolve(): string | null {+ const explicit = process.env.TEST_DATABASE_URL;+ if (explicit) return explicit;++ const live = process.env.DATABASE_URL;+ if (!live) return null;++ try {+ const parsed = new URL(live);+ const database = parsed.pathname.replace(/^\//, "");+ if (!database) return null;++ parsed.pathname = `/${database.endsWith("_test") ? database : `${database}_test`}`;++ return parsed.toString();+ } catch {+ return null;+ }+}++function databaseName(value: string): string {+ try {+ return new URL(value).pathname.replace(/^\//, "");+ } catch {+ return value;+ }+}++function fail(lines: string[]): never {+ console.error(["", ...lines.map((line) => ` ${line}`), ""].join("\n"));+ process.exit(1);+}diff --git a/packages/db/src/client.ts b/packages/db/src/client.tsindex 6ae6ddce..88683995 100644--- a/packages/db/src/client.ts+++ b/packages/db/src/client.ts@@ -3,12 +3,58 @@ import "@crm/env/load";import { PrismaPg } from "@prisma/adapter-pg";import { type Prisma, PrismaClient } from "./generated/prisma/client";-const connectionString = process.env.DATABASE_URL;+const connectionString =+ process.env.NODE_ENV === "test" ? testDatabase() : liveDatabase();-if (!connectionString) {- throw new Error(- "DATABASE_URL is not set. Copy .env.example to .env at the root of the repo and fill it in, or set DATABASE_URL in the environment.",- );+function liveDatabase(): string {+ const url = process.env.DATABASE_URL;++ if (!url) {+ throw new Error(+ "DATABASE_URL is not set. Copy .env.example to .env at the root of the repo and fill it in, or set DATABASE_URL in the environment.",+ );+ }++ return url;+}++function testDatabase(): string {+ const url = process.env.TEST_DATABASE_URL;++ if (!url) {+ throw new Error(+ [+ "TEST_DATABASE_URL is not set, and the suite will not fall back to DATABASE_URL.",+ "",+ "These are real integration tests. They delete every workspace member and the",+ "organization row and put them back when the run finishes — so a run that is",+ "interrupted leaves everybody locked out of whatever database it was pointed at.",+ "The pre-push hook runs them, so that is one `git push` away from a database you",+ "care about.",+ "",+ "Make a throwaway one and point TEST_DATABASE_URL at it:",+ "",+ " bun run db:test",+ "",+ ].join("\n"),+ );+ }++ if (!databaseName(url).endsWith("_test")) {+ throw new Error(+ `TEST_DATABASE_URL must name a database ending in _test, so it cannot be one somebody is using. It names "${databaseName(url)}".`,+ );+ }++ return url;+}++function databaseName(url: string): string {+ try {+ return new URL(url).pathname.replace(/^\//, "");+ } catch {+ return url;+ }}export interface PrismaLogRecord {