forked from UsefulSoftwareCo/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(host-cloudflare): configurable db seam — D1 default, Neon/Postgres via Hyperdrive opt-in #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amondnet
wants to merge
4
commits into
main
Choose a base branch
from
amondnet/evaluate-neon-planetscale-hyperdrive-for-apps-ho
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0a08c9f
feat(host-cloudflare): make db seam configurable (D1 default, Neon/Po…
amondnet 8ec707e
fix(host-cloudflare): request-scope the Postgres connection (Cloudfla…
amondnet c69d503
chore(host-cloudflare): address review (remove em-dashes, pin misconf…
amondnet c3c8f56
chore(host-cloudflare): apply AI code review suggestions
amondnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| dist/ | ||
| .dev-db/ | ||
| .dev.vars | ||
| .wrangler/ | ||
| wrangler.preview.jsonc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // --------------------------------------------------------------------------- | ||
| // Local dev Postgres via PGlite (no Docker, no install) | ||
| // --------------------------------------------------------------------------- | ||
| // | ||
| // Exposes an in-process PGlite instance over a TCP socket so Hyperdrive's | ||
| // `localConnectionString` can connect to it like a real Postgres server, for | ||
| // exercising the opt-in Postgres seam (src/db/postgres.ts) under `wrangler dev`. | ||
| // | ||
| // Unlike apps/cloud (which applies generated drizzle-kit migrations), the | ||
| // Cloudflare host brings its schema up at RUNTIME via | ||
| // `ensureDrizzleRuntimeSchemaFromTables`, so this script does the same here: | ||
| // the dev DB and the deployed Worker share one schema bring-up code path. | ||
|
|
||
| import { execSync } from "node:child_process"; | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
| import { dirname, resolve } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { PGlite } from "@electric-sql/pglite"; | ||
| import { PGLiteSocketServer } from "@electric-sql/pglite-socket"; | ||
| import { drizzle } from "drizzle-orm/pglite"; | ||
| import { | ||
| createDrizzleRuntimeSchemaFromTables, | ||
| ensureDrizzleRuntimeSchemaFromTables, | ||
| } from "@executor-js/fumadb/adapters/drizzle"; | ||
| import { collectTables } from "@executor-js/api/server"; | ||
|
|
||
| import { CLOUDFLARE_NAMESPACE, CLOUDFLARE_SCHEMA_VERSION } from "../src/config"; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| // Port + data dir default to the dev values but are env-overridable so a second | ||
| // throwaway instance can run alongside `bun dev`. | ||
| const PORT = Number(process.env.DEV_DB_PORT ?? 5433); | ||
| const DB_PATH = process.env.DEV_DB_PATH | ||
| ? resolve(process.env.DEV_DB_PATH) | ||
| : resolve(__dirname, "../.dev-db"); | ||
|
|
||
| // Reap any orphan dev-db from a previous run that didn't shut down cleanly, | ||
| // otherwise the new instance can't bind to PORT. | ||
| function reapStaleDevDb() { | ||
| const out = execSync(`lsof -ti tcp:${PORT} -sTCP:LISTEN 2>/dev/null || true`, { | ||
| encoding: "utf8", | ||
| }); | ||
| const pids = out.trim().split("\n").filter(Boolean); | ||
| if (pids.length === 0) return false; | ||
|
|
||
| for (const pid of pids) { | ||
| const cmd = execSync(`ps -p ${pid} -o args= 2>/dev/null || true`, { | ||
| encoding: "utf8", | ||
| }).trim(); | ||
| // The PID can exit between `lsof` and `ps` (the stale instance shutting down | ||
| // on its own): an empty cmd means it is already gone and the port is free, so | ||
| // skip it rather than misreading it as an unexpected process and bailing. | ||
| if (cmd === "") continue; | ||
| if (!cmd.includes("dev-db.ts")) { | ||
| console.error(`[dev-db] Port ${PORT} is held by an unexpected process (pid ${pid}): ${cmd}`); | ||
| console.error(`[dev-db] Refusing to kill it. Free the port and retry.`); | ||
| process.exit(1); | ||
| } | ||
| console.log(`[dev-db] Reaping stale dev-db (pid ${pid})`); | ||
| // process.kill (a Node built-in, no subshell) instead of `kill -KILL`; | ||
| // tolerate ESRCH if the process exited between the check above and here. | ||
| try { | ||
| process.kill(Number(pid), "SIGKILL"); | ||
| } catch { | ||
| // already gone | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| if (reapStaleDevDb()) { | ||
| // Give the kernel a beat to release the socket before we try to bind. | ||
| await sleep(200); | ||
| } | ||
|
|
||
| console.log(`[dev-db] Starting PGlite at ${DB_PATH}`); | ||
| const db = await PGlite.create(DB_PATH); | ||
|
|
||
| console.log("[dev-db] Ensuring runtime schema (provider=postgresql)"); | ||
| const options = { | ||
| tables: collectTables(), | ||
| namespace: CLOUDFLARE_NAMESPACE, | ||
| version: CLOUDFLARE_SCHEMA_VERSION, | ||
| provider: "postgresql" as const, | ||
| }; | ||
| const schema = createDrizzleRuntimeSchemaFromTables(options); | ||
| await ensureDrizzleRuntimeSchemaFromTables(drizzle(db, { schema }), options); | ||
|
|
||
| const server = new PGLiteSocketServer({ db, port: PORT, host: "127.0.0.1" }); | ||
| await server.start(); | ||
| console.log(`[dev-db] Listening on postgresql://postgres:postgres@127.0.0.1:${PORT}/postgres`); | ||
|
|
||
| const shutdown = async () => { | ||
| console.log("\n[dev-db] Shutting down"); | ||
| await server.stop(); | ||
| await db.close(); | ||
| process.exit(0); | ||
| }; | ||
|
|
||
| process.on("SIGINT", shutdown); | ||
| process.on("SIGTERM", shutdown); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.