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
10 changes: 5 additions & 5 deletions .superset/setup.local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
27 changes: 27 additions & 0 deletions packages/db/src/local-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
9 changes: 7 additions & 2 deletions packages/db/src/local-proxy.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions packages/db/src/migrate-local.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
if (!isLocalProxy(env.DATABASE_URL)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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);
});
Loading