From 921facbf9c9dd3d30de5ac69c3da60245b2c5966 Mon Sep 17 00:00:00 2001 From: imeepos Date: Wed, 10 Jun 2026 03:38:22 -0700 Subject: [PATCH] test: check generated auth migration --- scripts/check-better-auth-migration.mjs | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/check-better-auth-migration.mjs diff --git a/scripts/check-better-auth-migration.mjs b/scripts/check-better-auth-migration.mjs new file mode 100644 index 0000000..da9192e --- /dev/null +++ b/scripts/check-better-auth-migration.mjs @@ -0,0 +1,61 @@ +import { spawn } from "node:child_process"; +import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const projectRoot = fileURLToPath(new URL("..", import.meta.url)); +const expectedFile = fileURLToPath( + new URL("../migrations/0001_better_auth_account_center.sql", import.meta.url), +); + +function run(command, args) { + return new Promise((resolve, reject) => { + const child = spawn(command, args, { + cwd: projectRoot, + stdio: "inherit", + shell: process.platform === "win32", + }); + + child.on("error", reject); + child.on("exit", (code) => { + if (code === 0) { + resolve(); + return; + } + + reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`)); + }); + }); +} + +const tempDir = await mkdtemp(join(tmpdir(), "cfw-auth-better-auth-")); +const generatedFile = join(tempDir, "better-auth.sql"); + +try { + await run("pnpm", [ + "exec", + "auth", + "generate", + "--config", + "src/auth.migration.ts", + "--output", + generatedFile, + "-y", + ]); + + const [expected, generated] = await Promise.all([ + readFile(expectedFile, "utf8"), + readFile(generatedFile, "utf8"), + ]); + + if (expected !== generated) { + throw new Error( + `${expectedFile} is out of date. Run pnpm db:generate and review the generated SQL.`, + ); + } + + console.log(`${expectedFile} is up to date.`); +} finally { + await rm(tempDir, { recursive: true, force: true }); +}