Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(ci): give one-off scripts the project's IPv4 Supavisor URL (#1890)
Brad Barbin43375fa9a3f8
diff --git a/ci/src/migrations.ts b/ci/src/migrations.tsindex d7cdc0f083..5d930112e8 100644--- a/ci/src/migrations.ts+++ b/ci/src/migrations.ts@@ -6,11 +6,12 @@ import { createClient } from "@supabase/supabase-js";import { $ } from "execa";import { client } from "./client";-import type { LedgerDatabase } from "./one-off-scripts";+import type { LedgerDatabase, SupavisorConfig } from "./one-off-scripts";import {oneOffScriptEnv,oneOffScriptOutcome,- selectPendingScripts+ selectPendingScripts,+ supavisorUrl} from "./one-off-scripts";import {SUPABASE_ACCESS_TOKEN,@@ -64,6 +65,41 @@ export type Workspace = {inngest_event_key: string | null;};+/**+ * The project's IPv4 Supavisor URL, for the one-off scripts (see+ * `supavisorUrl`). Null, with a warning, when the Management API does not+ * give one; the scripts then get the workspace's pooler URL as before.+ */+async function lookUpSupavisorUrl(+ workspace: Workspace,+ accessToken: string+): Promise<string | null> {+ const { project_id, database_password } = workspace;+ if (!project_id || !database_password) return null;+ try {+ const response = await fetch(+ `https://api.supabase.com/v1/projects/${project_id}/config/database/pooler`,+ { headers: { Authorization: `Bearer ${accessToken}` } }+ );+ if (!response.ok) {+ throw new Error(`${response.status} ${await response.text()}`);+ }+ const url = supavisorUrl(+ (await response.json()) as SupavisorConfig[],+ database_password+ );+ if (!url) throw new Error("no primary pooler listed");+ return url;+ } catch (e) {+ console.warn(+ `⚠️ 📜 No Supavisor URL for ${workspace.id}, using its pooler URL: ${+ e instanceof Error ? e.message : String(e)+ }`+ );+ return null;+ }+}+/*** Run the one-off scripts this workspace has not run yet, recording each in the* workspace's OWN `scriptRun` table. The scripts are whatever is in@@ -306,10 +342,17 @@ async function migrate(): Promise<void> {// migration. It still fails the overall run via `hasErrors`.// `$(options)` replaces `env` rather than merging it, so the scripts get// their own (`oneOffScriptEnv`).+ const isSelfHosted = connection_string?.startsWith("postgresql://");const scripts$ = $$({// @ts-ignoreenv: oneOffScriptEnv(env, {- poolerUrl: database_connection_pooler_url,+ poolerUrl:+ (!isSelfHosted &&+ (await lookUpSupavisorUrl(+ workspace,+ env.SUPABASE_ACCESS_TOKEN+ ))) ||+ database_connection_pooler_url,connectionString: connection_string,}),});diff --git a/ci/src/one-off-scripts.test.ts b/ci/src/one-off-scripts.test.tsindex aebbf706bb..51a6773d17 100644--- a/ci/src/one-off-scripts.test.ts+++ b/ci/src/one-off-scripts.test.ts@@ -14,7 +14,8 @@ import {ONE_OFF_SCRIPT_DEFERRED,oneOffScriptEnv,oneOffScriptOutcome,- selectPendingScripts+ selectPendingScripts,+ supavisorUrl} from "./one-off-scripts";function withFiles<T>(names: string[], fn: (dir: string) => T): T {@@ -160,6 +161,29 @@ test("passes a script the Postgres URL and not the database password", () => {);});+test("builds the IPv4 Supavisor URL of the primary database", () => {+ const pooler = {+ db_user: "postgres.abcdef",+ db_host: "aws-0-us-east-1.pooler.supabase.com",+ db_port: 6543,+ db_name: "postgres"+ };+ assert.equal(+ supavisorUrl(+ [+ { ...pooler, database_type: "READ_REPLICA", db_host: "replica" },+ { ...pooler, database_type: "PRIMARY" }+ ],+ "p@ss/word"+ ),+ "postgresql://postgres.abcdef:p%40ss%[email protected]:6543/postgres"+ );+ assert.equal(+ supavisorUrl([{ ...pooler, database_type: "READ_REPLICA" }], "x"),+ null+ );+});+// The runner records nothing for a deferred script and does not fail the// deploy, so a workspace with no Postgres URL retries it on a later deploy.test("journal-legacy-documents defers when it has no Postgres URL", () => {diff --git a/ci/src/one-off-scripts.ts b/ci/src/one-off-scripts.tsindex fe6fb5cb8c..412c8c2c54 100644--- a/ci/src/one-off-scripts.ts+++ b/ci/src/one-off-scripts.ts@@ -170,12 +170,39 @@ export function oneOffScriptOutcome(return exitCode === ONE_OFF_SCRIPT_DEFERRED ? "deferred" : "failed";}+/** One entry of the Management API's `GET /v1/projects/{ref}/config/database/pooler`. */+export type SupavisorConfig = {+ database_type: "PRIMARY" | "READ_REPLICA";+ db_user: string;+ db_host: string;+ db_port: number;+ db_name: string;+};++/**+ * The primary database's Supavisor URL (`aws-0-<region>.pooler.supabase.com`),+ * which resolves to IPv4. The workspace's own pooler URL is the project's+ * dedicated pooler on `db.<ref>.supabase.co`, which resolves to IPv6 only:+ * the deployed app reaches it, a GitHub-hosted runner (no IPv6) gets+ * ENETUNREACH. Null when the project lists no primary pooler.+ */+export function supavisorUrl(+ configs: SupavisorConfig[],+ password: string+): string | null {+ const primary = configs.find((c) => c.database_type === "PRIMARY");+ if (!primary) return null;+ const user = encodeURIComponent(primary.db_user);+ return `postgresql://${user}:${encodeURIComponent(password)}@${primary.db_host}:${primary.db_port}/${primary.db_name}`;+}+/*** The environment a one-off script runs with: the workspace's, without the- * database password (only `supabase db push` uses it), and the Postgres URL- * the app gets (deploy.ts), else a self-hosted connection string. A- * workspace with neither passes no `SUPABASE_DB_URL`, and a script that- * needs one defers (`ONE_OFF_SCRIPT_DEFERRED`).+ * database password (only `supabase db push` uses it), and a Postgres URL the+ * runner can reach — the project's Supavisor URL (`supavisorUrl`), else the+ * pooler URL the app gets (deploy.ts), else a self-hosted connection string.+ * A workspace with none passes no `SUPABASE_DB_URL`, and a script that needs+ * one defers (`ONE_OFF_SCRIPT_DEFERRED`).*/export function oneOffScriptEnv(env: Record<string, string | undefined>,diff --git a/scripts/one-off/README.md b/scripts/one-off/README.mdindex 656dd834e4..cec687d002 100644--- a/scripts/one-off/README.md+++ b/scripts/one-off/README.md@@ -33,9 +33,13 @@ Nobody runs these by hand in production. Merging is what ships them.- `SUPABASE_URL` and `SUPABASE_SERVICE_ROLE_KEY`, with the rest of theworkspace's Supabase settings.-- `SUPABASE_DB_URL`: the workspace's pooler URL, as the deployed app gets it,- else its connection string when that starts with `postgresql://`. A workspace- with neither passes no `SUPABASE_DB_URL`.+- `SUPABASE_DB_URL`: for a Supabase-hosted workspace, the project's Supavisor+ URL (`aws-0-<region>.pooler.supabase.com`), read from the Management API with+ the workspace's access token and password. The workspace's own pooler URL is+ on `db.<ref>.supabase.co`, which resolves to IPv6 only, and GitHub-hosted+ runners have no IPv6 (`ENETUNREACH`). Failing that, the workspace's pooler+ URL, as the deployed app gets it, else its connection string when that starts+ with `postgresql://`. A workspace with none passes no `SUPABASE_DB_URL`.- **No database password.** The runner removes `SUPABASE_DB_PASSWORD`; only`supabase db push` uses it. A script never needs it and must not read it.diff --git a/scripts/one-off/journal-legacy-documents.ts b/scripts/one-off/journal-legacy-documents.tsindex b65fcfaa3d..30e9c78f21 100644--- a/scripts/one-off/journal-legacy-documents.ts+++ b/scripts/one-off/journal-legacy-documents.ts@@ -21,7 +21,8 @@// written and the next deploy retries.//// Needs a Postgres connection: SUPABASE_DB_URL, which the migrations runner-// passes to one-off scripts (the workspace's pooler URL, as the app gets it).+// passes to one-off scripts (the project's IPv4 Supavisor URL, see+// scripts/one-off/README.md).// A workspace with no Postgres URL defers the script: it logs why and exits// with ONE_OFF_SCRIPT_DEFERRED (75, ci/src/one-off-scripts.ts), so the runner// records nothing and runs it again on the next deploy, without failing this