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
9 changes: 9 additions & 0 deletions pm-stack/aggregator/src/metadata-ingestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* Metadata *queries*; GitHub stays the system of record for the items.
*/
import type { Database } from "bun:sqlite";
import { mkdirSync } from "node:fs";
import { dirname } from "node:path";
import {
createPmMetadataStore,
parsePmMetadata,
Expand Down Expand Up @@ -388,6 +390,13 @@ export function storeOutcome(db: Database, outcome: MetadataCheckOutcome): void

/** Open (creating) the sweeper/listener database with the store installed. */
export function openMetadataStore(path: string, open: (path: string) => Database): Database {
// bun:sqlite creates the database file but not its parent directories,
// so a clean environment (no state directory yet) crashed the server at
// boot with SQLITE_CANTOPEN (#13293). Create the parent first, like the
// other state stores do on write; genuine open failures (permissions,
// a parent that is not a directory) still throw — only the
// missing-directory case self-heals.
if (path !== ":memory:") mkdirSync(dirname(path), { recursive: true });
const db = open(path);
createPmMetadataStore(db);
return db;
Expand Down
33 changes: 33 additions & 0 deletions pm-stack/aggregator/test/metadata-ingestion.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { describe, expect, test } from "bun:test";
import { Database } from "bun:sqlite";
import { existsSync } from "node:fs";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
artifactIdFor,
buildTriagePacket,
extractMetadataPayload,
openMetadataStore,
renderMetadataBlock,
runMetadataCheck,
sourceUrlFor,
Expand Down Expand Up @@ -232,3 +237,31 @@ describe("buildTriagePacket: the broker's required fields, pinned", () => {
}
});
});

describe("openMetadataStore: clean-environment boot (#13293)", () => {
test("creates a missing parent directory before opening", async () => {
const root = await mkdtemp(join(tmpdir(), "pm-metadata-store-"));
try {
const path = join(root, "state", "pm-aggregator", "pm-metadata.sqlite");
const db = openMetadataStore(path, (p) => new Database(p));
expect(
db.query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'pm_metadata_items'").get(),
).toEqual({ name: "pm_metadata_items" });
db.close();
expect(existsSync(path)).toBe(true);
} finally {
await rm(root, { recursive: true, force: true });
}
});

test("an unhealable parent still fails closed", async () => {
const root = await mkdtemp(join(tmpdir(), "pm-metadata-store-"));
try {
const blocker = join(root, "blocker");
await writeFile(blocker, "not a directory");
expect(() => openMetadataStore(join(blocker, "pm-metadata.sqlite"), (p) => new Database(p))).toThrow();
} finally {
await rm(root, { recursive: true, force: true });
}
});
});