sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ Cloudflare/ D1/ Drizzle.test.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
import * as Drizzle from "@alchemy.run/drizzle";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as d1 from "@distilled.cloud/cloudflare/d1";
import { Interaction, type InteractionError, type SelectOptions } from "alchemy/Interaction";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientRequest from "effect/http/HttpClientRequest";
import type * as HttpClientResponse from "effect/http/HttpClientResponse";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Predicate from "effect/Predicate";
import * as Schedule from "effect/Schedule";
import Stack from "./fixtures/drizzle-stack.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: Layer.mergeAll(Cloudflare.providers(), Drizzle.providers()),
  state: Cloudflare.state(),
});

const HOOK_TIMEOUT = 300_000;
const TEST_TIMEOUT = 120_000;

class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
  status: number;
  body: string;
}> {}

// Bounded spaced schedule — rides out fresh-workers.dev cold-start
// propagation without blowing past the test timeout.
const ready = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(45)]);

/** Retry an HTTP call until it returns 200. */
const untilOk = <E, R>(eff: Effect.Effect<HttpClientResponse.HttpClientResponse, E, R>) =>
  eff.pipe(
    Effect.flatMap((res) =>
      res.status === 200
        ? Effect.succeed(res)
        : res.text.pipe(
            Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
          ),
    ),
    Effect.retry({
      while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
      schedule: ready,
    }),
  );

/**
 * End-to-end test of the Drizzle-on-D1 surface against a real Cloudflare
 * Worker + D1 database:
 *
 * - deploy applies the checked-in migration (generated by drizzle-kit from
 *   `fixtures/drizzle-schema.ts`) via `migrationsDir` into the
 *   `drizzle_migrations` table;
 * - `Drizzle.D1(d1, { relations })` drives insert/select/relational queries
 *   through the drizzle-orm `effect-d1` driver;
 * - `SQL.D1(d1)` drives raw tagged-template SQL through `@effect/sql-d1`.
 */
const stack = beforeAll(deploy(Stack), { timeout: HOOK_TIMEOUT });
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
  timeout: HOOK_TIMEOUT,
});

test(
  "Drizzle.D1 + SQL.D1 exercise the deployed database",
  Effect.gen(function* () {
    const { url } = yield* stack;

    // drizzle insert ... returning
    const userRes = yield* untilOk(
      HttpClient.execute(
        HttpClientRequest.post(`${url}/users`).pipe(
          HttpClientRequest.bodyJsonUnsafe({
            name: "alice",
            email: "alice@example.com",
          }),
        ),
      ),
    );
    const { user } = (yield* userRes.json) as {
      user: { id: number; name: string; email: string };
    };
    expect(user.name).toBe("alice");
    expect(user.email).toBe("alice@example.com");
    expect(typeof user.id).toBe("number");

    const postRes = yield* untilOk(
      HttpClient.execute(
        HttpClientRequest.post(`${url}/posts`).pipe(
          HttpClientRequest.bodyJsonUnsafe({
            userId: user.id,
            title: "hello world",
          }),
        ),
      ),
    );
    const { post } = (yield* postRes.json) as {
      post: { id: number; userId: number; title: string };
    };
    expect(post.userId).toBe(user.id);
    expect(post.title).toBe("hello world");

    // drizzle select
    const listRes = yield* untilOk(HttpClient.get(`${url}/users`));
    const { users } = (yield* listRes.json) as {
      users: Array<{ id: number; name: string }>;
    };
    expect(users.map((u) => u.name)).toContain("alice");

    // drizzle relational query (RQB v2): user with posts
    const relRes = yield* untilOk(HttpClient.get(`${url}/users/${user.id}`));
    const rel = (yield* relRes.json) as {
      user: { id: number; posts: Array<{ title: string }> };
    };
    expect(rel.user.id).toBe(user.id);
    expect(rel.user.posts.map((p) => p.title)).toContain("hello world");

    // raw @effect/sql-d1 tagged-template client
    const sqlRes = yield* untilOk(HttpClient.get(`${url}/sql/users`));
    const { rows } = (yield* sqlRes.json) as {
      rows: Array<{ id: number; name: string; email: string }>;
    };
    expect(rows.map((r) => r.email)).toContain("alice@example.com");
  }),
  {
    tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"],
    timeout: TEST_TIMEOUT,
  },
);

const USERS_SCHEMA = `
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const users = sqliteTable("users", {
  id: integer("id").primaryKey(),
  name: text("name").notNull(),
});
`;

