Can't generate migrations using D1 on Cloudflare Workers

There is no way to run migrations while using D1 with Kysely on Cloudflare for 2 reasons:
  • The DB is exposed only at runtime, aka we cannot use the better-auth CLI to detect the configuration since it's only looking for variables exported (when using D1 we construct the configuration with each request and we only have a function)
  • The usage of getMigrations is still undocumented and probably abandoned since It's returning generic errors from D1 such as Error: D1_ERROR: not authorized: SQLITE_AUTH
const { getMigrations } = await import("better-auth/db");
const { auth } = await import("@/lib/auth");

const GET_HANDLER = async () => {
  try {
    console.log(auth().options.database.db);
    const { toBeCreated, toBeAdded, compileMigrations } = await getMigrations(
      auth().options,
    );

    if (!toBeCreated.length && !toBeAdded.length) {
      return Response.json(
        { status: "success", message: "No schema changes detected" },
        { status: 200 },
      );
    }

    const schema = await compileMigrations();

    return Response.json(
      { status: "success", message: schema },
      { status: 200 },
    );
  } catch (err) {
    console.error(err);
    return new Response(null, { status: 500 });
  }
};

export { GET_HANDLER as GET };


This is from within a Next.js app running with Opennext on Cloudflare Workers, everything else
better-auth
related works like a charm, but if we can't make migration generation is useless, applying the migrations can be done with
wrangler
against D1, but if we cannot generate the schema according to the current state of the DB we cannot add plugins efficiently by possible knowing what tables to alter and what to add or remove.

Any help would be much appreciated!
Was this page helpful?