diff --git a/.superset/setup.local.sh b/.superset/setup.local.sh index be07b6c3e5b..fa755f334f4 100755 --- a/.superset/setup.local.sh +++ b/.superset/setup.local.sh @@ -112,9 +112,9 @@ local_db_up() { return 1 fi - # Postgres health != proxy ready. migrate uses direct pg, but seed (and the - # app) query through the neon-http proxy, which starts + bootstraps a beat - # later. Probe a real query so the seed never races a cold proxy. + # Postgres health != proxy ready. migrate, seed and the app all query through + # the neon-http proxy, which starts + bootstraps a beat later. Probe a real + # query so they never race a cold proxy. echo " Waiting for neon-proxy to serve queries on :$LOCAL_NEON_PROXY_PORT..." local j proxy_ready=0 for j in $(seq 1 30); do @@ -158,8 +158,8 @@ local_db_up() { local_migrate() { echo "📜 Applying database migrations..." - if ! bun run db:migrate; then - error "db:migrate failed" + if ! bun run db:migrate:local; then + error "db:migrate:local failed" return 1 fi success "Migrations applied" diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index e2a14adc417..df03ed1f048 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -42,7 +42,7 @@ validation, and `setup.local.sh` runs everything against a local Docker stack. 1. Copies `.env.local.example` → `.env` 2. Allocates a per-workspace port range so multiple worktrees don't collide 3. Brings up Postgres + neon-proxy + Redis (behind an HTTP shim, for the relay) via `docker compose` (project-scoped to this worktree) -4. Runs `bun install` and `bun run db:migrate` +4. Runs `bun install` and `bun run db:migrate:local` 5. Seeds a `Local Admin` dev account via `bun run db:seed-dev` 6. Writes a gitignored `.superset/config.local.json` overlay so subsequent worktrees automatically use this setup diff --git a/package.json b/package.json index d4a36b5c3ec..e62e441d514 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "test:release": "bun test scripts/release", "db:push": "bun run --cwd packages/db push", "db:migrate": "bun run --cwd packages/db migrate", + "db:migrate:local": "bun run --cwd packages/db migrate:local", "db:seed-dev": "NODE_ENV=development bun run packages/auth/src/seed-dev.ts", "db:seed-cloud-auth-token": "bun run packages/auth/src/seed-cloud-auth-token.ts", "db:seed-cloud-mobile-token": "bun run packages/auth/src/seed-cloud-mobile-token.ts", diff --git a/packages/db/package.json b/packages/db/package.json index ed7445a9682..c7216d5f7aa 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -41,6 +41,7 @@ "clean": "git clean -xdf .cache .turbo dist node_modules", "push": "drizzle-kit push", "migrate": "drizzle-kit migrate", + "migrate:local": "bun run src/migrate-local.ts", "studio": "drizzle-kit studio", "typecheck": "tsc --noEmit --emitDeclarationOnly false" }, diff --git a/packages/db/src/local-proxy.test.ts b/packages/db/src/local-proxy.test.ts new file mode 100644 index 00000000000..d17bc3f018b --- /dev/null +++ b/packages/db/src/local-proxy.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, test } from "bun:test"; +import { isLocalProxy } from "./local-proxy"; + +describe("isLocalProxy", () => { + test("accepts the local proxy URL", () => { + expect( + isLocalProxy("postgres://postgres:postgres@db.localtest.me:3010/main"), + ).toBe(true); + }); + + test("rejects a remote or invalid URL", () => { + expect(isLocalProxy("postgres://u:p@ep-x.neon.tech/main")).toBe(false); + expect(isLocalProxy("not a url")).toBe(false); + }); + + test("rejects a local hostname that the driver does not connect to", () => { + expect( + isLocalProxy( + "postgres://u:p@db.localtest.me:3010/main?host=remote.example", + ), + ).toBe(false); + expect( + isLocalProxy("postgres://remote.example\\@db.localtest.me:3010/main"), + ).toBe(false); + expect(isLocalProxy("socket://db.localtest.me/tmp/sock")).toBe(false); + }); +}); diff --git a/packages/db/src/local-proxy.ts b/packages/db/src/local-proxy.ts index 8eb89f2d624..10ea74df8cf 100644 --- a/packages/db/src/local-proxy.ts +++ b/packages/db/src/local-proxy.ts @@ -1,10 +1,15 @@ -import { neonConfig } from "@neondatabase/serverless"; +import { Client, neonConfig } from "@neondatabase/serverless"; const LOCAL_DATABASE_HOST = "db.localtest.me"; export function isLocalProxy(databaseUrl: string): boolean { try { - return new URL(databaseUrl).hostname === LOCAL_DATABASE_HOST; + // The driver lets `?host=` or a backslash override the URL hostname, so + // also check the host that the driver itself would connect to. + return ( + new URL(databaseUrl).hostname === LOCAL_DATABASE_HOST && + new Client({ connectionString: databaseUrl }).host === LOCAL_DATABASE_HOST + ); } catch { return false; } diff --git a/packages/db/src/migrate-local.ts b/packages/db/src/migrate-local.ts new file mode 100644 index 00000000000..6abf9adb99c --- /dev/null +++ b/packages/db/src/migrate-local.ts @@ -0,0 +1,37 @@ +import path from "node:path"; +import { Pool } from "@neondatabase/serverless"; +import { drizzle } from "drizzle-orm/neon-serverless"; +import { migrate } from "drizzle-orm/neon-serverless/migrator"; +import { env } from "./env"; +import { configureLocalProxy, isLocalProxy } from "./local-proxy"; + +/** + * Applies migrations to the local Docker stack through neon-proxy. + * `drizzle-kit migrate` cannot: no TCP Postgres driver is installed, so it + * falls back to Neon's websocket driver, which reaches only remote Neon. + * + * Usage: bun run db:migrate:local + */ +async function migrateLocal(): Promise { + if (!isLocalProxy(env.DATABASE_URL)) { + throw new Error( + "DATABASE_URL is not the local proxy (db.localtest.me). Use `bun run db:migrate` for a Neon database.", + ); + } + configureLocalProxy(); + + const pool = new Pool({ connectionString: env.DATABASE_URL }); + try { + await migrate(drizzle({ client: pool }), { + migrationsFolder: path.resolve(__dirname, "../drizzle"), + }); + } finally { + await pool.end(); + } + console.log("Local database is migrated"); +} + +migrateLocal().catch((error) => { + console.error(error); + process.exit(1); +});