// `name` renamed to `full_name`, plus a new `posts` table.
const RENAMED_USERS_SCHEMA = `
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const users = sqliteTable("users", {
  id: integer("id").primaryKey(),
  fullName: text("full_name").notNull(),
});

export const posts = sqliteTable("posts", {
  id: integer("id").primaryKey(),
  title: text("title").notNull(),
});
`;

const queryAll = Effect.fn(function* <T>(accountId: string, databaseId: string, sql: string) {
  const queryDb = yield* d1.queryDatabase;
  const result = yield* queryDb({ accountId, databaseId, sql });
  return (result.result[0]?.results ?? []) as T[];
});

class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {}

const waitForDatabaseToBeDeleted = (accountId: string, databaseId: string) =>
  d1.getDatabase({ accountId, databaseId }).pipe(
    Effect.flatMap(() => Effect.fail(new DatabaseStillExists())),
    Effect.retry({
      while: (e) => e._tag === "DatabaseStillExists",
      schedule: Schedule.max([Schedule.exponential("200 millis"), Schedule.recurs(10)]),
    }),
    Effect.catchTag("DatabaseNotFound", () => Effect.void),
  );

test.provider(
  "Drizzle.Schema snapshot updates migrate the live database, renames included",
  (stack) =>
    Effect.gen(function* () {
      const { accountId } = yield* yield* CloudflareEnvironment;
      const fs = yield* FileSystem.FileSystem;
      const path = yield* Path.Path;

      // Inside the workspace so the schema module resolves `drizzle-orm`.
      const tmp = path.join(yield* Effect.sync(() => process.cwd()), ".alchemy", "tmp");
      yield* fs.makeDirectory(tmp, { recursive: true });
      const root = yield* fs.makeTempDirectoryScoped({ directory: tmp, prefix: "d1-drizzle-" });
      const schemaPath = path.join(root, "schema.ts");
      const out = path.join(root, "migrations");
      yield* fs.writeFileString(schemaPath, USERS_SCHEMA);

      const deploy = (source: string) =>
        stack.deploy(
          Effect.gen(function* () {
            const schema = yield* Drizzle.Schema("live-schema", {
              dialect: "sqlite",
              schema: source,
              out,
            });
            return yield* Cloudflare.D1.Database("LiveSchemaDatabase", {
              migrations: { dir: schema.out, table: "drizzle_migrations" },
            });
          }),
        );

      yield* stack.destroy();
      const database = yield* deploy(schemaPath);
      const query = <T>(sql: string) => queryAll<T>(accountId, database.databaseId, sql);
      yield* query("INSERT INTO users (id, name) VALUES (1, 'alice');");

      // Rename the column; answer the rename-or-create question with rename.
      // A new file: the process has already imported (and cached) the first.
      const renamedPath = path.join(root, "schema-renamed.ts");
      yield* fs.writeFileString(renamedPath, RENAMED_USERS_SCHEMA);
      const asked: Array<string> = [];
      const base = yield* Interaction;
      const scripted = Interaction.of({
        ...base,
        prompt: {
          ...base.prompt,
          select: <Value>(options: SelectOptions<Value>) =>
            Effect.sync(() => {
              asked.push(options.message);
              const rename = options.options.find(
                ({ value }) => Predicate.hasProperty(value, "type") && value.type === "rename",
              );
              return (rename ?? options.options[0]!).value;
            }) as Effect.Effect<Value, InteractionError>,
        },
      });
      const updated = yield* deploy(renamedPath).pipe(Effect.provideService(Interaction, scripted));
      expect(updated.databaseId).toEqual(database.databaseId);
      expect(asked).toEqual(["Is column users.full_name created or renamed from another column?"]);

      // The row survived: the column was renamed, not dropped and recreated.
      expect(yield* query("SELECT id, full_name FROM users;")).toEqual([
        { id: 1, full_name: "alice" },
      ]);
      const tables = yield* query<{ name: string }>(
        "SELECT name FROM sqlite_master WHERE type='table';",
      );
      expect(tables.map((t) => t.name)).toContain("posts");
      const applied = yield* query<{ n: number }>("SELECT count(*) AS n FROM drizzle_migrations;");
      expect(applied).toEqual([{ n: 2 }]);

      // A redeploy with no schema change applies nothing new.
      yield* deploy(renamedPath);
      expect(yield* query("SELECT count(*) AS n FROM drizzle_migrations;")).toEqual([{ n: 2 }]);

      yield* stack.destroy();
      yield* waitForDatabaseToBeDeleted(accountId, database.databaseId);
    }).pipe(Effect.scoped),
  {
    tags: ["provider:cloudflare", "provider:cloudflare:d1", "live"],
    timeout: TEST_TIMEOUT,
  },
);