Phase 7
This commit is contained in:
189 files changed
+9873
-655
No files matched your search
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Builds the `mariadb` pool config from `DATABASE_URL`, shared by every
|
||||
* Prisma Client instance in the app (the main app client, the seed
|
||||
* script, and the integration-test fixtures) so the UTC-timezone and
|
||||
* connection-limit handling can't drift between them. See
|
||||
* src/lib/db/prisma.ts for why `timezone`/`sessionVariables` matter — this
|
||||
* file exists only because that reasoning needs to apply to all three
|
||||
* call sites, not because the logic itself is complex.
|
||||
*/
|
||||
export function buildMysqlPoolConfig(databaseUrl: string | undefined) {
|
||||
if (!databaseUrl) throw new Error("DATABASE_URL is not set.");
|
||||
|
||||
// Accept both `mysql://` and `mariadb://` — the `mariadb` driver's own
|
||||
// string parser only recognizes the latter, but the `URL` global parses
|
||||
// either scheme fine since we build the pool config object ourselves.
|
||||
const url = new URL(databaseUrl);
|
||||
|
||||
return {
|
||||
host: url.hostname,
|
||||
port: url.port ? Number(url.port) : 3306,
|
||||
user: decodeURIComponent(url.username),
|
||||
password: decodeURIComponent(url.password),
|
||||
database: url.pathname.replace(/^\//, ""),
|
||||
connectionLimit: Number(process.env.DATABASE_CONNECTION_LIMIT ?? 5),
|
||||
connectTimeout: Number(process.env.DATABASE_CONNECT_TIMEOUT_MS ?? 10_000),
|
||||
// Load-bearing, not cosmetic — see src/lib/db/prisma.ts's file header.
|
||||
timezone: "Etc/UTC",
|
||||
sessionVariables: { time_zone: "+00:00" },
|
||||
ssl: process.env.DATABASE_SSL === "true" ? { rejectUnauthorized: true } : undefined,
|
||||
};
|
||||
}
|
||||
+19
-9
@@ -1,24 +1,34 @@
|
||||
import { PrismaClient } from "@/generated/prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
||||
import { buildMysqlPoolConfig } from "./mysql-pool-config";
|
||||
|
||||
/**
|
||||
* Single Prisma Client instance for the whole app.
|
||||
*
|
||||
* Prisma 7 requires a driver adapter instead of a `datasource.url` in the
|
||||
* schema (see prisma/schema.prisma). This is the ONLY file that imports
|
||||
* `@prisma/adapter-pg` — switching to MySQL later means swapping this one
|
||||
* adapter (`@prisma/adapter-mariadb`), updating `datasource.provider` in
|
||||
* the schema, and re-running `prisma migrate dev`. No other file in the
|
||||
* app touches the database driver directly.
|
||||
* schema (see prisma/schema.prisma). This is the ONLY file that imports a
|
||||
* database driver directly — every other file goes through `prisma`.
|
||||
*
|
||||
* Migrated from PostgreSQL (`@prisma/adapter-pg`) to MySQL in Phase 7 — see
|
||||
* docs/phase-7-summary.md for the full record. `@prisma/adapter-mariadb` is
|
||||
* Prisma's one official driver adapter for BOTH MySQL and MariaDB (there is
|
||||
* no separate "adapter-mysql" package): the underlying `mariadb` npm driver
|
||||
* speaks the MySQL wire protocol and is Prisma's documented, supported way
|
||||
* to connect to a genuine MySQL server. The package name is not a sign
|
||||
* we're accidentally targeting MariaDB — `datasource.provider` in
|
||||
* schema.prisma is `"mysql"`, and every environment this app runs against
|
||||
* (local Docker, CI, and the documented production target) is real MySQL.
|
||||
*
|
||||
* See mysql-pool-config.ts for why the pool is built from a parsed object
|
||||
* rather than passed the raw connection string — the short version is that
|
||||
* the UTC-timezone handling it sets is load-bearing, not cosmetic.
|
||||
*/
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
const adapter = new PrismaPg({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
});
|
||||
const adapter = new PrismaMariaDb(buildMysqlPoolConfig(process.env.DATABASE_URL));
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ??
|
||||
|
||||
Reference in new issue
Block a user