Skip to content

Commit

Permalink
Merge pull request #100 from change-engine/d1
Browse files Browse the repository at this point in the history
Auto run d1 migrations
  • Loading branch information
carlos-alberto authored Apr 17, 2024
2 parents fd74b54 + ec89907 commit 7ad1ebf
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Miniflare, createFetchMock } from "miniflare";
import toml from "toml";
import fs from "fs";
import { Env } from "./types";
import { JWTPayload, SignJWT, exportJWK, generateKeyPair } from "jose";
import { Miniflare, createFetchMock } from "miniflare";
import nodefs from "node:fs/promises";
import path from "node:path";
import createClient from "openapi-fetch";
import consumers from "stream/consumers";
import toml from "toml";
import { URLSearchParams } from "url";
import createClient from "openapi-fetch";
import { generateKeyPair, SignJWT, JWTPayload, exportJWK } from "jose";
import { Env } from "./types";

// eslint-disable-next-line @typescript-eslint/ban-types
export const setupTests = async <Paths extends {}>(bindings: Env) => {
Expand All @@ -23,38 +25,71 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
...bindings,
},
queueConsumers: (config.queues?.consumers ?? []).map(
({ queue }: { queue: string }) => queue,
({ queue }: { queue: string }) => queue
),
queueProducers: Object.fromEntries(
(config.queues?.producers ?? []).map(
({ binding, queue }: { queue: string; binding: string }) => [
binding,
queue,
],
),
]
)
),
r2Buckets: (config.r2_buckets ?? []).map(
({ binding }: { binding: string }) => binding,
({ binding }: { binding: string }) => binding
),
kvNamespaces: (config.kv_namespaces ?? []).map(
({ binding }: { binding: string }) => binding,
({ binding }: { binding: string }) => binding
),
d1Databases: (config.d1_databases ?? []).map(
({ binding }: { binding: string }) => binding,
({ binding }: { binding: string }) => binding
),
serviceBindings: Object.fromEntries(
(config.services ?? []).map(
({ binding, service }: { binding: string; service: string }) => [
binding,
service,
],
),
]
)
),
fetchMock,
});

const url = await miniflare.ready;

// Hack until miniflare supports auto running D1 migrations.
// Note: Each migration file can contain only a single statement.
await Promise.all(
(config.d1_databases ?? []).map(
async ({ binding }: { binding: string }) => {
const d1 = await miniflare.getD1Database(binding);

const migrationsDir = "./migrations";
for (const migrationName of (await nodefs.readdir(migrationsDir)).sort(
(a, b) => {
const migrationNumberA = parseInt(a.split("_")[0]!);
const migrationNumberB = parseInt(b.split("_")[0]!);
if (migrationNumberA < migrationNumberB) {
return -1;
}
if (migrationNumberA > migrationNumberB) {
return 1;
}

// numbers must be equal
return 0;
}
)) {
const migrationPath = path.join(migrationsDir, migrationName);
let migration = await nodefs.readFile(migrationPath, "utf8");
// Remove migration comment
migration = migration.split("\n").slice(1).join(" ");
await d1.exec(migration);
}
}
)
);

const client = createClient<Paths>({ baseUrl: url.toString() });

return {
Expand Down Expand Up @@ -91,7 +126,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
headers: {
"Content-Type": "application/json",
},
},
}
)
.persist();
fetchMock
Expand All @@ -106,7 +141,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
headers: {
"Content-Type": "application/json",
},
},
}
)
.persist();
return await new SignJWT(claims)
Expand Down Expand Up @@ -138,7 +173,7 @@ export function recordRequest(
data: string | object | Buffer | undefined,
responseOptions?: {
headers: Record<string, string | string[] | undefined>;
},
}
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ({ body }: any) => {
Expand All @@ -154,15 +189,15 @@ export function recordFormRequest(
data: string | object | Buffer | undefined,
responseOptions?: {
headers: Record<string, string | string[] | undefined>;
},
}
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ({ body }: any) => {
consumers
.text(body)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.then((data: any) =>
cb(Object.fromEntries(new URLSearchParams(data).entries())),
cb(Object.fromEntries(new URLSearchParams(data).entries()))
);
return { statusCode, data, responseOptions };
};
Expand All @@ -175,7 +210,7 @@ export function recordFirehoseRequest(
data: string | object | Buffer | undefined,
responseOptions?: {
headers: Record<string, string | string[] | undefined>;
},
}
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ({ body }: any) => {
Expand Down

0 comments on commit 7ad1ebf

Please sign in to comment.