98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { type Client, createClient } from '@libsql/client/sqlite3';
|
|
import retry from 'async-retry';
|
|
import { sql } from 'drizzle-orm';
|
|
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
import { migrate } from 'drizzle-orm/libsql/migrator';
|
|
import { drizzle } from 'drizzle-orm/libsql/sqlite3';
|
|
import { afterAll, beforeAll, beforeEach, expect, test } from 'vitest';
|
|
import { skipTests } from '~/common';
|
|
import { randomString } from '~/utils';
|
|
import { anotherUsersMigratorTable, tests, usersMigratorTable } from './sqlite-common';
|
|
|
|
const ENABLE_LOGGING = false;
|
|
|
|
let db: LibSQLDatabase;
|
|
let client: Client;
|
|
|
|
beforeAll(async () => {
|
|
const url = ':memory:';
|
|
client = await retry(async () => {
|
|
client = createClient({ url });
|
|
return client;
|
|
}, {
|
|
retries: 20,
|
|
factor: 1,
|
|
minTimeout: 250,
|
|
maxTimeout: 250,
|
|
randomize: false,
|
|
onRetry() {
|
|
client?.close();
|
|
},
|
|
});
|
|
db = drizzle(client, { logger: ENABLE_LOGGING });
|
|
});
|
|
|
|
afterAll(async () => {
|
|
client?.close();
|
|
});
|
|
|
|
beforeEach((ctx) => {
|
|
ctx.sqlite = {
|
|
db,
|
|
};
|
|
});
|
|
|
|
test('migrator', async () => {
|
|
await db.run(sql`drop table if exists another_users`);
|
|
await db.run(sql`drop table if exists users12`);
|
|
await db.run(sql`drop table if exists __drizzle_migrations`);
|
|
|
|
await migrate(db, { migrationsFolder: './drizzle2/sqlite' });
|
|
|
|
await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }).run();
|
|
const result = await db.select().from(usersMigratorTable).all();
|
|
|
|
await db.insert(anotherUsersMigratorTable).values({ name: 'John', email: 'email' }).run();
|
|
const result2 = await db.select().from(anotherUsersMigratorTable).all();
|
|
|
|
expect(result).toEqual([{ id: 1, name: 'John', email: 'email' }]);
|
|
expect(result2).toEqual([{ id: 1, name: 'John', email: 'email' }]);
|
|
|
|
await db.run(sql`drop table another_users`);
|
|
await db.run(sql`drop table users12`);
|
|
await db.run(sql`drop table __drizzle_migrations`);
|
|
});
|
|
|
|
test('migrator : migrate with custom table', async () => {
|
|
const customTable = randomString();
|
|
await db.run(sql`drop table if exists another_users`);
|
|
await db.run(sql`drop table if exists users12`);
|
|
await db.run(sql`drop table if exists ${sql.identifier(customTable)}`);
|
|
|
|
await migrate(db, { migrationsFolder: './drizzle2/sqlite', migrationsTable: customTable });
|
|
|
|
// test if the custom migrations table was created
|
|
const res = await db.all(sql`select * from ${sql.identifier(customTable)};`);
|
|
expect(res.length > 0).toBeTruthy();
|
|
|
|
// test if the migrated table are working as expected
|
|
await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' });
|
|
const result = await db.select().from(usersMigratorTable);
|
|
expect(result).toEqual([{ id: 1, name: 'John', email: 'email' }]);
|
|
|
|
await db.run(sql`drop table another_users`);
|
|
await db.run(sql`drop table users12`);
|
|
await db.run(sql`drop table ${sql.identifier(customTable)}`);
|
|
});
|
|
|
|
skipTests([
|
|
'delete with limit and order by',
|
|
'update with limit and order by',
|
|
'transaction',
|
|
'transaction rollback',
|
|
'nested transaction',
|
|
'nested transaction rollback',
|
|
]);
|
|
|
|
tests();
|