-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync-sql.test.ts
94 lines (88 loc) · 3.03 KB
/
sync-sql.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { describe, expect, it } from 'bun:test'
import { Kysely } from 'kysely'
import { executeSQL } from '../src'
import { column, defineTable, generateMigrateSQL, type Schema } from '../src/schema'
import { createDialect } from './utils'
const db = new Kysely({ dialect: createDialect() })
describe('test sync sql', async () => {
async function run(schema: Schema, expectSQLs: string[]): Promise<Promise<void>> {
const initSQLs = await generateMigrateSQL(db, schema)
expect(initSQLs).toStrictEqual(expectSQLs)
for (const sql of initSQLs) {
await executeSQL(db, sql)
}
}
it('should generate sqls and run', async () => {
await run(
{
test: defineTable({
columns: {
id: column.increments(),
person: column.object({ defaultTo: { name: 'test' } }),
gender: column.boolean({ notNull: true }),
},
primary: 'id', // optional
index: ['person', ['id', 'gender']],
createAt: true, // `createTime` column
updateAt: true, // `updateTime` column
}),
},
[
'CREATE TABLE IF NOT EXISTS "test" ("id" INTEGER PRIMARY KEY AUTOINCREMENT,"person" TEXT DEFAULT \'{"name":"test"}\',"gender" INTEGER NOT NULL,"createAt" TEXT DEFAULT CURRENT_TIMESTAMP,"updateAt" TEXT DEFAULT CURRENT_TIMESTAMP);',
'CREATE INDEX IF NOT EXISTS idx_test_person on "test" ("person");',
'CREATE INDEX IF NOT EXISTS idx_test_id_gender on "test" ("id","gender");',
'CREATE TRIGGER IF NOT EXISTS "tgr_test_updateAt" AFTER UPDATE ON "test" BEGIN UPDATE "test" SET "updateAt" = CURRENT_TIMESTAMP WHERE "rowid" = NEW."rowid"; END;',
],
)
await run(
{
test: defineTable({
columns: {
id: column.increments(),
person: column.object({ defaultTo: { name: 'test' } }),
gender: column.boolean({ notNull: true }),
},
primary: 'id', // optional
index: ['person', ['id', 'gender']],
}),
},
[
'DROP TRIGGER IF EXISTS "tgr_test_updateAt";',
'ALTER TABLE "test" DROP COLUMN "createAt";',
'ALTER TABLE "test" DROP COLUMN "updateAt";',
],
)
await run(
{
test: defineTable({
columns: {
id: column.increments(),
person: column.object({ defaultTo: { name: 'test' } }),
gender: column.boolean({ notNull: true }),
},
primary: 'id', // optional
}),
},
[
'DROP INDEX IF EXISTS "idx_test_id_gender";',
'DROP INDEX IF EXISTS "idx_test_person";',
],
)
await run(
{
test: defineTable({
columns: {
id: column.increments(),
person: column.object({ defaultTo: { name: 'test' } }),
type: column.string().$cast<'normal' | 'premium'>(),
},
primary: 'id',
}),
},
[
'ALTER TABLE "test" ADD COLUMN "type" TEXT;',
'ALTER TABLE "test" DROP COLUMN "gender";',
],
)
})
})