forked from DallasHoff/sqlocal
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
96a6ccf
commit a7ee09f
Showing
4 changed files
with
87 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { SQLocalKysely } from '../../src/kysely'; | ||
import { Kysely, Migrator } from 'kysely'; | ||
|
||
describe('kysely migrations', () => { | ||
const { dialect } = new SQLocalKysely('kysely-migrations-test.sqlite3'); | ||
const db = new Kysely({ dialect }); | ||
|
||
const migrator = new Migrator({ | ||
db, | ||
provider: { | ||
async getMigrations() { | ||
const { migrations } = await import('./migrations'); | ||
return migrations; | ||
}, | ||
}, | ||
}); | ||
|
||
const getTableNames = async () => { | ||
const tables = await db.introspection.getTables(); | ||
return tables.map((table) => table.name); | ||
}; | ||
|
||
const getColumnNames = async (tableName: string) => { | ||
const tables = await db.introspection.getTables(); | ||
const table = tables.find((table) => table.name === tableName); | ||
return table?.columns.map((column) => column.name); | ||
}; | ||
|
||
it('should migrate the database', async () => { | ||
expect(await getTableNames()).toEqual([]); | ||
|
||
await migrator.migrateToLatest(); | ||
expect(await getTableNames()).toEqual(['groceries']); | ||
expect(await getColumnNames('groceries')).toEqual([ | ||
'id', | ||
'name', | ||
'quantity', | ||
]); | ||
|
||
await migrator.migrateDown(); | ||
expect(await getTableNames()).toEqual(['groceries']); | ||
expect(await getColumnNames('groceries')).toEqual(['id', 'name']); | ||
|
||
await migrator.migrateDown(); | ||
expect(await getTableNames()).toEqual([]); | ||
|
||
await migrator.migrateUp(); | ||
expect(await getTableNames()).toEqual(['groceries']); | ||
expect(await getColumnNames('groceries')).toEqual(['id', 'name']); | ||
}); | ||
}); |
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,14 @@ | ||
import { Kysely, Migration } from 'kysely'; | ||
|
||
export const Migration20230801: Migration = { | ||
async up(db: Kysely<any>) { | ||
await db.schema | ||
.createTable('groceries') | ||
.addColumn('id', 'integer', (cb) => cb.primaryKey().autoIncrement()) | ||
.addColumn('name', 'text', (cb) => cb.notNull()) | ||
.execute(); | ||
}, | ||
async down(db: Kysely<any>) { | ||
await db.schema.dropTable('groceries').execute(); | ||
}, | ||
}; |
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,13 @@ | ||
import { Kysely, Migration } from 'kysely'; | ||
|
||
export const Migration20230802: Migration = { | ||
async up(db: Kysely<any>) { | ||
await db.schema | ||
.alterTable('groceries') | ||
.addColumn('quantity', 'integer') | ||
.execute(); | ||
}, | ||
async down(db: Kysely<any>) { | ||
await db.schema.alterTable('groceries').dropColumn('quantity').execute(); | ||
}, | ||
}; |
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,8 @@ | ||
import { Migration } from 'kysely'; | ||
import { Migration20230801 } from './2023-08-01'; | ||
import { Migration20230802 } from './2023-08-02'; | ||
|
||
export const migrations: Record<string, Migration> = { | ||
'2023-08-02': Migration20230802, | ||
'2023-08-01': Migration20230801, | ||
}; |