From b46842d79020f0d834be0d1de8cfecf48da5092f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 4 Aug 2026 01:07:04 +0200 Subject: [PATCH] refactor(relay): generate mysql migrations manually per the alchemy drizzle guide alchemy's Drizzle.Schema auto-generation is Postgres-only (drizzle-kit has no programmatic mysql API), so follow the documented MySQL pattern: a drizzle.config.ts + `drizzle-kit generate`, with migrations checked in and the database/branch resources pointed at the static migrations/mysql dir. This also makes RelayMysqlDatabase's props identical to the provisioning PR, so the cutover deploy no-ops on the database itself. Co-Authored-By: Claude Fable 5 --- infra/relay/README.md | 4 +++- infra/relay/drizzle.config.ts | 11 +++++++++++ infra/relay/src/db.ts | 24 +++++++++++------------- 3 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 infra/relay/drizzle.config.ts diff --git a/infra/relay/README.md b/infra/relay/README.md index 28ba14e0e7..069566d984 100644 --- a/infra/relay/README.md +++ b/infra/relay/README.md @@ -40,7 +40,9 @@ credential, or authorization behavior. APNs delivery, and queue processing. - [`src/auth`](./src/auth) contains relay token and DPoP proof handling. - [`src/persistence/schema.ts`](./src/persistence/schema.ts) defines persisted relay state. Keep - schema and migration changes together. + schema and migration changes together: after editing the schema, run + `pnpm exec drizzle-kit generate` (see [`drizzle.config.ts`](./drizzle.config.ts)) and commit the + new `migrations/mysql/` directory — MySQL migrations are generated manually, not by the deploy. Shared request and response schemas live in [`packages/contracts/src/relay.ts`](../../packages/contracts/src/relay.ts). Shared client-side relay diff --git a/infra/relay/drizzle.config.ts b/infra/relay/drizzle.config.ts new file mode 100644 index 0000000000..8405e03f04 --- /dev/null +++ b/infra/relay/drizzle.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from "drizzle-kit"; + +// MySQL migrations are generated manually and checked in (alchemy's +// Drizzle.Schema auto-generation is Postgres-only). After changing +// src/persistence/schema.ts run `pnpm exec drizzle-kit generate` and commit +// the new migrations/mysql/_*/ directory; deploys apply it. +export default defineConfig({ + schema: "./src/persistence/schema.ts", + out: "./migrations/mysql", + dialect: "mysql", +}); diff --git a/infra/relay/src/db.ts b/infra/relay/src/db.ts index 1cf5e498d6..085ac68f4f 100644 --- a/infra/relay/src/db.ts +++ b/infra/relay/src/db.ts @@ -1,6 +1,5 @@ import type { MysqlClient } from "@effect/sql-mysql2/MysqlClient"; import * as Cloudflare from "alchemy/Cloudflare"; -import * as Drizzle from "alchemy/Drizzle"; import * as Planetscale from "alchemy/Planetscale"; import * as Alchemy from "alchemy"; import * as RemovalPolicy from "alchemy/RemovalPolicy"; @@ -35,14 +34,14 @@ export class RelayTransactions extends Context.Service< ); } +// Unlike Postgres, alchemy has no automatic Drizzle.Schema generation for +// MySQL (drizzle-kit exposes no programmatic mysql API): migrations are +// generated manually with `pnpm exec drizzle-kit generate` (drizzle.config.ts) +// and checked in; deploys apply whatever is committed here. +const mysqlMigrationsDir = "migrations/mysql"; + export const PlanetscaleDatabase = Effect.gen(function* () { const { stage } = yield* Alchemy.Stack; - const schema = yield* Drizzle.Schema("RelaySchema", { - schema: "./src/persistence/schema.ts", - out: "./migrations/mysql", - dialect: "mysql", - }); - const mode = relayDatabaseMode(stage); // The retired Postgres database stays in the stack (prod only) until the @@ -71,15 +70,14 @@ export const PlanetscaleDatabase = Effect.gen(function* () { const database = mode === "shared-database" - ? // Same resource id as the provisioning PR that created this database - // (with its baseline already applied) ahead of the cutover, so this - // deploy is a plain update; only migrationsDir moves from the static - // baseline dir to the schema resource for future schema evolution. + ? // Same resource + props as the provisioning PR that created this + // database (with its baseline already applied) ahead of the cutover, + // so this deploy no-ops on the database itself. yield* Planetscale.MySQLDatabase("RelayMysqlDatabase", { name: "t3coderelay-vitess", region: { slug: "us-west" }, clusterSize: "PS_20", - migrationsDir: schema.out, + migrationsDir: mysqlMigrationsDir, migrationsTable: "relay_migrations", replicas: 2, }).pipe(RemovalPolicy.retain()) @@ -91,7 +89,7 @@ export const PlanetscaleDatabase = Effect.gen(function* () { ? yield* Planetscale.MySQLBranch("RelayMysqlBranch", { database, isProduction: false, - migrationsDir: schema.out, + migrationsDir: mysqlMigrationsDir, migrationsTable: "relay_migrations", }) : undefined;