test: check generated auth migration

This commit is contained in:
2026-06-10 03:38:22 -07:00
parent 8a20e5cb85
commit 921facbf9c

View File

@@ -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 });
}