-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |