diff --git a/.changeset/prisma-schema-drift-check-1691.md b/.changeset/prisma-schema-drift-check-1691.md new file mode 100644 index 00000000..bf121d27 --- /dev/null +++ b/.changeset/prisma-schema-drift-check-1691.md @@ -0,0 +1,5 @@ +--- +"secureflow": minor +--- + +ci(db): fail build on schema-migration drift, add `npm run db:check-drift` script, and document in CONTRIBUTING.md (#1691) diff --git a/.env.example b/.env.example index 2cd2fcf5..b4c4b512 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ DATABASE_URL="postgresql://neondb_owner:@ep-lingering-thunder-ate1tkym.c-9.us-east-1.aws.neon.tech/neondb?sslmode=verify-full&channel_binding=require" DATABASE_POOL_URL="postgresql://neondb_owner:@ep-lingering-thunder-ate1tkym-pooler.c-9.us-east-1.aws.neon.tech/neondb?sslmode=verify-full&channel_binding=require" +# Optional: Shadow database for local migration drift checks (npm run db:check-drift) +# SHADOW_DATABASE_URL="postgresql://user:password@localhost:5432/secureflow_shadow" DB_POOL_MAX=10 GROQ_API_KEY=gsk_your_actual_api_key_here GITHUB_APP_ID="your_app_id" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be37a444..016bcf84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -235,10 +235,7 @@ jobs: PGPASSWORD=ci psql -h localhost -U ci -d secureflow \ -c 'CREATE DATABASE secureflow_shadow' set +e - npx prisma migrate diff \ - --from-migrations prisma/migrations \ - --to-schema prisma/schema.prisma \ - --exit-code + npm run db:check-drift code=$? if [ "$code" -eq 2 ]; then echo "::error::Schema drift detected. Run 'npx prisma migrate dev --name ' and commit the new migration." diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b24b3822..d7aa4d6d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -202,8 +202,13 @@ npm run db:migrate # Seed default data npm run db:seed + +# Check for schema drift (ensures schema.prisma and migrations are in sync) +npm run db:check-drift ``` +> 💡 **Tip**: Running `npm run db:check-drift` locally requires `SHADOW_DATABASE_URL` configured in your environment to diff against a shadow database (e.g. `postgresql://user:password@localhost:5432/secureflow_shadow`). In CI, this is provided automatically. + ### 5. Run Development Server ```bash @@ -234,6 +239,7 @@ Open [http://localhost:9002](http://localhost:9002) with your browser to see the - [ ] Run `npm run lint` and fix any issues - [ ] Run `npm run typecheck` and ensure no TypeScript errors - [ ] Run `npm run format:check` to verify code adheres to Prettier formatting +- [ ] Run `npm run db:check-drift` to verify Prisma schema and migrations are in sync - [ ] Run `npm test` and ensure all test suites pass - [ ] Run `npm run build` and confirm it builds - [ ] Test your changes thoroughly diff --git a/package.json b/package.json index f8e6a386..76bfc420 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "db:migrate": "npx prisma migrate dev", "db:push": "npx prisma db push", "db:seed": "npx prisma db seed", + "db:check-drift": "prisma migrate diff --from-migrations prisma/migrations --to-schema prisma/schema.prisma --exit-code", "postinstall": "prisma generate", "worker": "tsx --env-file=.env scripts/start-worker.ts", "worker:render": "tsx scripts/start-worker.ts", diff --git a/prisma/README.md b/prisma/README.md index 5b53a057..572ffcee 100644 --- a/prisma/README.md +++ b/prisma/README.md @@ -215,6 +215,11 @@ Opens at `http://localhost:5555`. If your local database schema gets out of sync or encounters drift: +- **Check for schema vs. migration drift**: + ```bash + npm run db:check-drift + ``` + _Diffs migrations against `schema.prisma` using `prisma migrate diff --exit-code`. Exits with code 0 when in sync or code 2 when drift is detected. Requires `SHADOW_DATABASE_URL`._ - **Check migration status**: ```bash npx prisma migrate status diff --git a/scripts/armoriq-verify.ts b/scripts/armoriq-verify.ts index 58551c36..906ee347 100644 --- a/scripts/armoriq-verify.ts +++ b/scripts/armoriq-verify.ts @@ -270,8 +270,12 @@ export async function runArmorIQCheck(options: CliCheckOptions = {}) { const isMain = process.argv[1] && /armoriq-verify(\.ts|\.js)?$/.test(process.argv[1]); if (isMain) { const options = parseCliArgs(process.argv.slice(2)); - runArmorIQCheck(options).catch((err) => { - console.error("Fatal error during ArmorIQ verification:", err); - process.exit(1); - }); + runArmorIQCheck(options) + .then(() => { + process.exit(0); + }) + .catch((err) => { + console.error("Fatal error during ArmorIQ verification:", err); + process.exit(1); + }); } diff --git a/src/ai/flows/generate-remediation-patch.backoff.test.ts b/src/ai/flows/generate-remediation-patch.backoff.test.ts index 688da84a..76930a26 100644 --- a/src/ai/flows/generate-remediation-patch.backoff.test.ts +++ b/src/ai/flows/generate-remediation-patch.backoff.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); @@ -31,12 +31,17 @@ const rateLimit = () => Object.assign(new Error("429 Too Many Requests"), { stat describe("generateRemediationPatch - backoff and retry (#1513)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); if (typeof resetModelCircuitBreakers === "function") resetModelCircuitBreakers(); vi.spyOn(console, "warn").mockImplementation(() => {}); vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("retries a rate limit and returns the patch once the provider recovers", async () => { mockGenerate .mockRejectedValueOnce(rateLimit()) diff --git a/src/ai/flows/generate-remediation-patch.vram.test.ts b/src/ai/flows/generate-remediation-patch.vram.test.ts index aa6ce7d3..8b6bdaf0 100644 --- a/src/ai/flows/generate-remediation-patch.vram.test.ts +++ b/src/ai/flows/generate-remediation-patch.vram.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); @@ -33,12 +33,17 @@ const goodOutput = { describe("generateRemediationPatch - insufficient VRAM (#1525)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); if (typeof resetModelCircuitBreakers === "function") resetModelCircuitBreakers(); vi.spyOn(console, "warn").mockImplementation(() => {}); vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("does not crash when the local model does not fit in memory", async () => { mockGenerate.mockRejectedValue(OOM); diff --git a/src/ai/flows/web3-security-explanations.fallback.test.ts b/src/ai/flows/web3-security-explanations.fallback.test.ts index 46aede50..f44512b1 100644 --- a/src/ai/flows/web3-security-explanations.fallback.test.ts +++ b/src/ai/flows/web3-security-explanations.fallback.test.ts @@ -111,7 +111,7 @@ describe("web3SecurityExplanation - provider fallback (#1525)", () => { it("handles CircuitBreakerError by fast-failing with circuit breaker explanation", async () => { const { CircuitBreakerError } = await import("@/ai/resilience"); - mockGenerate.mockRejectedValue(new CircuitBreakerError("test-breaker", 60000)); + mockGenerate.mockRejectedValue(new CircuitBreakerError("test-breaker")); const result = await web3SecurityExplanation(input); diff --git a/src/ai/flows/web3-security-explanations.vram.test.ts b/src/ai/flows/web3-security-explanations.vram.test.ts index b7ee29e3..fb269dd0 100644 --- a/src/ai/flows/web3-security-explanations.vram.test.ts +++ b/src/ai/flows/web3-security-explanations.vram.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { resetModelCircuitBreakers } from "@/ai/resilience"; const mockGenerate = vi.fn(); @@ -37,6 +37,7 @@ const goodReply = { describe("web3SecurityExplanation - insufficient VRAM (#1525)", () => { beforeEach(() => { + resetModelCircuitBreakers(); mockGenerate.mockReset(); if (typeof mockSetCached === "function") mockSetCached.mockReset(); if (typeof resetModelCircuitBreakers === "function") resetModelCircuitBreakers(); @@ -44,6 +45,10 @@ describe("web3SecurityExplanation - insufficient VRAM (#1525)", () => { vi.spyOn(console, "error").mockImplementation(() => {}); }); + afterEach(() => { + resetModelCircuitBreakers(); + }); + it("does not crash when the local model does not fit in memory", async () => { mockGenerate.mockRejectedValue(OOM); diff --git a/tests/prisma-drift-check.test.ts b/tests/prisma-drift-check.test.ts new file mode 100644 index 00000000..fd44c0d3 --- /dev/null +++ b/tests/prisma-drift-check.test.ts @@ -0,0 +1,147 @@ +import { describe, it, expect } from "vitest"; +import fs from "node:fs"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; + +describe("Prisma Schema Drift Check (#1691)", () => { + const rootDir = process.cwd(); + const packageJsonPath = path.join(rootDir, "package.json"); + const ciWorkflowPath = path.join(rootDir, ".github", "workflows", "ci.yml"); + const contributingPath = path.join(rootDir, "CONTRIBUTING.md"); + const prismaReadmePath = path.join(rootDir, "prisma", "README.md"); + const schemaPath = path.join(rootDir, "prisma", "schema.prisma"); + + describe("npm scripts configuration", () => { + it("defines the db:check-drift script in package.json", () => { + expect(fs.existsSync(packageJsonPath)).toBe(true); + const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); + + expect(pkg.scripts).toHaveProperty("db:check-drift"); + const script = pkg.scripts["db:check-drift"]; + expect(script).toContain("prisma migrate diff"); + expect(script).toContain("--from-migrations prisma/migrations"); + expect(script).toContain("--to-schema prisma/schema.prisma"); + expect(script).toContain("--exit-code"); + }); + }); + + describe("CI workflow configuration (.github/workflows/ci.yml)", () => { + it("configures the prisma-drift-check job with PostgreSQL 16 service and shadow db", () => { + expect(fs.existsSync(ciWorkflowPath)).toBe(true); + const ciContent = fs.readFileSync(ciWorkflowPath, "utf8"); + + expect(ciContent).toContain("prisma-drift-check:"); + expect(ciContent).toContain("name: Prisma Schema Drift Check"); + expect(ciContent).toContain("image: postgres:16"); + expect(ciContent).toContain( + "SHADOW_DATABASE_URL: postgresql://ci:ci@localhost:5432/secureflow_shadow", + ); + expect(ciContent).toContain("CREATE DATABASE secureflow_shadow"); + expect(ciContent).toContain("npm run db:check-drift"); + expect(ciContent).toContain( + "Schema drift detected. Run 'npx prisma migrate dev --name ' and commit the new migration.", + ); + }); + + it("ensures release job depends on prisma-drift-check", () => { + const ciContent = fs.readFileSync(ciWorkflowPath, "utf8"); + const releaseJobIndex = ciContent.indexOf("release:"); + expect(releaseJobIndex).toBeGreaterThan(-1); + + const releaseSection = ciContent.slice(releaseJobIndex); + expect(releaseSection).toContain("prisma-drift-check"); + }); + }); + + describe("Documentation", () => { + it("documents db:check-drift in CONTRIBUTING.md setup and PR checklist", () => { + expect(fs.existsSync(contributingPath)).toBe(true); + const contributingContent = fs.readFileSync(contributingPath, "utf8"); + + expect(contributingContent).toContain("npm run db:check-drift"); + expect(contributingContent).toContain("SHADOW_DATABASE_URL"); + expect(contributingContent).toMatch(/- \[[ x]\] Run `npm run db:check-drift`/); + }); + + it("documents db:check-drift in prisma/README.md", () => { + expect(fs.existsSync(prismaReadmePath)).toBe(true); + const prismaReadmeContent = fs.readFileSync(prismaReadmePath, "utf8"); + + expect(prismaReadmeContent).toContain("npm run db:check-drift"); + }); + }); + + describe("Prisma migrate diff exit code contract", () => { + it("exits with 0 when comparing schema to itself (no drift)", () => { + expect(fs.existsSync(schemaPath)).toBe(true); + const isWindows = process.platform === "win32"; + const cmd = isWindows ? "cmd.exe" : "npx"; + const args = isWindows + ? [ + "/c", + "npx", + "prisma", + "migrate", + "diff", + "--from-schema", + "prisma/schema.prisma", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ] + : [ + "prisma", + "migrate", + "diff", + "--from-schema", + "prisma/schema.prisma", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ]; + + const res = spawnSync(cmd, args, { + cwd: rootDir, + encoding: "utf8", + }); + + expect(res.status).toBe(0); + expect(res.stdout).toContain("No difference detected"); + }); + + it("exits with 2 when diff detects unmigrated changes", () => { + expect(fs.existsSync(schemaPath)).toBe(true); + const isWindows = process.platform === "win32"; + const cmd = isWindows ? "cmd.exe" : "npx"; + const args = isWindows + ? [ + "/c", + "npx", + "prisma", + "migrate", + "diff", + "--from-empty", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ] + : [ + "prisma", + "migrate", + "diff", + "--from-empty", + "--to-schema", + "prisma/schema.prisma", + "--exit-code", + ]; + + const res = spawnSync(cmd, args, { + cwd: rootDir, + encoding: "utf8", + }); + + expect(res.status).toBe(2); + expect(res.stdout).toContain("[+] Added"); + }); + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index 35c96f21..88cadcc6 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -19,7 +19,13 @@ export default defineConfig({ "tests/**/*.test.ts", "scripts/**/*.test.ts", ], - exclude: ["**/node_modules/**", "**/dist/**", "**/*.redteam.test.ts", "**/*.redteam.test.tsx"], + exclude: [ + "**/node_modules/**", + "**/dist/**", + ...(process.env.npm_lifecycle_event === "test:redteam" || process.env.REDTEAM === "true" + ? [] + : ["**/*.redteam.test.ts", "**/*.redteam.test.tsx"]), + ], setupFiles: ["./vitest.setup.ts"], testTimeout: 20000, coverage: {