chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import { type Client, createClient } from '@libsql/client/node';
|
||||
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/node';
|
||||
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 = process.env['LIBSQL_URL'];
|
||||
const authToken = process.env['LIBSQL_AUTH_TOKEN'];
|
||||
if (!url) {
|
||||
throw new Error('LIBSQL_URL is not set');
|
||||
}
|
||||
client = await retry(async () => {
|
||||
client = createClient({ url, authToken });
|
||||
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',
|
||||
]);
|
||||
|
||||
tests();
|
||||
Reference in New Issue
Block a user