Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/build/neo4j/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* (json) output entirely.
*/

import type { ManagedTransaction } from "neo4j-driver";
import type { Logger } from "../../utils";
import type { EdgeRow, GraphRows, NodeRow, Prop } from "./rows";
import { chunk } from "./rows";
Expand Down Expand Up @@ -130,6 +131,28 @@ export async function boltWriter(
);
}
}
// Full runs reconcile the current application's artifact ownership. Artifact and ConfigKey
// ids are neutral across sibling analyzers, so remove only this app's stale ownership edges
// first and delete the shared subtree only when no Application still owns the artifact.
if (fullRun && appId !== null) {
const artifactIds = rows.nodes
.filter((n) => n.labels.includes("Artifact"))
.map((n) => n.value);
await withSession(session, async (s) => {
await s.executeWrite(async (tx: ManagedTransaction) => {
await tx.run(
`MATCH (a:Application {id: $appId})-[r:HAS_ARTIFACT]->(artifact:Artifact) ` +
`WHERE NOT artifact.id IN $artifactIds DELETE r`,
{ appId, artifactIds },
);
await tx.run(
`MATCH (artifact:Artifact) WHERE NOT ()-[:HAS_ARTIFACT]->(artifact) ` +
`OPTIONAL MATCH (artifact)-[:DEFINES_CONFIG]->(config:ConfigKey) ` +
`DETACH DELETE config, artifact`,
);
});
});
}

// 3. diff content_hash.
const dbHash = new Map<string, string | null>();
Expand Down
4 changes: 4 additions & 0 deletions src/build/neo4j/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function wipe(appId: string): string {
"OPTIONAL MATCH (a)-[:TS_HAS_MODULE]->(m:TSModule)",
"OPTIONAL MATCH (m)-[:TS_DECLARES|TS_HAS_METHOD|TS_HAS_FIELD|TS_HAS_BODY_NODE*1..]->(x)",
"DETACH DELETE x, m, a;",
"MATCH (artifact:Artifact)",
"WHERE NOT ()-[:HAS_ARTIFACT]->(artifact)",
"OPTIONAL MATCH (artifact)-[:DEFINES_CONFIG]->(config:ConfigKey)",
"DETACH DELETE config, artifact;",
].join("\n");
}

Expand Down
9 changes: 8 additions & 1 deletion test/artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { analyze } from "../src/core";
import { project } from "../src/build/neo4j";
import { project, renderCypher } from "../src/build/neo4j";
import type { AnalysisOptions } from "../src/options";

const FIXTURE = path.resolve(import.meta.dir, "fixtures/artifacts-app");
Expand Down Expand Up @@ -246,4 +246,11 @@ describe("Neo4j projection — neutral :Artifact/:Package (#101)", () => {
rows.edges.some((e) => e.type === "TS_UNRESOLVED_IMPORT" && String(e.to.value).endsWith("/@external/left-pad")),
).toBe(true);
});

test("snapshot cleanup removes only unowned artifact subtrees", () => {
const cypher = renderCypher(rows, root.id);
expect(cypher).toContain("WHERE NOT ()-[:HAS_ARTIFACT]->(artifact)");
expect(cypher).toContain("OPTIONAL MATCH (artifact)-[:DEFINES_CONFIG]->(config:ConfigKey)");
expect(cypher).toContain("DETACH DELETE config, artifact");
});
});
24 changes: 24 additions & 0 deletions test/neo4j-bolt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ containerSuite("neo4j bolt writer", () => {
120_000,
);

test(
"a full run prunes an artifact no longer owned by the application",
async () => {
const rows = project((await analyze(optsFor())).application);
const victim = rows.nodes.find((n) => n.labels.includes("Artifact"))?.value;
expect(victim).toBeDefined();

const reduced = {
nodes: rows.nodes.filter((n) => n.value !== victim),
edges: rows.edges.filter((e) => e.from.value !== victim && e.to.value !== victim),
};
await boltWriter(reduced, cfg, log, true);

expect(
await num(
"MATCH (:Application)-[:HAS_ARTIFACT]->(a:Artifact {id:$id}) RETURN count(a)",
{ id: victim },
),
).toBe(0);
expect(await num("MATCH (a:Artifact {id:$id}) RETURN count(a)", { id: victim })).toBe(0);
},
120_000,
);

test(
"migrates a 1.1.0-shaped graph to the current schema, wiping legacy residue (#46)",
async () => {
Expand Down