Skip to content

Commit

Permalink
Add migration in DO tests
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 9, 2024
1 parent 7220de0 commit 4e81208
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type Env = {
DB: D1Database
TEST_DO: DurableObjectNamespace
}

declare module 'cloudflare:test' {
Expand Down
10 changes: 10 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DurableObject } from 'cloudflare:workers'
import { Env } from './bindings'

export class TestDO extends DurableObject {}

export default {
async fetch(request: Request, env: Env) {
return new Response('test')
},
}
File renamed without changes.
152 changes: 152 additions & 0 deletions tests/integration/migrations-do.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { env, runInDurableObject } from 'cloudflare:test'
import { describe, expect, it } from 'vitest'
import { D1QB, DOQB, Migration } from '../../src'

export const migrations: Migration[] = [
{
name: '100000000000000_add_logs_table.sql',
sql: `
create table logs
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`,
},
]

describe('Migrations', () => {
it('initialize', async () => {
const id = env.TEST_DO.idFromName('test')
const stub = env.TEST_DO.get(id)

await runInDurableObject(stub, async (_instance, state) => {
const qb = new DOQB(state.storage.sql)

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([])

qb.migrations({ migrations }).initialize()

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([
{
name: 'migrations',
},
])
})
})

it('apply', async () => {
const id = env.TEST_DO.idFromName('test')
const stub = env.TEST_DO.get(id)

await runInDurableObject(stub, async (_instance, state) => {
const qb = new DOQB(state.storage.sql)

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([])

const applyResp = qb.migrations({ migrations }).apply()

expect(applyResp.length).toEqual(1)
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([
{
name: 'migrations',
},
{
name: 'logs',
},
])

const applyResp2 = qb.migrations({ migrations }).apply()
expect(applyResp2.length).toEqual(0)
})
})

it('incremental migrations', async () => {
const id = env.TEST_DO.idFromName('test')
const stub = env.TEST_DO.get(id)

await runInDurableObject(stub, async (_instance, state) => {
const qb = new DOQB(state.storage.sql)

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([])

const applyResp = qb.migrations({ migrations }).apply()

expect(applyResp.length).toEqual(1)
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')

expect(
Array.from(
state.storage.sql.exec(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`)
)
).toEqual([
{
name: 'migrations',
},
{
name: 'logs',
},
])

const updatedMigrations = [
...migrations,
{
name: '100000000000001_add_second_table.sql',
sql: `
create table logs_two
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`,
},
]

const applyResp2 = qb.migrations({ migrations: updatedMigrations }).apply()

expect(applyResp2.length).toEqual(1)
expect(applyResp2[0]?.name).toEqual('100000000000001_add_second_table.sql')

const applyResp3 = qb.migrations({ migrations: updatedMigrations }).apply()
expect(applyResp3.length).toEqual(0)
})
})
})
3 changes: 3 additions & 0 deletions tests/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: {
configPath: './wrangler.toml',
},
miniflare: {
compatibilityFlags: ['nodejs_compat'],
d1Databases: {
Expand Down
11 changes: 11 additions & 0 deletions tests/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "test"
main = "index.ts"
compatibility_date = "2024-11-09"

[[durable_objects.bindings]]
name = "TEST_DO"
class_name = "TestDO"

[[migrations]]
tag = "v1"
new_sqlite_classes = ["TestDO"]

0 comments on commit 4e81208

Please sign in to comment.