Skip to content

Commit

Permalink
Merge pull request #6 from kimitrii/kimitri
Browse files Browse the repository at this point in the history
Implement updates and configurations to D1 database to run migrations before end to end tests
  • Loading branch information
kimitrii authored Nov 22, 2024
2 parents 9d09587 + bf5f41a commit f9578f6
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/deploy-cloudflare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ jobs:
with:
version: 9

- name: Deploy
- name: Run Migrations and Deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
preCommands: pnpm migration:prod

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"@cloudflare/workers-types": "^4.20241022.0",
"drizzle-kit": "^0.28.1",
"vitest": "^2.1.5",
"wrangler": "^3.83.0"
"wrangler": "^3.89.0"
}
}
67 changes: 65 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sql } from 'drizzle-orm';
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { ulid } from 'ulid';

export const customer = sqliteTable('customer', {
export const users = sqliteTable('users', {
id: text().$defaultFn(() => ulid()),
name: text({ length: 255 }).notNull(),
createdAt: text()
Expand Down
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Hono } from 'hono'
import { drizzle } from 'drizzle-orm/d1';
import { Env } from './types/drizzleTypes';
import { users } from './db/schema';

const app = new Hono()
const app = new Hono<{ Bindings: Env}>()

app.get('/', (c) => {
return c.text('Hello Hono!')
app.get('/', async (c) => {
const db = drizzle(c.env.DB)

await db.insert(users).values({
name: 'Eric',
})

const dbUsers = await db.select().from(users)

return c.json(dbUsers[0])
})

export default app
2 changes: 1 addition & 1 deletion src/migrations/0000_migration.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE `customer` (
CREATE TABLE `users` (
`id` text,
`name` text(255) NOT NULL,
`createdAt` text DEFAULT CURRENT_TIMESTAMP NOT NULL
Expand Down
6 changes: 3 additions & 3 deletions src/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"version": "6",
"dialect": "sqlite",
"id": "2d97d2b1-db71-4f41-bca4-137c0d5f701f",
"id": "904c021b-cbb6-47a0-8e75-310e6c9a836e",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"customer": {
"name": "customer",
"users": {
"name": "users",
"columns": {
"id": {
"name": "id",
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"idx": 0,
"version": "6",
"when": 1732221030610,
"when": 1732266483328,
"tag": "0000_migration",
"breakpoints": true
}
Expand Down
6 changes: 6 additions & 0 deletions src/types/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module "cloudflare:test" {
interface ProvidedEnv extends Env {
DB: D1Database
TEST_MIGRATIONS: D1Migration[]
}
}
18 changes: 11 additions & 7 deletions tests/sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { expect, test } from 'vitest'
import { expect, test, describe, beforeAll } from 'vitest'
import app from '../src'
import { applyD1Migrations, env } from 'cloudflare:test'

test('Sample E2E test', async () => {
const res = await app.request('/', {
method: 'GET'
describe('Example', () => {
beforeAll(async () => {
await applyD1Migrations(env.DB, env.TEST_MIGRATIONS);
})

test('Sample E2E test', async () => {
const res = await app.request('/', {}, env)

expect(res.status).toBe(200)
})

expect(res.status).toBe(200)
expect(await res.text()).toBe('Hello Hono!')
})
34 changes: 22 additions & 12 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { defineWorkersConfig, readD1Migrations } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersConfig({

test: {
globals: true,
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
},
},
},
});
export default defineWorkersConfig(async () => {
const migrations = await readD1Migrations("./src/migrations")
return {
test: {
globals: true,
poolOptions: {
workers: {
isolatedStorage: true,
miniflare: {
bindings: {
TEST_MIGRATIONS: migrations
}
},
wrangler: {
configPath: "./wrangler.toml"
}
}
}
}
}
})
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ compatibility_flags = [ "nodejs_compat" ]
[[d1_databases]]
binding = "DB"
database_name = "legion-db"
database_id = "c6bc1ae3-4c33-499d-a7d2-d10e6d667822"
database_id = "2e03019b-39d6-4a9a-90de-90f8bd6282e2"
migrations_dir = "./src/migrations"

0 comments on commit f9578f6

Please sign in to comment.