Skip to content

Commit

Permalink
Add test for Kysely migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Aug 2, 2023
1 parent 96a6ccf commit a7ee09f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/kysely/migrations.test.ts
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']);
});
});
14 changes: 14 additions & 0 deletions test/kysely/migrations/2023-08-01.ts
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();
},
};
13 changes: 13 additions & 0 deletions test/kysely/migrations/2023-08-02.ts
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();
},
};
8 changes: 8 additions & 0 deletions test/kysely/migrations/index.ts
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,
};

0 comments on commit a7ee09f

Please sign in to comment.