Replies: 8 comments 5 replies
-
Would also be in need of this! I'm programmatically provisioning a database on Neon with their API and I'd like to run the schema migration directly from my code on that newly created database. |
Beta Was this translation helpful? Give feedback.
-
100% yes - came here to ask the question. I need to be able to create / apply the migrations programmatically . At the moment we're using typeorm and db-migrate but was hoping to move to drizzle and go all in. |
Beta Was this translation helpful? Give feedback.
-
Looking for this, too. Forcing us to use |
Beta Was this translation helpful? Give feedback.
-
Drizzle team any updates here? And did anyone find workaround? |
Beta Was this translation helpful? Give feedback.
-
We have a similar use case where we need to create the DDL statements for our drizzle schema programmatically. We don’t need migrations from an old schema to the current one, we just need to create the current schema. drizzle-kit exports some functions that help building a solution: import { generateDrizzleJson, generateMigration } from 'drizzle-kit/api'
import { date, integer, pgTable, timestamp } from 'drizzle-orm/pg-core'
// [...]
// the drizzle schema we want to create DDL statements for
const schema = {
testTable1: pgTable('testtable1', {
id: integer('id').primaryKey(),
testdate: date('testdate').notNull(),
}),
testTable2: pgTable('testtable2', {
id: integer('id').primaryKey(),
testtimestamp: timestamp('testtimestamp').notNull(),
}),
}
// drizzle-kit seems to always migrate from one schema to another
// initially creating a schema seems to be equivalent to migrating from an empty schema
const prevSchema = {}
const migrationStatements = await generateMigration(
generateDrizzleJson(prevSchema),
generateDrizzleJson(schema),
)
console.log(migrationStatements.join('\n'))
// prints this:
//
// CREATE TABLE IF NOT EXISTS "testtable1" (
// "id" integer PRIMARY KEY NOT NULL,
// "testdate" date NOT NULL
// );
//
// CREATE TABLE IF NOT EXISTS "testtable2" (
// "id" integer PRIMARY KEY NOT NULL,
// "testtimestamp" timestamp NOT NULL
// ); I didn’t find any documentation for drizzle-kit/api, instead I figured this out by inspecting drizzle-kit’s source code. There is more to discover in drizzle-kit/api, so you may be able to solve some other tasks with it. |
Beta Was this translation helpful? Give feedback.
-
I'm hoping to do something similar; but need the full flow including generating a diff migration from an existing state. I believe the CLI does it using Anybody have a workaround; would it be useful to open an issue that asks for more power in |
Beta Was this translation helpful? Give feedback.
-
Second that! All operations |
Beta Was this translation helpful? Give feedback.
-
I have found that the correct way of doing this is doing something like: import { db } from '../src/lib/db_postgresql/db';
import { cuentas } from '../src/lib/db_postgresql/schema';
import { generateDrizzleJson, generateMigration } from 'drizzle-kit/api';
const prevSchema = {};
const schema = { cuentas };
const migrationStatements = await generateMigration(
generateDrizzleJson(prevSchema),
generateDrizzleJson(schema)
);
await db.execute('drop table if exists cuentas');
await db.execute(migrationStatements.join('\n')); |
Beta Was this translation helpful? Give feedback.
-
We have some sync code that dynamically write tables & columns into our database based on shape of the incoming object. Therefore are not able to use a static schemas.ts file with the existing drizzle:kit based approach. Would it be possible to get the migration command drizzle
would
have run? Or at least be able to get a create (ideally create if not exists) statement for a given table specified in Drizzle syntax?Beta Was this translation helpful? Give feedback.
All reactions