Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/prisma-schema-drift-check-1691.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DATABASE_URL="postgresql://neondb_owner:<Password>@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:<Password>@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"
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <change>' and commit the new migration."
Expand Down
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions prisma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions scripts/armoriq-verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
7 changes: 6 additions & 1 deletion src/ai/flows/generate-remediation-patch.backoff.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion src/ai/flows/generate-remediation-patch.vram.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/ai/flows/web3-security-explanations.fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion src/ai/flows/web3-security-explanations.vram.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -37,13 +37,18 @@ const goodReply = {

describe("web3SecurityExplanation - insufficient VRAM (#1525)", () => {
beforeEach(() => {
resetModelCircuitBreakers();
mockGenerate.mockReset();
if (typeof mockSetCached === "function") mockSetCached.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);

Expand Down
147 changes: 147 additions & 0 deletions tests/prisma-drift-check.test.ts
Original file line number Diff line number Diff line change
@@ -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 <change>' 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");
});
});
});
8 changes: 7 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading