Push schema programatically? #4373
Unanswered
Nikola-Milovic
asked this question in
Q&A
Replies: 2 comments 1 reply
-
I've tried to do the same for the exact same reason. It doesn't appear to be a supported feature but here are a couple of issues that I've come across that helped me in my journey: #2853 and #3913 I'm using Vitest for testing and my current import { createRequire } from 'node:module';
import { PGlite } from '@electric-sql/pglite';
import type * as DrizzleKit from 'drizzle-kit/api';
import { drizzle, PgliteDatabase } from 'drizzle-orm/pglite';
import { afterAll, vi } from 'vitest';
import { relations } from '@/db/relations';
import * as schema from '@/db/schema';
// workaround for https://github.com/drizzle-team/drizzle-orm/issues/2853
const require = createRequire(import.meta.url);
const { generateDrizzleJson, generateMigration } = require('drizzle-kit/api') as typeof DrizzleKit;
// end of workaround
// workaround for https://github.com/drizzle-team/drizzle-orm/issues/3913
async function pushSchema(db: PgliteDatabase<typeof schema, typeof relations>) {
const prevJson = generateDrizzleJson({});
const curJson = generateDrizzleJson(schema, prevJson.id, undefined, 'snake_case');
const statements = await generateMigration(prevJson, curJson);
for (const statement of statements) {
await db.execute(statement);
}
}
// end of workaround
let client: PGlite;
// Replace the database with an in-memory database (Pglite)
vi.mock('@/db', async () => {
client = new PGlite();
const db = drizzle(client, { schema, relations, casing: 'snake_case' });
await pushSchema(db);
return { default: db };
});
afterAll(async () => {
await client?.close();
}); Hope that helps! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Why not just apply current migrations? Because of fast schema changes? Also maybe reverse engineering of |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey! I manage my schema purely in Drizzle and I mostly push the schema during development, and I would like to also push the schema during tests. For that I would need a method like
db.migrate({schema: my_schemas})
, that does NOT require having pregenerated migration files like the current migration function does.I am looking through the issues and discussions and cant find any mentions of this feature, is this not a common need for people? How do you migrate your db during tests?
Beta Was this translation helpful? Give feedback.
All reactions