Push schema programatically? #4373
Replies: 5 comments 2 replies
-
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.
-
Why not just apply current migrations? Because of fast schema changes? Also maybe reverse engineering of |
Beta Was this translation helpful? Give feedback.
-
Would |
Beta Was this translation helpful? Give feedback.
-
Hello, I would really like to know how to use this function as well. There seems to be very little information about how to apply push and migrations in production. |
Beta Was this translation helpful? Give feedback.
-
I think you'll find what you're looking for here- #4205 (Using in-memory Postgres when testing with vitest) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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